FunctionScoreQuery.Builder not able to create

I'm trying to build the following query in elasticsearch-java library.

{
  "from": 0,
  "size": 10,
  "query": {
    "function_score": {
      "functions": [
        {
          "filter": {
            "term": {
              "cat": "val1"
            }
          },
          "weight": 40
        },
        {
          "filter": {
            "term": {
              "bat": "val2"
            }
          },
          "weight": 30
        }
      ],
      "query": {
        "bool": {
          "must": [....]
        }
      }
  }
}

I'm not able to create Function Cores. I tried following.

FunctionScoreQuery.Builder functionScoreBuilder = new FunctionScoreQuery.Builder();
functionScoreBuilder.functions(function -> {
   function.filter(filt-> {
      filt.term(term -> {
          term.field("cat").field("val1");
          return term;
      });
       return filt;
   });
    return function;  // COMPILATION ERROR - Type mismatch: cannot convert from FunctionScore.Builder to 
ObjectBuilder<FunctionScore>
});

POM

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.2</version>
</dependency>

<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>7.17.2</version>
</dependency>

Usually in the functions whatever the input could be output. But filter level, I see none of the functions returning desired object.

Hi @pasupathi-raja

Another way could be this:

FunctionScore functionScoreCat = new FunctionScore.Builder()
        .weight(40.0)
        .filter(Query.of(
            q -> q.term(
                TermQuery.of(
                    t -> t.field("cat").value("val1")
                )
            )
        )).build();

    FunctionScore functionScoreBat = new FunctionScore.Builder()
        .weight(30.0)
        .filter(Query.of(
            q -> q.term(
                TermQuery.of(
                    t -> t.field("bat").value("val1")
                )
            )
        )).build();

    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery.Builder()
        .query(queryMatchAll)
        .functions(List.of(functionScoreCat, functionScoreBat))
        .boostMode(FunctionBoostMode.Multiply)
        .scoreMode(FunctionScoreMode.Sum)
        .build();

    Query query = new Query.Builder().functionScore(functionScoreQuery).build();

The method build() from the type FunctionScore.Builder is not visible

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.