gpt4 book ai didi

javascript - Elasticsearch 中必须查询(AND)内的嵌套应该查询(OR)

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:33 25 4
gpt4 key购买 nike

我有以下格式的 Elasticsearch 数据:

{
"is_cricketer": 1,
"name": "Abraham",
"cities": [
{ "name": "stellenbosch" },
{ "name": "Nelspruit" },
{ "name": "East London" }
]
},
{
"is_cricketer": 1,
"name": "Abraham",
"cities": [
{ "name": "Rustenburg" },
{ "name": "Nelspruit" },
{ "name": "East London" }
]
},
{
"is_cricketer": 0,
"name": "deVilliers",
"cities": [
{ "name": "Cape town" },
{ "name": "Nelspruit" },
{ "name": "East London" }
]
}

我需要查询 Elasticsearch 以获取所有具有 is_cricketer = 1 的配置文件,并在 cities.name 字段上进行 OR 查询和 name 字段。即

( profile.is_cricketer == 1 && (profile.name == 'Abraham' || profile.cities[i].name == 'Nelspruit' ))

要在字段 cities.namename 字段上使用 OR 查询来获取配置文件以匹配查询字符串,如下所示,它按预期工作:

"should": [
{
"nested": {
"path": "cities",
"query": {
"multi_match": {
"query": "Nelspruit",
"fields": [
"cities.name"
]
}
}
}
},
{
"multi_match": {
"query": "Abraham",
"fields": [
"name"
]
}
}
]

获取字段 is_cricketer = 1 的所有配置文件的 Must 查询如下:

{
"must": {
"match": {
"is_cricketer": "1"
}
}
}

以上两个查询都工作正常,我正在尝试按如下方式组合两个查询:

{
"query": {
"bool": {
"must": {
"match": {
"is_cricketer": "1"
}
},
"should": [
{
"nested": {
"path": "cities",
"query": {
"multi_match": {
"query": "Nelspruit",
"fields": [
"cities.name"
]
}
}
}
},
{
"multi_match": {
"query": "Abraham",
"fields": [
"name"
]
}
}
]
}
}
}

它没有返回预期的结果,它返回所有带有 is_cricketer = 1 的配置文件,而不过滤 namecities.name

我还尝试在 must 查询中包含 should 查询,如下所示:

{
"query": {
"bool": {
"must": [{
"match": {
"is_cricketer": "1"
}
}, {
"should": [
{
"nested": {
"path": "cities",
"query": {
"multi_match": {
"query": "Nelspruit",
"fields": [
"cities.name"
]
}
}
}
},
{
"multi_match": {
"query": "Abraham",
"fields": [
"name"
]
}
}
]
}]
}
}
}

但是我在上面的查询中得到了以下错误:

"Error: [parsing_exception] [should] query malformed, no start_object after query name, with { line=1 & col=64 } at respond (/GitRepo/project/node_modules/elasticsearch/src/lib/transport.js:307:15) at checkRespForFailure (/GitRepo/project/node_modules/elasticsearch/src/lib/transport.js:266:7) at HttpConnector. (/GitRepo/project/node_modules/elasticsearch/src/lib/connectors/http.js:159:7) at IncomingMessage.bound (/GitRepo/project/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1056:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)"

如何组合这两个查询以获得所需的结果。任何帮助将不胜感激。

最佳答案

如果你想在 must 中使用 should 查询,你可以按以下方式使用它

{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
... your query here
}
]
}
}
]
}
}
}

关于javascript - Elasticsearch 中必须查询(AND)内的嵌套应该查询(OR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49752913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com