弹性搜索SQL语句的DSL语法等效

时间:2015-07-12 23:14:21

标签: elasticsearch tire elasticsearch-plugin

我正试图在弹性搜索查询中复制下面的查询逻辑,但有些事情是不对的。

以下查询基本上会返回一个doc。我想要应用第一个条件:"name": "iphone" 更复杂的第二个条件:(username = 'gogadget' AND status_type = '1' AND created_time between 4532564 AND 64323238)。请注意,嵌套bool必须才能处理更复杂的条件。如果我将"name": "iphone"的外部匹配更改为"name": "wrong value",我仍然会看到1个doc。但是当我这样做时,我什么也得不到。我不确定这是错的。

SQL查询如下所示。

SELECT * from data_points 
WHERE name = 'iphone'
    OR 
(username = 'gogadget' AND status_type = '1' AND created_time between 4532564 AND 64323238) 


{
    "size": 30,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "minimum_should_match": "1",
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "match": {
                                                "username": "gogadget"
                                            }
                                        },
                                        {
                                            "terms": {
                                                "status_type": [
                                                    "3",
                                                    "4"
                                                ]
                                            }
                                        },
                                        {
                                            "range": {
                                                "created_time": {
                                                    "gte": 20140712,
                                                    "lte": 1405134711
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ],
                        "must": [],
                        "must_not": []
                    }
                },
                {
                    "match": {
                        "name": "iphone"
                    }
                }
            ]
        }
    }
}

1 个答案:

答案 0 :(得分:1)

should查询将与查询匹配并返回。

您不需要使用must汇总OR query

查询应该如下:

{
    "query": {
        "bool": {
            "should": [{
                "bool": {
                    "must": [{
                        "match": {
                            "username": "gogadget"
                        }
                    }, {
                        "terms": {
                            "status_type": [
                                "3",
                                "4"
                            ]
                        }
                    }, {
                        "range": {
                            "created_time": {
                                "gte": 20140712,
                                "lte": 1405134711
                            }
                        }
                    }]
                }
            }, {
                "match": {
                    "name": "iphone"
                }
            }]
        }
    }
}