[英]Keep the documents with a missing field in the filter query results
I'm querying my index with the following query: 我正在使用以下查询查询索引:
GET /dbpedia201510/entity/_search { "from": 0, "size": 10000, "query": { "bool": { "must": { "query_string":{ "fields": ["name","alias","pseudonyme"], "query": "Elias~ Franck~", "default_operator": "OR", "fuzziness": "auto" } }, "filter": { "bool": { "must": [ { "terms": { "job.keyword":["Architect","Politician","Tailor"] } } ] } } } } }
The job
field is an array of strings, and the query works like expected. job
字段是一个字符串数组,查询的工作方式与预期的一样。 Nevertheless, some of the documents do not have the job
field, and I want to get those documents as well. 但是,有些文档没有job
字段,我也想获取这些文档。
How can I do that? 我怎样才能做到这一点?
You need to use should clause
here, which matches when either job
field exists and matches terms query OR job
field doesn't exists. 您需要在此处使用should clause
,该should clause
在任何一个job
字段存在且匹配术语查询或 job
字段不存在时匹配。
Your final query should look like this: 您的最终查询应如下所示:
GET /dbpedia201510/entity/_search
{
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": {
"query_string": {
"fields": ["name","alias","pseudonyme"],
"query": "Elias~ Franck~",
"default_operator": "OR",
"fuzziness": "auto"
}
},
"filter": {
"bool": {
"should": [
{
"terms": {
"job.keyword": ["Architect","Politician","Tailor"]
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "job"
}
}
}
}
]
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.