Search code examples
elasticsearchelasticsearch-2.0

How to boost individual documents


I have a pretty complex query and now I want to boost some documents that fulfill some criteria. I have the following simplified document structure and I try to give some documents a boost based on the id, genre, tag.

{
  "id": 123,
  "genres": ["ACTION", "DRAMA"],
  "tags": ["For kids", "Romantic", "Nature"]
}

What I want to do is for example

id: 123 boost: 5
genres: ACTION boost: 3
tags: Romantic boost: 0.2

and boost all documents that are contained in my query and fit the criteria but I don't want to filter them out. So query clause boosting is not of any help I guess.

Edit: To make if easier to understand what I want to achieve (not sure if it is possible with elasticsearch, no is also a valid answer).

I want to search with a query and get a result set. In this set I want to boost some documents. But I don't want to enlarge the result set or filter it. The boost should be independent from the query.

For example I search for a specific tag and want to boost all documents with category 'ACTION' in the result set. But I don't want all documents with category 'ACTION' in the result set and also I don't want only documents with the specific tag AND category 'ACTION'.


Solution

  • I found a solution and it was pretty simple. I use a boosting query. I now just nest the different boosting criteria with and my original query is now the base query.

    https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-boosting-query.html

    For example:

    {
      "query": {
        "boosting": {
          "positive": {
            "boosting": {
              "positive": {
                "match": {
                  "director": "Spielberg"
                }
              },
              "negative": {
                "term": {
                  "genres": "DRAMA"
                }
              },
              "negative_boost": 1.3
            }
          },
          "negative": {
            "term": {
              "tags": "Romantic"
            }
          },
          "negative_boost": 1.2
        }
      }
    }