# FunctionScoreQuery.Builder not able to create

**URL:** <https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676>\
**Category:** Elastic Search\
**Created:** [June 20, 2022, 2:57pm UTC](https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676 "2022-06-20T14:57:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pasupathi-raja](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@pasupathi-raja](https://discuss.elastic.co/u/pasupathi-raja)\
**Post date:** [June 20, 2022, 2:57pm UTC](https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676/1 "2022-06-20T14:57:44Z")

</div>

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

```auto
{
  "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.

```auto
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

```auto
<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.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 20, 2022, 4:24pm UTC](https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676/2 "2022-06-20T16:24:04Z")

</div>

Hi @pasupathi-raja

Another way could be this:

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![pasupathi-raja](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@pasupathi-raja](https://discuss.elastic.co/u/pasupathi-raja)\
**Post date:** [June 20, 2022, 4:44pm UTC](https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676/3 "2022-06-20T16:44:46Z")

</div>

> [@RabBit\_BR](#):
>
> ```auto
> FunctionScore functionScoreCat = new FunctionScore.Builder()
> .weight(40.0)
> .filter(Query.of(
> q -> q.term(
> TermQuery.of(
> t -> t.field("cat").value("val1")
> )
> )
> )).build();
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 18, 2022, 4:45pm UTC](https://discuss.elastic.co/t/functionscorequery-builder-not-able-to-create/307676/4 "2022-07-18T16:45:18Z")

</div>

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