Elasticsearch 布尔查询必须匹配单个字段单个术语,另一个字段必须匹配 OR 术语



我想找到一个名称包含"Bob"并且位置位于"paducah"或"smyrna"中的文档。

这是我现在拥有的:

query: {
bool: {
must: [
{ match: { name: 'bob' } }, 
{ match: { location: ['paducah', 'smyrna'] } }
],
},
},

我知道问题出在位置数组中,因为如果我将其更改为没有数组的单个元素,查询就可以正常工作。

这是我能找到的最接近的答案。

它不起作用,我收到以下错误:

[term] 格式错误的查询,预期 [END_OBJECT] 但找到 [FIELD_NAME]

你可以试试这个查询:

{
"query": {
"bool": {
"must": [
{ "match": { "name": "bob" } }
],
"should": [
{ "match": { "location": "paducah" }},
{ "match": { "location": "smyrna"   }}
],
"minimum_should_match": 1
}
}
}

下面呢:

{
"query": {
"bool": {
"must": [
{ "term": { "name": "bob" }, 
"bool": {
"should": [
{"term": {"location": "paducah"}},
{"term": {"location": "smyrna"}}
]
}
}
]
}
}
}

最新更新