【问题标题】:Elastic search range query弹性搜索范围查询
【发布时间】:2015-05-06 14:36:51
【问题描述】:

考虑索引中的 2 个文档,如下所示:

{
            "_index": "32",
            "_type": "places",
            "_id": "_FqlAzzSRN6Ge_294D5Mwg",
            "_score": 1,
            "_source": {
               "name_3": "xxxx",
               "id_3": "xxxxx",
               "name_2": "xxxx",
               "id_2": "xxx",
               "name_1": "xxx",
               "id_1": "xxx",
               "tempid": "xxxxx",
               "field1": 316.6666666666667,
               "type": "processeddata"
            }
         },
         {
            "_index": "32",
            "_type": "places",
            "_id": "3RCO-zHeSr2nWFZd8W-MDg",
            "_score": 1,
            "_source": {
               "name_3": "yyyy",
               "id_3": "yyy",
               "name_2": "yyy",
               "id_2": "yyy",
               "name_1": "yyyy",
               "id_1": "yyy",
               "tempid": "yy",
               "field2": 400.6666666666667,
               "type": "processeddata"
            }
         }

我想为以下场景构建一个查询。我必须找到特定范围内的字段的文档。

field1:200-400 field2:300-400 所以上面的2个文件应该来了。

我的查询如下:

"query": {
    "bool": {
      "must": [
        {
          "range": {
            "field1": {
              "gte": 200,
              "lte": 400
            }
          },"range": {
            "field2": {
              "gte": 300,
              "lte": 400
            }
          }
        }
      ]
    }
  }

但是上面的查询“在单个文档中查找 2 个字段,所以没有结果。所以我必须搜索是否有任何文件满足文档中的范围应该返回。请分享你的想法。谢谢提前。

【问题讨论】:

    标签: elasticsearch lucene


    【解决方案1】:

    您需要使用bool should 而不是bool must。这意味着匹配至少匹配一个条件的任何文档。 注意:您的第二个条件与第二个文档不匹配,因为 400.66 不在[300, 400] 范围内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多