Search code examples
elasticsearchrelevancebooleanquery

Min_score for a "must" in a bool query in Elasticsearch


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.


Solution

  • 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
              }
            }
          ]
        }
      }
    }