gpt4 book ai didi

elasticsearch - Spring Data Elasticsearch嵌套字段多匹配查询

转载 作者:行者123 更新时间:2023-11-29 02:56:03 24 4
gpt4 key购买 nike

我将 elasticsearch 与 spring-data-elastic 结合使用。并尝试使用多重搜索。问题是在使用类字段搜索 wprking 时,它不适用于嵌套字段。我的映射如下所示

{
"archieve": {
"mappings": {
"author": {
"properties": {
"books": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string",
"analyzer": "standard"
}
}
},
"id": {
"type": "long"
},
"firstName": {
"type": "string",
"analyzer": "standard"
},
"lastName": {
"type": "string",
"analyzer": "standard"
}
}
}
}
}
}

我有一个带有 searchQuery 的端点,例如:

  @GetMapping(value = "/es/archieve/multi/{keyword}")
public Page<Author> getBrandMulti(@PathVariable String keyword, Pageable pageable) {

SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.multiMatchQuery(keyword)
.field("firstName", 1.2f)
.field("books.name", 1.1f)
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
.fuzziness(Fuzziness.TWO)
)
.withIndices("archieve")
.withTypes("author")
.withPageable(pageable)
.build();

return elasticsearchTemplate.queryForPage(searchQuery, Author.class);
}

问题是查询不适用于嵌套字段。有什么建议,问候?

更新

实际上嵌套对象可以查询为

NativeSearchQueryBuilder()
.withQuery(QueryBuilders.nestedQuery("books",
QueryBuilders.termQuery("books.name", searchKey)))

有没有办法像这样连接两个查询

NativeSearchQueryBuilder()
.withQuery(Query1)
.withQuery(Query1)
.build();

最佳答案

ES 嵌套对象

作为the document说,当你查询嵌套字段时,你必须使用嵌套查询:

Because nested objects are indexed as separate hidden documents, we can’t query them directly. Instead, we have to use the nested query to access them:

Spring 数据

回到 spring 数据,我更喜欢使用 Query 的方式,IMO,更具可读性:

@Query(" {" +
" \"bool\": {\n" +
" \"should\": [\n" +
" {\n" +
" \"multi_match\": {\n" +
" \"query\": \"?0\",\n" +
" \"fields\": [\"firstName\"]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"nested\": {\n" +
" \"path\": \"books\",\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"books.name\": \"?0\"\n" +
" }}\n" +
" }\n" +
" } ]\n" +
" }" +
"}")
Page<EsBrand> findByNameOrBooks(String info, Pageable pageable);

您可以将此签名放在您的 repo 接口(interface)中,spring 将实现一个代理来完成其他工作。

关于elasticsearch - Spring Data Elasticsearch嵌套字段多匹配查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com