基于嵌套属性的弹性搜索查询过滤

时间:2016-02-12 08:28:01

标签: java json elasticsearch

查询:

.ajax

此查询将以弹性搜索服务器的以下结果结束。我想根据subCategories特定的子属性过滤搜索。我已经尝试了以下查询但是徒劳无益的是什么? subCategories nod是一个数组列表我的意思是jakson转换List在json转换中有什么不对吗?

GET service/_search
{
  "query":{ 
"match": {"id":1}
  }

}

子类别映射;没有什么花哨的东西只是一个很多很多的映射如下

GET service/_search
{
  "query": 
{ 
"match": {
  "subCategories.name": "subname1"
}
}
}





{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "service",
            "_type": "service",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 1,
               "title": "title",
               "searchTerms": null,
               "description": "description",
               "picUrl": "/imgurl",
               "price": 65000,
               "discount": 10,
               "topservice": true,
               "place": "100,200",
               "status": null,
               "subCategories": [
                  {
                     "id": 1,
                     "name": "subname1",
                     "subCategoryGroup": {
                        "id": 1,
                        "name": "Engineering",
                        "category": {
                           "id": 1,
                           "name": "Education"
                        }
                     }
                  },
                  {
                     "id": 2,
                     "name": "subname2",
                     "subCategoryGroup": {
                        "id": 1,
                        "name": "Engineering",
                        "category": {
                           "id": 1,
                           "name": "Education"
                        }
                     }
                  },
                  {
                     "id": 3,
                     "name": "subname3",
                     "subCategoryGroup": {
                        "id": 1,
                        "name": "Engineering",
                        "category": {
                           "id": 1,
                           "name": "Education"
                        }
                     }
                  },


               ],
               "deleted": false
            }
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:1)

我很确定字段“subCategories”不是nested字段。您可以通过将“subCategories”设置为嵌套字段来实现您想要的行为。阅读嵌套类型here以及如何查询嵌套字段here

基本上您的映射定义应如下所示:

{
  "mappings": {
    "<mapping_name>": {
      "properties": {
        ...                   <-- Other fields like id, title, etc go here
        "subCategories": {
          "type": "nested",   <-- This is important. This is missing in your current mapping definition
          "properties": {
            "id": {
              "type":"integer"
            },
            "name": {
              "type":"string"
            },
            ...               <-- Definition of subCategoryGroup goes here
          }
        }
      }
    }
  }
}

你的查询应该是这样的。

{
  "query": {
    "nested": {
      "path": "subCategories",
      "query": {
        "match": {
          "subCategories.name": "subname1"
        }
      }
    }
  }
}