我在 Elasticsearch 中查询时遇到问题。我正在搜索由 state_id 定义的一组特定数据,然后想要返回没有以下标识符定义的任何一个城市的所有州。
下面的查询返回 18 个仅包含“city_id_1”的结果,0 个包含“city_id_2”的结果。尽管如此,我返回 0 个结果(因为“city_id_2”位于每个州记录上)。我想做的仍然是返回 18 个结果,但查询两个城市。
我觉得我的查询应该有效,并且基本上执行 NOT(A 或 B)样式查询,相当于 NOT A 和 NOT B,但基本上 0 结果似乎覆盖了 18。
有没有办法可以更改我的查询以获得我想要的结果,或者这是elasticsearch无法做到的?
{
"query": {
"bool": {
"must": [
{ "terms": { "state_id": ["4ca16f80-da79-11e5-9874-64006a4f57cb"]}}
],
"must_not": [
{
"nested": {
"path": "cities",
"query": {
"bool": {
"should": [
{"term": { "cities.identifier": "city_id_1"}},
{"term": { "cities.identifier": "city_id_2"}}
]
}
}
}
}
]
}
},
"size": 10
}
最佳答案
试穿一下尺寸。 Elasticsearch 很愚蠢。过滤器需要位于每个嵌套查询中。
{
"query": {
"bool": {
"should": [
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "cities",
"query": {
"term": { "cities.identifier": "city_id_1"}
}
}
}
],
"filter":[
{
"term":{
"state_id":"4ca16f80-da79-11e5-9874-64006a4f57cb"
}
}
]
}
}
},
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "cities",
"query": {
"term": { "cities.identifier": "city_id_2"}
}
}
}
],
"filter":[
{
"term":{
"state_id":"4ca16f80-da79-11e5-9874-64006a4f57cb"
}
}
]
}
}
}
]
}
},
"size": 10
}
关于elasticsearch - Elastic Search - OR 查询不匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591245/