【问题标题】:Elastic search DSL Syntax equivalence for SQL statementSQL 语句的 Elasticsearch DSL 语法等效项
【发布时间】:2015-10-01 03:13:42
【问题描述】:

我正在尝试在弹性搜索查询中复制以下查询逻辑,但有些地方不对劲。

基本上,下面的查询返回一个文档。我想应用第一个条件:"name": "iphone" 更复杂的第二个条件:(username = 'gogadget' AND status_type = '1' AND created_time between 4532564 AND 64323238)。请注意,在 should 中的 nested bool must 会处理更复杂的情况。如果我将 "name": "iphone" 的外部匹配更改为 "name": "wrong value",我仍然应该看到 1 个文档。但是当我这样做时,我什么也得不到。我不确定这是哪里错了。

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"
                    }
                }
            ]
        }
    }
}

【问题讨论】:

  • 你的 sql 查询返回什么?
  • sql查询?那不存在我只是用它来解释我想在弹性中做什么。
  • 好的。那么预期结果呢?
  • @eliasah 我在问题中添加了更多细节。我希望返回 1 个文档。请阅读问题的附加部分。谢谢!

标签: elasticsearch tire elasticsearch-plugin


【解决方案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"
                }
            }]
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2021-01-08
    • 2018-12-11
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多