I want to filter a list of employees based on programming language skills like C, C++, Java etc. I am using Elasticsearch DSL in Java to search based onall of the terms.termsQuery
returns data matchingany of the terms: means if at least one terms matches, it selects the data
I tried to the following code to set minimum_should_match
to tags.length
to match all given tags as "ANDoperator" to filter data but failed.
QueryBuilder query = QueryBuilders
.boolQuery()
.must(
QueryBuilders
.termsQuery("tags",tags)
)
.minimumShouldMatch(tags.length);
I also tried to use TermsSetQueryBuilder
to check list of terms but it throws exception : minimum_should_match_field
not set
QueryBuilder query =
QueryBuilders
.boolQuery()
.should(
new TermsSetQueryBuilder("tags", tags)
)
.minimumShouldMatch(tags.size());
Also, tried to set minimum_should_match_field
in TermsSetQuery
, but it only acceptsString, not numeric value or percentage as mention here. Tried to set like minimum_should_match_field
= "2" minimum_should_match_field
= "100%" even tried to setMinimumShouldMatchScript
. Not working.
QueryBuilder query =
QueryBuilders
.boolQuery()
.should(
new TermsSetQueryBuilder("tags", tags)
.setMinimumShouldMatchField(tags.size())
)
.minimumShouldMatch(tags.size());
How can I filter for a field("tags") based on several terms("tags": ["JAVA", "C"]) matching on all of them?
UPDATEDMy code looks like the following:
public List<Employee> getEmployeesByFilters(List<String> terms) {
int required_matches = terms.size();
QueryBuilder query =
QueryBuilders
.boolQuery()
.must(
new TermsSetQueryBuilder(
"filters", terms)
.setMinimumShouldMatchField("required_matches")
);
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.build();
List<Employee> employees = elasticsearchRestTemplate
.search(nativeSearchQuery, Employee.class)
.stream().map(SearchHit -> SearchHit.getContent())
.collect(Collectors.toList());
return employees;
}
2条答案
按热度按时间sauutmhj1#
您可以简单地使用布尔查询来获得预期的结果:
术语集查询不起作用,因为您需要添加一个类似于
required_matches
的字段,并设置在对文档编制索引时返回文档所需的匹配术语数。因此,您的索引文档将如下所示:
您的查询将如下所示:
我希望您能够根据这个查询创建Java代码。
更新日期:
更新2:
您可以使用
minimum_should_match_script
并提供params.num_terms
作为脚本,该脚本将计算您在查询中给定的术语数,并匹配所有术语并返回结果。ElasticSearch查询:
Java程式码:
2ic8powd2#
作为参考,我的Java代码(工作版本)如下所示: