簡體   English   中英

Elasticsearch-如何使用嵌套字段過濾數據

[英]Elasticsearch - How to filter data with nested fields

以下是一些帶有標簽作為嵌套字段的文檔。 是否可以過濾結果以獲取同時具有tags.id = 21tags.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM