elasticsearch搜索嵌套类型中的数组字段

时间:2014-11-26 14:20:39

标签: elasticsearch

我正在尝试使用嵌套过滤器过滤我的结果,但结果不正确

这是我的地图信息

{
  "stock" : {
    "mappings" : {
      "clip" : {
        "properties" : {
          "description" : {
            "type" : "string"
          },
          "keywords" : {
            "type" : "nested",
            "properties" : {
              "category" : {
                "type" : "string"
              },
              "tags" : {
                "type" : "string",
                "index_name" : "tag"
              }
            }
          },
          "tags" : {
            "type" : "string",
            "index_name" : "tag"
          },
          "title" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

剪辑文档数据

{
      "_index" : "stock",
      "_type" : "clip",
      "_id" : "AUnsTOBBpafrKleQN284",
      "_score" : 1.0,
      "_source":{
   "title": "journey to forest",
   "description": "this clip contain information about the animals",
   "tags": ["birls", "wild", "animals", "roar", "forest"],
   "keywords": [
          {
             "tags": ["spring","summer","autumn"],
             "category": "Weather"
          },
          {
             "tags": ["Cloudy","Stormy"],
             "category": "Season"
          },
          {
             "tags": ["Exterior","Interior"],
             "category": "Setting"
          }
       ]
  }

我正在尝试过滤嵌套字段中的标记'关键字' 这是我的查询

{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
             "nested": {
                 "path": "keywords",
                     "filter": {
                         "bool": {
                             "must": [
                                 {
                                    "terms": { "tags": ["autumn", "summer"] }
                                 }
                         ]
                     }
                 }
             }

         }
      }
   }
}

我没有结果为什么? 我的查询或架构有什么问题请帮忙

1 个答案:

答案 0 :(得分:1)

上述查询在语法上是不正确的。您需要在术语查询中提供 标记 的完整路径 keywords.tags

{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
             "nested": {
                 "path": "keywords",
                     "filter": {
                         "bool": {
                             "must": [
                                 {
                                    "terms": { "keywords.tags": ["autumn", "summer"] }
                                 }
                         ]
                     }
                 }
             }

         }
      }
   }
}