import co.elastic.clients.elasticsearch._types.query_dsl.*;
*
*
BoolQuery.Builder mainQuery = QueryBuilders.bool();
*
*
*
// existing logic, which cannot be changed
*
*
BoolQuery.Builder subQuery = QueryBuilders.bool();
subQuery.should(QueryBuilders.wildcard().field("projectNumber").value('*' + projectNumber + '*').build()._toQuery());
subQuery.should(org.elasticsearch.index.query.QueryBuilders.termsQuery("projects", projectNumber).toQuery());
mainQuery.must(subQuery.build()._toQuery());
I have saved a document in the elastic search and it contains the field "projectNumber" and a list "projects". Both are strings and I want to select all the documents if the project number is equal to the field "projectNumber" or it is present in the list "projects". the above-mentioned code has an issue. the ".toQuery()" method in "termsQuery" gives compilation error, "Cannot access org.apache.lucene.search.Query". and if I remove the ".toQuery()" i will get "Cannot resolve method 'should(TermsQueryBuilder)'" error.
Can anyone help? if the above-mentioned TermsQuery declaration is not possible is it possible to declare the terms query like "TermsQuery.Builder termsQuery = QueryBuilders.terms();"?
I was able to write the TermQuery as follows,
TermsQueryField numberField = new TermsQueryField.Builder()
.value(List.of(projectNumber).stream().map(FieldValue::of).collect(Collectors.toList()))
.build();
subQuery.should(QueryBuilders.terms(term -> term.field("projects").terms(numberField)));