繁体   English   中英

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

[英]Elastic search query filtering based on nested properties

查询:

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

}

该查询将最终从弹性搜索服务器得到以下结果。 我想过滤基于子类别的基于子属性的搜索。 我已经尝试了以下查询,但是徒然有什么错呢? subCategories nod是数组列表,我的意思是jakson转换后的列表json转换中有什么错误吗?

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

子类映射; 没什么特别的,只是多对多映射如下

@Field(type= FieldType.Nested)
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "service_subcategory", joinColumns = @JoinColumn(name = "service_id") , inverseJoinColumns = @JoinColumn(name = "subcategory_id") )
private List<SubCategory> subCategories;

我很确定字段“ subCategories”不是nested字段。 您可以通过使“ subCategories”成为嵌套字段来实现所需的行为。 在此处阅读有关嵌套类型的信息,在此处了解如何查询嵌套字段的信息

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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