I am trying to migrate some elastic functionalty from elasticsearch 6.5.4 -> 8.1.1, specifically a functionScoreQuery:
private SearchRequest buildRequest(String query) {
SearchRequest searchRequest = new SearchRequest(INDEX_ALIAS);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.functionScoreQuery(
getQuery(),
getFilterFunctions())
);
searchRequest.source(sourceBuilder);
return searchRequest;
}
private FilterFunctionBuilder[] getFilterFunctions() {
return new FilterFunctionBuilder[] {
boostFunction(termQuery("Term1", "Value1").boost(2.0f)),
boostFunction(termQuery("Term2", "Value2").boost(1.5f)),
};
}
private FunctionScoreQueryBuilder.FilterFunctionBuilder boostFunction(QueryBuilder query{
return new FilterFunctionBuilder(query, weightFactorFunction(1.0f));
}
I cannot figure out how to do this with the new java api. I am trying something like this:
private SearchRequest buildRequest(String query, int numberOfResults) {
return new SearchRequest.Builder()
.index(INDEX_ALIAS)
.query(buildQuery(query))
.build();
}
private Query buildQuery(String queryWithoutDots) {
return new Query.Builder()
.functionScore(f -> f
.query(getQueries())
.functions(getFilterFunctions())
)
.build();
}
private List<FunctionScore> getFilterFunctions() {
return List.of(
new FunctionScore.Builder()
.filter(new Query.Builder()
.term(t ->
t.queryName("Term1")
.value("Value1")
.boost(2.0f))
.build())
**.build(),**
new FunctionScore.Builder()
.filter(new Query.Builder()
.term(t ->
t.queryName("Term2")
.value("Value2")
.boost(1.5f))
.build())
**.build(),**
}
But it is not possible to instantiate a FunctionScore as
'build()' has protected access in 'co.elastic.clients.elasticsearch._types.query_dsl.FunctionScore
Is this a bug? Is there any other approach i should consider?
CodePudding user response:
I figured it out. I need to use a modifier for instance: linear
new FunctionScore.Builder()
.linear(
l -> l.filter(
f -> f.term(
t -> t.queryName("Term1")
.value("Value1")
.boost(2.0f)
)
)
)
.build()