# Scoring script not applied unless I use deprecated filtered query

**URL:** <https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083>\
**Category:** Elasticsearch\
**Created:** [February 6, 2017, 3:28pm UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083 "2017-02-06T15:28:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 6, 2017, 3:28pm UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/1 "2017-02-06T15:28:58Z")

</div>

I'm using the Java API (v 2.3.1) to run a custom scoring script as part of a query.

However, I've noticed that the scoring script is not applied unless I use a deprecated part of the API.

ie: This:

```auto
BoolQueryBuilder bool = QueryBuilders.boolQuery().minimumNumberShouldMatch(6);
... add bool limiters here via bool.should(...) ...

Map<String, Object> params = new HashMap<>();
... add script parameters here ...

QueryBuilder query = QueryBuilders.functionScoreQuery(bool).setMinScore(12.0f).add(ScoreFunctionBuilders.scriptFunction(new Script("hamming_distance", ScriptService.ScriptType.INLINE, "native", params)))
...execute query here ...

```

Produces this query json:

```auto
{
  "function_score" : {
    "query" : {
      "bool" : {
        "should" : [...bool limiters here...],
        "minimum_should_match" : "6"
      }
    },
    "functions" : [ {
      "script_score" : {
        "script" : {
          "inline" : "hamming_distance",
          "lang" : "native",
          "params" : {
            ...script params here...
          }
        }
      }
    } ],
    "min_score" : 12.0
  }
}

```

and the scoring script is not applied.

Whereas this:

```auto
BoolQueryBuilder bool = QueryBuilders.boolQuery().minimumNumberShouldMatch(6);
... add bool limiters here via bool.should(...) ...

Map<String, Object> params = new HashMap<>();
... add script parameters here ...

QueryBuilder filter = QueryBuilders.filteredQuery(null, bool); // This is deprecated
QueryBuilder query = QueryBuilders.functionScoreQuery(filter).setMinScore(12.0f).add(ScoreFunctionBuilders.scriptFunction(new Script("hamming_distance", ScriptService.ScriptType.INLINE, "native", params)))
...execute query here ...

```

produces this query json:

```auto
{
  "function_score" : {
    "query" : {
      "filtered" : {
        "filter" : {
          "bool" : {
            "should" : [...bool limiters here...],
            "minimum_should_match" : "6"
          }
        }
      }
    },
    "functions" : [ {
      "script_score" : {
        "script" : {
          "inline" : "hamming_distance",
          "lang" : "native",
          "params" : {
            ...script params here...
          }
        }
      }
    } ],
    "min_score" : 12.0
  }
}

```

and the scoring script is correctly applied.

Am I doing this right?

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2017, 8:07am UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/2 "2017-02-07T08:07:53Z")

</div>

Hey,

have you tried to replace the `filtered` keyword with `bool` - that should work as well (sorry not tested, just a hunch after a quick view).

--Alex

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 7, 2017, 9:48am UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/3 "2017-02-07T09:48:02Z")

</div>

So I modified the code to replace filter with an outer bool and put the inner bool within a must, like so:

```auto
BoolQueryBuilder bool = QueryBuilders.boolQuery().minimumNumberShouldMatch(6);
... add bool limiters here via bool.should(...) ...

Map<String, Object> params = new HashMap<>();
... add script parameters here ...

QueryBuilder filter = QueryBuilders.boolQuery().must(bool);
QueryBuilder query = QueryBuilders.functionScoreQuery(filter).setMinScore(12.0f).add(ScoreFunctionBuilders.scriptFunction(new Script("hamming_distance", ScriptService.ScriptType.INLINE, "native", params)))
...execute query here ...

```

which produces the following query json:

```auto
{
  "function_score" : {
    "query" : {
      "bool" : {
        "must" : {
          "bool" : {
            "should" : [...bool limiters here...],
            "minimum_should_match" : "6"
          }
        }
      }
    },
    "functions" : [ {
      "script_score" : {
        "script" : {
          "inline" : "hamming_distance",
          "lang" : "native",
          "params" : {
            ..script params here...
          }
        }
      }
    } ],
    "min_score" : 12.0
  }
}

```

Alas, the scoring script is still not applied.

It appears as though an API option has been deprecated and the replacement functionality doesn't work in the same way.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2017, 9:53am UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/4 "2017-02-07T09:53:36Z")

</div>

Hey,

that was not what I meant, sorry. I meant

```auto
{
  "function_score" : {
    "query" : {
      "bool" : {
        "filter" : {
          "bool" : {
            "should" : [...bool limiters here...],
            "minimum_should_match" : "6"
          }
        }

```

Can you compile a full reproducible example? Including mapping, scripts, documents and query (best in console syntax so one can put it into the kibana console and reproduce easily).

--Alex

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 7, 2017, 10:16am UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/5 "2017-02-07T10:16:41Z")

</div>

Ahh ok! Sorry about that.

So I modified it to use bool filter, like so:

```auto
 ...
        QueryBuilder filter = QueryBuilders.boolQuery().filter(bool);
...

```

which produced query json:

```auto
{
  "function_score" : {
    "query" : {
      "bool" : {
        "filter" : {
          "bool" : {
            "should" : [...bool limiters here...],
            "minimum_should_match" : "6"
          }
        }
      }
    },
    "functions" : [ {
      "script_score" : {
        "script" : {
          "inline" : "hamming_distance",
          "lang" : "native",
          "params" : {
            ...script params here...
          }
        }
      }
    } ],
    "min_score" : 12.0
  }
}

```

Interestingly, it looks as though something else is not happening as with that version. It returns 0 hits, rather than the expected 1 hit.

I can try and compile the full test harness if that helps. To start with I just wanted to see if anyone knew what I was doing wrong, because the docs don't make it obvious.

---

<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:** [March 7, 2017, 10:16am UTC](https://discuss.elastic.co/t/scoring-script-not-applied-unless-i-use-deprecated-filtered-query/74083/6 "2017-03-07T10:16:48Z")

</div>

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