【问题标题】:Why my Elasticsearch query retrieves all indexed documents为什么我的 Elasticsearch 查询检索所有索引文档
【发布时间】:2019-02-12 09:34:16
【问题描述】:

我无法理解以下 Elasticsearch (ES 6.4) 查询的功能:

{
    "query" : {
        "bool" : {
            "should" : [
                {
                    "match" : {
                        "title" : {
                            "query" : "example",
                            "operator" : "AND",
                            "boost" : 2
                        }
                    }
                },
                {
                    "multi_match" : {
                        "type" : "best_fields",
                        "query" : "example",
                        "operator" : "AND",
                        "fields" : [
                            "author", "content", "tags"
                        ],
                        "boost" : 1
                    }
                }
            ],
            "must" : [
                {
                    "range" : {
                        "dateCreate" : {
                            "gte" : "2000-01-01T00:00:00+0200",
                            "lte" : "2019-02-12T23:59:59+0200"
                        }
                    }
                },
                {
                    "term" : {
                        "client" : {
                            "value" : "test",
                            "boost" : 1
                        }
                    }
                }
            ]
        }
    },
    "size" : 10,
    "from" : 0,
    "sort" : [
        {
            "_score" : {
                "order" : "desc"
            }
        }
    ]
}

查询成功执行,但检索到大约 400,000 个文档,这是我的索引的总数。这意味着所有文档都在结果集中。但为什么?这真的是 multi_match 查询的正确行为吗?
当我还在使用 query_string 查询时,我只得到了实际匹配的文档。这就是为什么我有点惊讶。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你错过了minimum_should_match

        "bool" : {
            "minimum_should_match": 1,            <--- add this
            "should" : [
               ...
    

    【讨论】:

    • 但也有must 子句,因此即使should 子句中的查询都不匹配,也应根据该条件过滤文档(除非该条件与所有文档匹配)。 minimum_should_match 会以某种方式影响should 子句中的至少一个应该匹配。
    • 那么显然,这两个 must 子句匹配所有文档,因为我怀疑所有文档都包含 example 令牌。如果没有minimum_should_match,这两个 should 子句仅用作排名助推器。
    • 这样搜索词肯定不会出现在所有文档中,所以查询有问题。我现在将 minimum_should_match 设置为 1 并且只得到四分之一的结果,这在我看来太多了。如果我将 minium_should_match 设置为 2,我可以将结果集限制为 4,000。但是效果我不太明白。
    • @altralaser 当您完全删除 should 子句并且查询只有 must 子句时会发生什么?
    • 措辞不同,您希望在这些条件下有多少文档?即当你使用query_string 查询时你得到了多少。另请分享该查询以及您更改的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多