简体   繁体   English

弹性搜索范围查询

[英]Elastic search range query

Consider 2 documents in an index as like below: 考虑索引中的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"
            }
         }

I want to construct a query for the following scenario. 我想为以下场景构建一个查询。 I have to find the documents for field in particular range. 我必须在特定范围内找到字段的文档。

field1:200-400 field2:300-400 so the above 2 documents should come. field1:200-400 field2:300-400所以上面的两个文件应该来了。

My query is as follows: 我的查询如下:

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

But the above query "Looks for 2 fields in a singe document, so no result is coming. SO i have to make to search if any of the filed satisfies the range in the document should return. Please share your ideas. Thanks in advance. 但是上面的查询“在单个文档中查找2个字段,因此没有结果。所以我必须进行搜索,如果任何满足文档范围的文件应该返回。请分享您的想法。提前致谢。

You need to use bool should and not bool must . 你需要使用bool should而不是bool must That would mean match any document that matches at least one condition. 这意味着匹配任何匹配至少一个条件的文档。 NOTE: Your second condition won't match second document as 400.66 does not fall in the range [300, 400] . 注意:您的第二个条件与第二个文档不匹配,因为400.66不在[300, 400]范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM