查询:
GET service/_search
{
"query":{
"match": {"id":1}
}
}
此查询最终将从 Elasticsearch 服务器得到以下结果。我想根据子属性过滤搜索,特别是根据子类别。我已尝试以下查询,但徒劳无功,其中有什么问题? 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”字段不是嵌套
字段。您想要的行为可以通过将“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"
}
}
}
}
}
关于java - 基于嵌套属性的Elasticsearch查询过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357910/