简体   繁体   English

Elasticsearch可以建议在数字字段上查询的范围吗?

[英]Can Elasticsearch suggest range for query on number fields?

For example I have a list of person with have age property. 例如,我有一个具有age属性的人的列表。
On my search page, I allow user to query this list base on the range of age: 在搜索页面上,我允许用户根据年龄范围查询此列表:

"mus" : {
    "range" : {
        "age" : { "from" : 60, "to" : 80 }
    }
},

Let say the above query return 0 result (since my list include only person from 20 to 50), do ES support any method to return the suggestion for this range query (in this case [20, 50])? 假设上述查询返回0个结果(由于我的列表仅包含20至50个人),ES是否支持任何方法返回此范围查询的建议(在本例中为[20,50])?

Yes, you can use the histogram aggregation in order to retrieve these counts. 是的,您可以使用histogram聚合来检索这些计数。

{
  "post_filter": {
    "bool": {
      "must": {
        "range": {
          "age": {
            "from": 60,
            "to": 80
          }
        }
      }
    }
  },
  "aggs": {
    "ages": {
      "histogram": {
        "field": "age",
        "interval": 10
      }
    }
  }
}

The above query would return: 上面的查询将返回:

  • all documents whose age field is between 60 and 80 所有age在60至80 age之间的文件
  • the number of documents whose age field is within 10-year buckets, ie age字段在10年以内的文档数,即
    • for age between 10 and 20: 4 documents (fake number) 10至20岁之间:4个文件(假号码)
    • for age between 20 and 30: 6 documents (fake number) 适用于20至30岁的年龄:6个文档(假号码)
    • for age between 30 and 40: 12 documents (fake number) 30至40岁之间:12个文件(假号码)
    • for age between 40 and 50: 2 documents (fake number) 40至50岁之间:2个文件(假号码)
    • ... ...

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

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