elastic / elasticsearch

Free and Open Source, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
841 stars 24.8k forks source link

Make it easier to boost by field value #49761

Open jpountz opened 4 years ago

jpountz commented 4 years ago

I have seen a couple users wanting to boost documents based on the category that documents belong to end up with a query that looked like this:

POST _search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "body": "apache lucene"
        }
      },
      "functions": [
        {
          "filter": { "match": { "category": "books" } },
          "weight": 5
        },
        ...tens of similar functions
        {
          "filter": { "match": { "category": "food" } },
          "weight": 0.3
        }
      ],
      "score_mode": "first"
    }
  }
}

These queries have tens or hundreds of functions, for each category that they are trying to boost differently, and that need to be evaluated in order until one of them matches (which typically still requires to evaluate half the filters on average since values are usually exclusive). It is possible to do something that usually performs better with a script:

POST _search
{
  "query": {
    "script": {
      "query": {
        "match": {
          "body": "apache lucene"
        }
      },
      "script": {
        "params": {
          "books": 5,
         ...other boosts...
          "food": 0.3
        },
        "source": """if (doc["category"].size() != 0) { params.getOrDefault(doc["category"].value, 0) } else { 0 }"""
      }
    }
  }
}

But it is still far from being ideal. For instance scripts have to do the ordinal->value lookup here while what we'd really want would be to make this work on top of ordinals. This seems to be a common problem to me, can we add a new query that would help solve this problem?

elasticmachine commented 4 years ago

Pinging @elastic/es-search (:Search/Ranking)

elasticsearchmachine commented 4 months ago

Pinging @elastic/es-search (Team:Search)

elasticsearchmachine commented 3 months ago

Pinging @elastic/es-search-relevance (Team:Search Relevance)