2

Is there a way to apply a minimum score to a must clause in a bool query in elasticsearch.

I want to be able to do something like this:

{
  "query": {
     "bool": {
        "must": [
            {
              "match": {
                 "name": {
                    "query":"A Name",
                    "min_score": 0.3
                 }
               }
            },
            {
              "match": {
                 "address": {
                    "query":"1 Somewhere Street, Somewhereset, UK",
                    "min_score": 0.3
                 }
               }
            }
        ]
    }
  }
}

Which would require the name query to match with a score > 0.3 and the address query to match with a score > 0.3 for the document to be returned. This is to stop really good name matches being returned despite having a terrible address match (e.g. just matching the 1) and vice versa.

I'm currently using Elasticsearch 1.5, but I've also wanted this in the past for 2.3.

kiml42
  • 638
  • 2
  • 11
  • 26
  • 1
    Specifying the version of Elastic can help a lot with some questions - there are may differences between v1 and v2. – harvzor Jul 21 '16 at 12:05

2 Answers2

3

Try this and let me know if it works:

{
  "query": {
    "bool": {
      "must": [
        {
          "function_score": {
            "query": {
              "match": {
                "name": {
                  "query": "A Name"
                }
              }
            },
            "min_score": 0.3
          }
        },
        {
          "function_score": {
            "query": {
              "match": {
                "address": {
                  "query": "1 Somewhere Street, Somewhereset, UK"
                }
              }
            },
            "min_score": 0.3
          }
        }
      ]
    }
  }
}
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • That works great. I had thought of using a function score query, but I was thinking I'd need to use the function to make the score go so low it would not get past the overall min score. Also, I was expecting it to be too slow to be practical, but it doesn't seem any slower at all, as it's not actually using a function. Thanks. – kiml42 Jul 22 '16 at 09:32
0

It seems that the score of a query is calculated from all of the fields combined. You can change how the score is calculated using the script_score in a function query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score

However, it seems unlikely that you can access the score of each field result individually.

harvzor
  • 2,832
  • 1
  • 22
  • 40