我有一个包含整数数组的字段,例如:
_source: {
...
prices: [
20001,
30001
]
}
我想过滤结果,使价格至少包含以下值之间的列表之一:[20002,30000] #
不会返回上述文档,因为没有值介于 20002 和 30000 之间
但[10000,20002]
将返回上述文档,因为上述文档的价格字段中的 20001 介于 10000 和 20002 之间
Elasticsearch 始终认为字段可以包含值列表,因此,范围过滤器 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-filter.html应该管用。如果任何值与范围匹配,它将被过滤。
您可以将该过滤器用作过滤查询 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html:
{
"query": {
"filtered": {
"filter": {
"range": {
"prices": {
"gte": 10000,
"lte": 20002
}
}
}
}
}
}
但是,过滤查询在 2.0 中已被弃用,因此,如果您使用 2.0,您可以更好地使用布尔查询 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html带过滤器:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"range": {
"prices": {
"gte": 10000,
"lte": 20002
}
}
}
}
}
}
请注意,我在示例中使用了过滤器,因为您要求使用过滤器:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)