在查询字符串查询中对范围字段进行Elasticsearch范围查询

时间:2020-10-22 08:38:25

标签: elasticsearch

我们正在将Elasticsearch从5.2更新到5.6,并且还在准备将其更新为6.x,然后再更新为7.x。

从5.2更新到5.6之后,并在所有文档中创建了新索引之后,我们发现某些内容无法像以前一样工作。

使用查询字符串查询(即?q = ...)时,我们曾经能够对范围字段进行范围查询。但是更新之后,会导致错误。

例如,我们有一个像这样的字段映射:

"typicalAgeRange": {
    "type": "integer_range"
}

过去,我们可以执行以下查询:

?q=typicalAgeRange:[0 TO 12]

这将返回所有年龄段与0-12相交的文档。 (例如6-8、6-14、0-4等)

但是,当我们现在在5.6上运行相同的查询时,我们得到:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"query_string\" : {\n    \"query\" : \"typicalAgeRange:[0 TO 12]\",\n    \"fields\" : [ ],\n    \"use_dis_max\" : true,\n    \"tie_breaker\" : 0.0,\n    \"default_operator\" : \"or\",\n    \"auto_generate_phrase_queries\" : false,\n    \"max_determinized_states\" : 10000,\n    \"enable_position_increments\" : true,\n    \"fuzziness\" : \"AUTO\",\n    \"fuzzy_prefix_length\" : 0,\n    \"fuzzy_max_expansions\" : 50,\n    \"phrase_slop\" : 0,\n    \"analyze_wildcard\" : false,\n    \"escape\" : false,\n    \"split_on_whitespace\" : true,\n    \"boost\" : 1.0\n  }\n}",
        "index_uuid": "4XB5FxxVSvq1pDxvTRRS3Q",
        "index": "udb3_core_v20201014145500"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "udb3_core_v20201014145500",
        "node": "sXvyyNlvRPyfYo3Gk2aC0g",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"query_string\" : {\n    \"query\" : \"typicalAgeRange:[0 TO 12]\",\n    \"fields\" : [ ],\n    \"use_dis_max\" : true,\n    \"tie_breaker\" : 0.0,\n    \"default_operator\" : \"or\",\n    \"auto_generate_phrase_queries\" : false,\n    \"max_determinized_states\" : 10000,\n    \"enable_position_increments\" : true,\n    \"fuzziness\" : \"AUTO\",\n    \"fuzzy_prefix_length\" : 0,\n    \"fuzzy_max_expansions\" : 50,\n    \"phrase_slop\" : 0,\n    \"analyze_wildcard\" : false,\n    \"escape\" : false,\n    \"split_on_whitespace\" : true,\n    \"boost\" : 1.0\n  }\n}",
          "index_uuid": "4XB5FxxVSvq1pDxvTRRS3Q",
          "index": "udb3_core_v20201014145500",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Field [typicalAgeRange] of type [integer_range] does not support range queries"
          }
        }
      }
    ]
  },
  "status": 400
}

我现在在文档中阅读了5.2和5.6:

可以为日期,数字或字符串字段指定范围。

5.2:https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-query-string-query.html#_ranges

5.6:https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-query-string-query.html#_ranges

因此,似乎从未得到官方支持(或未记录)? 但是为什么它在此之前有效,而现在却不再有效?

我们目前仍在生产中运行5.2,并且相同的查询仍然可以在其中运行。

更新:更多信息。

如果我们在JSON正文中进行范围查询,则可以得到预期的结果。例如:

{
    "query": {
        "range" : {
            "typicalAgeRange" : {
                "gte" : 0,
                "lte" : 12,
                "boost" : 2.0
            }
        }
    }
}

返回:

{
   "took":6,
   "timed_out":false,
   "_shards":{
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":6,
      "max_score":2.0,
      "hits":[
        ... // ommited for brevity
      ]
   }
}

因此JSON范围查询在范围字段上似乎工作正常。但是,出于多种原因,我们还需要它在Lucene语法中工作。 (向后兼容性是显而易见的兼容性之一,因为我们有一些系统使用此语法,但现在无法对其进行重构。)

0 个答案:

没有答案