导致在 elasticsearch 中返回零结果的空 bool 子句

Empty bool clause causing zero results being returned in elasticsearch

我在 ruby 中写了一个 API 帮助客户构建 elasticsearch 查询 dsl。 构建的查询包含一个空布尔值,如下所示,这会导致问题。由于 bool 是空的,它导致返回 0 个结果。如果我删除 bool,我会得到预期的结果。 如何在不删除那个 bool 的情况下将它变成 match_all? 我需要将 bool 留在那里,直到下一个版本可以删除它。如果我默认在 bool 中添加 must 并在其中添加 match_all ,我觉得它可能会产生意想不到的结果。

弹性版本:1.0.1

{
    "query": {
        "filtered": {
            "query": {
                "bool": {} <--- Causing problems
            },
            "filter": {
                "query": {
                    "query_string": {
                        "fields": [
                            [
                                "name_field",
                                "message_field"
                            ]
                        ],
                        "query": "Halo AND Yaylo"
                    }
                }
            }
        }
    },
    "sort": [
        {
            "interactions": {
                "order": "desc"
            }
        }
    ]
}

此行为在 https://github.com/elastic/elasticsearch/issues/7240 中发生了变化,这意味着从 ES 版本 1.3.3、2.0 和 1.4 开始。

同时,如果你可以过去this statement about bool:

By default, none of the should clauses are required to match, with one exception: if there are no must clauses, then at least one should clause must match.

然后您可以在 must:

中使用它
        {
          "constant_score": {
            "filter": {
              "query": {"match_all": {}}
            },
            "boost": 0
          }
        }

无伤大雅(从得分的角度)must声明。