[英]Combining query and filter on same field in elastic search
I have address doc type which has city as field. 我有地址的doc类型,有城市作为字段。 The city field uses analyser 城市领域使用分析仪
"keyword_analyzer": {
"tokenizer": "keyword", # So that we don't split multi word city name
"filter": ["lowercase"] # The search needs to case insensitive
}
How can I get any address which matches the give city 'X' or city is null. 如何获得与给定城市“X”或城市匹配的任何地址为空。 In sql it would be written as 在sql中它将被写为
SELECT * FROM address
WHERE city = 'X' or city IS NULL
I guess I have to use match query to get city ='X'
and 'missing' filter to get city IS NULL
. 我想我必须使用匹配查询来获取city ='X'
和'missing'过滤器以获得city IS NULL
。 How can I combine this? 我该如何结合这个?
Elasticsearch allows to combine queries with the bool query. Elasticsearch允许将查询与bool查询组合在一起。 Bool query has three fields: Bool查询有三个字段:
The following elasticsearch query is a boolean query containing a should field, with two sub-queries: 以下elasticsearch查询是一个包含should字段的布尔查询,包含两个子查询:
Results of this query will be equivalent to city = 'mycity' OR city IS NULL
: 此查询的结果将等同于city = 'mycity' OR city IS NULL
:
{
"query": {
"bool": {
"should": [
{
"match": {
"city": "mycity"
}
},
{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"missing": {
"field": "city"
}
}
}
}
]
}
}
}
Some useful resources: 一些有用的资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.