簡體   English   中英

如何在elasticsearch中使用AND運算符查詢嵌套字段?

[英]How to use AND operator in elasticsearch to query nested fields?

我在elasticsearch中有2個文檔,結構如下:

文件1:

{
  "specification": [
                    {
                     "name": "Processor",
                     "value": "Intel"
                    },
                    {
                     "name": "RAM",
                     "value": "2GB"
                    }
                   ]
}

Document 2:
{
  "specification": [
                    {
                     "name": "Processor",
                     "value": "Intel"
                    },
                    {
                     "name": "RAM",
                     "value": "3GB"
                    }
                   ]
}

我想獲得具有 intel 值和 2GB (ie) 1st document 的規范的文檔。 但是當我嘗試使用 must (AND 運算符)時,我什么也沒得到。 如果我使用 should(OR 運算符),我將獲得兩個文件。 誰可以幫我這個事? 下面是我的查詢..

{
  "query": {
    "nested": {
      "path": "specification",
      "query": {
        "bool": {
          "must": [

                {
                    "bool": {
                        "must": [
                            { "match": { "specification.name": "Processor" }},
                            { "match": { "specifications.value": "Intel" }}
                        ]
                    }
                },
                {
                    "bool": {
                        "must": [
                            { "match": { "specification.name": "RAM" }},
                            { "match": { "specifications.value": "2GB" }}
                        ]
                    }
                }
              ]
        }
      }
    }
  }
}

試試這個:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "specification",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "specification.name": "Processor"
                    }
                  },
                  {
                    "match": {
                      "specification.value": "Intel"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "specification",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "specification.name": "RAM"
                    }
                  },
                  {
                    "match": {
                      "specification.value": "2GB"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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