[英]Search Documents Containing Certain Nested Objects in Elasticsearch
我在 Elasticsearch 索引中的文檔具有以下格式:
{
timestamp: "123456789",
tags: [
{ key:"tag1", "value": "val1" }, ...
]
}
我想在tags
字段中獲取所有包含{ key:"tag1" }
和{ key:"tag2", "value": "val2" }
文檔。
我怎樣才能做到這一點?
您可以嘗試使用 bool 查詢,您可以在 must 部分指定需要多少嵌套查詢:
GET test_nested/test/_search
{
"query": {
"bool": {
"must": [
{"nested" : {
"path" : "tags",
"query" : {
"bool" : {
"must" : [
{ "match" : {"tags.key" : "tag1"} }
]
}
}
}},
{"nested" : {
"path" : "tags",
"query" : {
"bool" : {
"must" : [
{ "match" : {"tags.key" : "tag2"} },
{ "match" : {"tags.value" : "val2"} }
]
}
}
}}
]
}
}
}
在這種情況下,我有一個嵌套查詢,用於選擇帶有鍵“tag1”的所有文檔,第二個嵌套查詢用於選擇帶有“tag2”和“value2”的所有文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.