简体   繁体   中英

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

I would like to filter hits (with query_string) which have Product.Name = "name1" AND Product.Version = "v1"

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

You need to use Nested Mapping .

Nested type is different from object types, you will get difference in above link.

Query_String doesn't work on nested types, from the docs

Avoid using the query_string query for nested documentsedit query_string searches do not return nested documents. To search nested documents, use the nested query.

Mapping:

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

Query:

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
    }
  }
}

Result:

"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
                  }
                }
              ]
            }
          }
        }
      }
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM