簡體   English   中英

ElasticSearch query_string:過濾數組成員包含2個字段值的元素

[英]ElasticSearch query_string : filter elements which array members contains 2 field values

我想過濾具有 Product.Name = "name1" AND Product.Version = "v1" 的命中(使用 query_string)

... 
"Product" : [
   {
      "Name" : ....,
      "Version" : ...,
      ...
   },
   {
      ...
   },
   etc ...
 ],
...

您需要使用嵌套映射

嵌套類型不同於 object 類型,您將在上面的鏈接中得到不同。

Query_String 不適用於嵌套類型,來自文檔

避免對嵌套文檔使用 query_string 查詢編輯 query_string 搜索不返回嵌套文檔。 要搜索嵌套文檔,請使用嵌套查詢。

映射:

PUT nestedindex
{
  "mappings": {
    "properties": {
     "Product":{
       "type":"nested",
       "properties": {
         "Name":{
           "type":"text",
           "fields":{
             "keyword":{
               "type":"keyword"
             }
           }
         },
         "Version":{
           "type":"integer"
         }
       }
     } 
    }
  }
}

詢問:

GET nestedindex/_search
{
  "query": {
    "nested": {
      "path": "Product",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "Product.Name.keyword": {
                  "value": "Prod1"
                }
              }
            },
            {
              "term": {
                "Product.Version": {
                  "value": 1
                }
              }
            }
          ]
        }
      },
      "inner_hits": {}  ---> If you need ti find matching nested document
    }
  }
}

結果:

"hits" : [
      {
        "_index" : "nestedindex",
        "_type" : "_doc",
        "_id" : "plQhjW0BywGFQhV7Zs6c",
        "_score" : 1.6931472,
        "_source" : {
          "Product" : [
            {
              "Name" : "Prod1",
              "Version" : 1
            },
            {
              "Name" : "Prod2",
              "Version" : 2
            }
          ]
        },
        "inner_hits" : {
          "Product" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.6931472,
              "hits" : [
                {
                  "_index" : "nestedindex",
                  "_type" : "_doc",
                  "_id" : "plQhjW0BywGFQhV7Zs6c",
                  "_nested" : {
                    "field" : "Product",
                    "offset" : 0
                  },
                  "_score" : 1.6931472,
                  "_source" : {
                    "Name" : "Prod1",
                    "Version" : 1
                  }
                }
              ]
            }
          }
        }
      }
    ]

暫無
暫無

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

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