Spring Data Elasticsearch嵌套字段多匹配查询
Spring Data Elasticsearch nested field multimatch query
我将 elasticsearch 与 spring-data-elastic 一起使用。并尝试使用多重搜索。问题是在使用 class 字段搜索 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 接口中,spring 将实现一个代理来完成其他工作。
我将 elasticsearch 与 spring-data-elastic 一起使用。并尝试使用多重搜索。问题是在使用 class 字段搜索 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 接口中,spring 将实现一个代理来完成其他工作。