Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

min_score not working in function_score query #10253

Closed
clintongormley opened this issue Mar 25, 2015 · 3 comments
Closed

min_score not working in function_score query #10253

clintongormley opened this issue Mar 25, 2015 · 3 comments
Assignees
Labels
>bug :Search/Search Search-related issues that do not fall into other categories v1.5.1 v1.6.0 v2.0.0-beta1

Comments

@clintongormley
Copy link

The new min_score functionality in the function_score query doesn't seem to to work as advertised. For instance:

DELETE t

PUT t
{
  "mappings": {
    "t": {
      "properties": {
        "name": {
          "type": "string"
        },
        "foo": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "string",
              "index": "not_analyzed"
            },
            "value": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

PUT /t/t/1
{
  "name": "test",
  "foo": [
    {
      "key": "bar",
      "value": 10
    },
    {
      "key": "bar",
      "value": 20
    },
    {
      "key": "bar",
      "value": 30
    },
    {
      "key": "bar",
      "value": 40
    }
  ]
}

This query returns a score of 4 for the above document, which means that it should be filtered out by the min_score of 10:

GET /_search
{
  "query": {
    "function_score": {
      "boost_mode": "replace",
      "min_score": 10,
      "query": {
        "nested": {
          "path": "foo",
          "score_mode": "sum",
          "query": {
            "constant_score": {
              "filter": {
                "match_all": {}
              }
            }
          }
        }
      }
    }
  }
}
@brwe
Copy link
Contributor

brwe commented Mar 25, 2015

This is a bug indeed and it seems to me for two reasons:

  1. when the score is 4 the document should not be returned
  2. the score should not be 4 in the first place. "boost_mode": "replace" should cause the score to be 1 (default if no functions and no query given).
    I'll work on a fix. For now the workaround would be to define a

"weight": 1,

if you really want to score all matches with 1 (which does not make much sense imo but should still work) or

 "script_score": {
        "script": "_score"
}, 

in case one actually wants the score of the query to be used.

@pickypg
Copy link
Member

pickypg commented Mar 27, 2015

To be clear, min_score as a child of function_score is new in 1.5.0.

min_score at the root document level works locally running 1.4.4.

@brwe
Copy link
Contributor

brwe commented Mar 30, 2015

Currently the way it works is this: If function_score encounters a query without a function, then the query is just executed as is without wrapping in a function score query (https://github.com/elastic/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryParser.java#L161) so min_score has no effect in this case. This was implemented so on purpose but it seems to me that now with the min_score there is a usecase where you would want to use function_score without a function. I'll change that.

brwe added a commit to brwe/elasticsearch that referenced this issue Mar 30, 2015
…ovided

For optimization pruposes a function score query with an empty function
will just result in the original sub query. However, sometimes one might
want to use function_score query to actually filter out docs within for example
bool clauses by using the min_score functionallity.
Therefore the sub query should only be used without wrapping inside
a function_score query if min_score was also not set.

closes elastic#10253
@brwe brwe closed this as completed in 567a50c Mar 31, 2015
brwe added a commit that referenced this issue Mar 31, 2015
…ovided

For optimization pruposes a function score query with an empty function
will just result in the original sub query. However, sometimes one might
want to use function_score query to actually filter out docs within for example
bool clauses by using the min_score functionallity.
Therefore the sub query should only be used without wrapping inside
a function_score query if min_score was also not set.

closes #10253
closes #10326
brwe added a commit that referenced this issue Mar 31, 2015
…ovided

For optimization pruposes a function score query with an empty function
will just result in the original sub query. However, sometimes one might
want to use function_score query to actually filter out docs within for example
bool clauses by using the min_score functionallity.
Therefore the sub query should only be used without wrapping inside
a function_score query if min_score was also not set.

closes #10253
closes #10326
mute pushed a commit to mute/elasticsearch that referenced this issue Jul 29, 2015
…ovided

For optimization pruposes a function score query with an empty function
will just result in the original sub query. However, sometimes one might
want to use function_score query to actually filter out docs within for example
bool clauses by using the min_score functionallity.
Therefore the sub query should only be used without wrapping inside
a function_score query if min_score was also not set.

closes elastic#10253
closes elastic#10326
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Query DSL labels Feb 14, 2018
fixmebot bot referenced this issue in VectorXz/elasticsearch Apr 22, 2021
fixmebot bot referenced this issue in VectorXz/elasticsearch May 28, 2021
fixmebot bot referenced this issue in VectorXz/elasticsearch Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories v1.5.1 v1.6.0 v2.0.0-beta1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants