如何在弹性搜索中结合过滤器编写嵌套查询



我需要执行一个嵌套查询,该查询尝试在对象数组中进行搜索。除此之外,我还有其他参数需要搜索。我正在使用过滤器。如何有效地组合过滤器和嵌套查询?对不起,我不是ES的专家。

以下是我试图在Kibana:中执行的查询

GET test-index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"isPublic": true
}
},
{
"term": {
"isDeleted": false
}
}
]
},
"nested": {
"path": "data.location.countries",
"query": {
"bool": {
"must": [
{
"match": {
"data.location.countries.name": "United States"
}
},
{
"range": {
"data.location.countries.weight": {
"gt": 30
}
}
}
]
}
}
}
},
"size": "60",
"from": 0,
"sort": [
{
"followers": {
"order": "desc"
}
}
]
}

返回时出现错误:

{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 17,
"col": 5
}
],
"type": "parsing_exception",
"reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 17,
"col": 5
},
"status": 400
}

有人能对此有所了解吗?

在bool.filter中移动嵌套查询:

{
"query": {
"bool": {
"filter": [
{
"term": {
"isPublic": true
}
},
{
"term": {
"isDeleted": false
}
},
{
"nested": {
"path": "data.location.countries",
"query": {
"bool": {
"must": [
{
"match": {
"data.location.countries.name": "United States"
}
},
{
"range": {
"data.location.countries.weight": {
"gt": 30
}
}
}
]
}
}
}
}
]
}
},
"size": "60",
"from": 0,
"sort": [
{
"followers": {
"order": "desc"
}
}
]
}

在复合查询afaik之外,不能使用超过1个查询。

最新更新