为什么“存在”对于正常查询而不是过滤查询失败?

时间:2016-06-16 13:08:06

标签: elasticsearch filter querydsl

考虑以下两个问题:

{
  "_source" : false,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "gte": 1460789348000,
              "lte": 1466059748000
            }
          }
        }
      ]
    }
  }
}

现在考虑添加filteredfilter字段的以下查询:

{
  "_source": false,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "date": {
                  "gte": 1460789348000,
                  "lte": 1466059748000
                }
              }
            }
          ]
        }
      }
    }
  }
}

两者都给出相同的结果,没有filteredfilter字段的第一个查询不会导致错误,这与以下查询不同:

{
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "comment"
          }
        }
      ]
    }
  }
}

错误:

{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[bi8BrruxR9i_H8-n4Qo9Mg][acct_prop_5][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[acct_prop_5][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[acct_prop_5] No query registered for [exists]]; }{[JorHtifCSxGrbb3ovGJMWQ][advocacy_insights_p5_v1_00000000_0000][0]: RemoteTransportException[[Caiera][inet[/172.16.0.11:9304]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[advocacy_insights_p5_v1_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[advocacy_insights_p5_v1_00000000_0000] No query registered for [exists]]; }{[Tlt3tXF_S9-rEmK9iwSvoA][audience_act_p5_v2_00000000_0000][0]: RemoteTransportException[[Monet St. Croix][inet[/172.16.0.11:9303]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[audience_act_p5_v2_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v2_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][audience_act_p5_v3_00000000_0000][0]: SearchParseException[[audience_act_p5_v3_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v3_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][audience_act_p5_v3_20150601_0000][0]: SearchParseException[[audience_act_p5_v3_20150601_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v3_20150601_0000] No query registered for [exists]]; }{[Tlt3tXF_S9-rEmK9iwSvoA][auth2][0]: RemoteTransportException[[Monet St. Croix][inet[/172.16.0.11:9303]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[auth2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[auth2] No query registered for [exists]]; }{[JorHtifCSxGrbb3ovGJMWQ][authors][0]: RemoteTransportException[[Caiera][inet[/172.16.0.11:9304]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[authors][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[authors] No query registered for [exists]]; }{[bi8BrruxR9i_H8-n4Qo9Mg][bm_content_lifetime_p5_v1_00000000_0000][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[bm_content_lifetime_p5_v1_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[bm_content_lifetime_p5_v1_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][bm_content_lifetime_p5_v2_00000000_0000][0]: SearchParseException[[bm_content_lifetime_p5_v2_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[bm_content_lifetime_p5_v2_00000000_0000] No query registered for [exists]]; }{[bi8BrruxR9i_H8-n4Qo9Mg][bm_content_lifetime_p5_v93_00000000_0000][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[bm_content_lifetime_p5_v93_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: .........

然后使用这两个字段成为正确的查询:

{
  "_source": false,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "comment"
              }
            }
          ]
        }
      }
    }
  }
}

rangeexists查询之间有什么区别,它只允许一个查询在没有filteredfilter字段的情况下有效?

1 个答案:

答案 0 :(得分:1)

您可能必须使用旧版本的Elasticsearch [版本< 2.0 ]其中exists是一个过滤器。 没有 exists查询就是您收到错误消息的原因:No query registered for [exists]

相反,您可以使用:

{
   "_source": false,
   "filter": {
      "bool": {
         "must": [
            {
               "exists": {
                  "field": "comment"
               }
            }
         ]
      }
   }
}

或者简单地说:

{
   "_source": false,
   "filter": {
      "exists": {
         "field": "comment"
      }
   }
}

从Elasticsearch版本2.0开始,查询过滤器对象已合并在一起,只有查询对象能够执行两个功能。因此,您可以从版本2.0开始使用exists查询。

您可以在Better query execution coming to Elasticsearch 2.0

了解有关合并的更多信息