弹性搜索范围滤波器用于阵列字段 [英] elasticsearch range filter for array field

查看:115
本文介绍了弹性搜索范围滤波器用于阵列字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含整数数组的字段,例如:

  _source:{
...
价格:[
20001,
30001
]
}

我想过滤结果,使价格至少包含以下值之一:
[20002,30000]#不会返回上述文档,因为20002和30000
之间没有值,但$ code> [10000,20002] 将返回上述文档,因为20001在价格字段以上文件在10000到20002之间

解决方案

Elasticsearch总是认为一个字段可以包含一个值列表,所以范围过滤器应该工作。如果任何值与范围相匹配,将被过滤。



您可以将该过滤器作为过滤查询

  {
query:{
filtered:{
filter:{
range:{
price:{
gte:10000,
lte:20002
}
}
}
}
}
}

但是,过滤的查询在2.0中已被弃用,所以如果您使用2.0您可以更好地使用 bool query 与过滤器:

  {
查询:{
bool:{
必须:{
match_all:{}
},
过滤器:{
范围:{
price:{
gte:10000,
lte:20002
}
}
}
}
}
}

请注意,我在示例中使用过滤器,因为您要求过滤器:)


I have a field containing an array of integers eg:

_source: {
  ...
  prices: [
    20001,
    30001
  ]
}

I'd like to filter the results such that prices contains at least one of a list of values which are between eg: [20002,30000] # would not return the above document because no value is between 20002 and 30000 but [10000,20002] would return above document because 20001 in the prices field of above document is between 10000 and 20002

解决方案

Elasticsearch always considers that a field can contain a list of values so, a range filter should work. If any of the values matches the range it will be filtered in.

You can use that filter as part of a filtered query:

{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "prices": {
            "gte": 10000,
            "lte": 20002
          }
        }
      }
    }
  }
}

However, filtered query is deprecated in 2.0, so, if you are using 2.0 you can better use a bool query with a filter:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}   
      },  
      "filter": {
        "range": {
          "prices": {
            "gte": 10000,
            "lte": 20002
          }
        }
      }
    }
  }
}

Note that I'm using a filter in my examples because you asked for a filter :)

这篇关于弹性搜索范围滤波器用于阵列字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆