admin管理员组文章数量:1200421
I'm using Elasticsearch filters and I would like to apply a must condition for selected regions (e.g., Occitanie and Île-de-France). Additionally, I want to ensure that profiles with the region "Toute la France" are also included in the search results, even if they are not part of the selected regions.
Currently, I am doing the following:
if (regions != null && !regions.isEmpty()) {
BoolQuery.Builder finalQuery = new BoolQuery.Builder();
for (String regionName : regions) {
finalQuery.must(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query(regionName)
)))
)));
}
finalQuery.should(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query("Toute la France")
.boost(4.0f)
)))
)));
finalQuery.minimumShouldMatch("1");
boolQuery.filter(Query.of(q -> q.bool(finalQuery.build())));
}
However, this query only returns profiles that match the selected regions (must), but it does not include profiles with "Toute la France."
How can I modify the query to include profiles with "Toute la France" along with the selected regions?
Thanks in advance for your help!
I'm using Elasticsearch filters and I would like to apply a must condition for selected regions (e.g., Occitanie and Île-de-France). Additionally, I want to ensure that profiles with the region "Toute la France" are also included in the search results, even if they are not part of the selected regions.
Currently, I am doing the following:
if (regions != null && !regions.isEmpty()) {
BoolQuery.Builder finalQuery = new BoolQuery.Builder();
for (String regionName : regions) {
finalQuery.must(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query(regionName)
)))
)));
}
finalQuery.should(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query("Toute la France")
.boost(4.0f)
)))
)));
finalQuery.minimumShouldMatch("1");
boolQuery.filter(Query.of(q -> q.bool(finalQuery.build())));
}
However, this query only returns profiles that match the selected regions (must), but it does not include profiles with "Toute la France."
How can I modify the query to include profiles with "Toute la France" along with the selected regions?
Thanks in advance for your help!
Share Improve this question asked Jan 21 at 17:25 Raouf ZouaouiRaouf Zouaoui 11 bronze badge1 Answer
Reset to default 0You need to generate a query using two bool queries:
if (regions != null && !regions.isEmpty()) {
BoolQuery.Builder regionsQuery = new BoolQuery.Builder();
for (String regionName : regions) {
regionsQuery.must(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query(regionName)
)))
)));
}
BoolQuery.Builder touteLaFranceQuery = new BoolQuery.Builder();
touteLaFranceQuery.must(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query("Toute la France")
)))
)));
BoolQuery.Builder finalQuery = new BoolQuery.Builder();
finalQuery.should(Query.of(q -> q.bool(regionsQuery.build()))
.should(Query.of(q -> q.bool(touteLaFranceQuery.build()));
finalQuery.minimumShouldMatch(1);
}
Please note that there might be some syntactical issues. I hope you get the idea.
本文标签: How to combine must and should for the same field in filter Elasticsearch Spring BootStack Overflow
版权声明:本文标题:How to combine must and should for the same field in filter Elasticsearch Spring Boot? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738611063a2102633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论