[英]Elasticsearch - How to filter data with nested fields
以下是一些帶有標簽作為嵌套字段的文檔。 是否可以過濾結果以獲取同時具有tags.id = 21
和tags.id = 22
的文檔?
{
"title": "Nokia",
"tags": [
{
"id": 21
},
{
"id": 22
}
]
}
{
"title": "HTC",
"tags": [
{
"id": 21
},
{
"id": 23
}
]
}
{
"title": "Samsung",
"tags": [
{
"id": 21
},
{
"id": 22
}
]
}
在這種情況下,結果應返回標題為諾基亞和三星的文檔
您可以使用布爾查詢:
GET my_index/_search
{
"query": {
"bool": {
"filter": [
{"match": {"tags.id": 21}},
{"match": {"tags.id": 22}}
]
}
}
}
以下是問題中提到的情況下的搜索查詢
{
"query": {
"bool": {
"filter": [
{
"nested":{
"path":"tags",
"query":{
"bool":{
"filter":[
{"term": {"tags.id": 21}}
]
}
}
}
},
{
"nested":{
"path":"tags",
"query":{
"bool":{
"filter":[
{"term": {"tags.id": 22}}
]
}
}
}
}
]
}
}
}
我所做的錯誤是將2個匹配塊放在同一過濾器塊下。
"filter":[
{"term": {"tags.id": 21}},
{"term": {"tags.id": 22}}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.