elasticsearch-2.0 - bool 查询不支持过​​滤器

标签 elasticsearch-2.0

这是使用 ES 2.0 抛出异常的查询:

bool query does not support filter

如何使用 Exists 和 Missing 查询?

询问:
{
   "bool":{
      "must":[
         {
            "bool":{
               "should":[
                  {
                     "bool":{
                        "must":[
                           {
                              "range":{
                                 "startDate":{
                                    "lte":"2016-10-27T11:24:49.6616538+05:30"
                                 }
                              }
                           }
                        ],
                        "filter":[
                           {
                              "bool":{
                                 "must_not":[
                                    {
                                       "exists":{
                                          "field":"endDate"
                                       }
                                    }
                                 ]
                              }
                           }
                        ]
                     }
                  }
               ]
            }
         }
      ]
   }
}

最佳答案

首先,该错误通常来自在 1.x 版本的 Elasticsearch 上使用。 (在这种情况下,您需要一个 FilteredQuery )

接下来,您似乎有许多级别的不必要的嵌套。不确定您是否删除了其他东西来制作一个更简单的示例。我已经像这样重写了您的查询(并添加了外括号):

{
    "query" : {
        "bool" : {
            "must" : [{
                    "range" : {
                        "startDate" : { "lte" : "2016-10-27T11:24:49.6616538+05:30" }
                    }
                }
            ],
            "filter" : [{
                    "bool" : {
                        "must_not" : [{
                                "exists" : { "field" : "endDate"    }
                            }
                        ]
                    }
                }
            ]} } 
}

您的原始查询和我重写的查询在我的服务器 (v2.3.1) 上都可以正常工作,所以我猜您真的有 ES 1.x 吗?

此外,如果您没有利用 lucene 评分,而只想返回文档(或应用您自己的排序),那么您可以完全删除过滤器并进一步简化它:
{
    "query" : {
        "bool" : {
            "must" : [{
                    "range" : {
                        "startDate" : { "lte" : "2016-10-27T11:24:49.6616538+05:30"}
                    }
                }
            ],
            "must_not" : [{
                    "exists" : { "field" : "endDate"    }
                }
            ]} } 
}

关于elasticsearch-2.0 - bool 查询不支持过​​滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40277134/

相关文章:

elasticsearch - 如何记录所有对 ElasticSearch 的请求?

elasticsearch - 在elasticsearch中创建索引时自定义分析器的mapper_parsing_exception?

php - 根据用户输入的值进行 Elasticsearch 排序

elasticsearch - 编写Elasticsearch 2.1脚本没有这样的属性:doc

elasticsearch - 如何检测何时将新的唯一术语插入到 Elasticsearch 中特定索引中特定字段的索引中?

javascript - Elasticsearch NodeJS 搜索 : How to retrieve results from response

elasticsearch - 查找完整日期,而不仅仅是年份(Elasticsearch)

curl - Elasticsearch: curl 不起作用

elasticsearch - Elasticsearch 2.x至5.x查询问题

Elasticsearch 聚合之聚合