繁体   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