gpt4 book ai didi

spring-boot - 如何在Spring Data ElasticSearch中对嵌套对象使用术语查询?

转载 作者:行者123 更新时间:2023-12-02 22:26:54 28 4
gpt4 key购买 nike

我的文档就像:

class Foo{
private Integer idDl;
private String Name;
private String Add;
@Field(type = FieldType.Nested)
private List< Bar> Bar;
}

class Bar{
private String barId;
private List<String> barData
}
Foo样本响应数据如下:
{
"idDl": 123,
"Name": "ABCD",
"Add": "FL",
"Bar": [
{
"barId": "A456B",
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": "A985D",
"barData": [
"Bar4",
"Bar5"
]
}
]
}
我想返回 Foo匹配的所有 Bar.barId对象。我正在使用spring-data-elasticsearch提供的 NativeSearchQueryBuilder:
String[] includeFields = new String[]{"idDl", "Name"};
String[] excludeFields = new String[]{"Add"}; // to exclude Add field of Foo
Query searchQuery = new NativeSearchQueryBuilder()
.withQuery(termQuery("Bar.barId", "A456B"))
.withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
.build();
return elasticsearchRestTemplate.queryForList( searchQuery, Foo.class);
我们还尝试了使用 nestedQuery,如下所示:
 SearchQuery  searchQuery = new NativeSearchQueryBuilder()
.withQuery(nestedQuery("Bar",
boolQuery().must(termQuery("Bar.barId", "A456B")), ScoreMode.Max))
.withIndices(indices)
.withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
.build();
return elasticsearchRestTemplate.queryForList(searchQuery, Foo.class);
但得到异常为:
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177) ~[elasticsearch-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2053) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:2030) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1777) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1734) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1696) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1092) ~[elasticsearch-rest-high-level-client-6.8.7.jar:6.8.7]
我在第一个代码段中使用 termQuery,但是我没有得到响应,但是如果我使用 matchQuery("Bar.barId", "A456B"),我得到了响应。我们只想使用 termQuerymatchQuery检查查询性能。如何使用 termQuery提取数据?
附言:我们在 spring-boot-starter-data-elasticsearch项目中使用 spring-boot 2.2.6.RELEASE。

最佳答案

我们有类似的要求,并使用此代码段解决了该问题,根据您的要求,我已尝试对其进行隐蔽。代码非常简单,如果需要进一步说明,请告诉我。

    BoolQueryBuilder boolQueryBuilder = boolQuery();
BoolQueryBuilder nestedBoolQueryBuilder = boolQuery().must(boolQuery()
.should(termQuery("Bar.barId", barId.toLowerCase()))).minimumNumberShouldMatch(1);
QueryBuilder nestedQueryBuilder = nestedQuery("Bar", nestedBoolQueryBuilder);
boolQueryBuilder = boolQueryBuilder.must(nestedQueryBuilder);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.withPageable(pageable)
.build();

关于spring-boot - 如何在Spring Data ElasticSearch中对嵌套对象使用术语查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62952978/

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