首页 文章

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

提问于
浏览
0

查询:

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

}

此查询将以弹性搜索服务器的以下结果结束 . 我想根据subCategories特定的子属性过滤搜索 . 我已经尝试了以下查询但是徒劳无益的是什么? subCategories nod是一个数组列表我的意思是jakson转换List在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;

1 回答

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

相关问题