Boosting query

If you’re searching for the word “pitcher”, your results may relate to either baseball players or containers for liquids. For a search in the context of baseball, you might want to completely exclude results that contain the words “glass” or “water” by using the must_not clause. However, if you want to keep those results but downgrade them in relevance, you can do so with boosting queries.

A boosting query returns documents that match a positive query. Among those documents, the ones that also match the negative query are scored lower in relevance (their relevance score is multiplied by the negative boosting factor).

Example

Consider an index with two documents that you index as follows:

  1. PUT testindex/_doc/1
  2. {
  3. "article_name": "The greatest pitcher in baseball history"
  4. }
  1. PUT testindex/_doc/2
  2. {
  3. "article_name": "The making of a glass pitcher"
  4. }

Use the following match query to search for documents containing the word “pitcher”:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "match": {
  5. "article_name": "pitcher"
  6. }
  7. }
  8. }

Both returned documents have the same relevance score:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.18232156,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0.18232156,
  21. "_source": {
  22. "article_name": "The greatest pitcher in baseball history"
  23. }
  24. },
  25. {
  26. "_index": "testindex",
  27. "_id": "2",
  28. "_score": 0.18232156,
  29. "_source": {
  30. "article_name": "The making of a glass pitcher"
  31. }
  32. }
  33. ]
  34. }
  35. }

Now use the following boosting query to search for documents containing the word “pitcher” but downgrade the documents that contain the words “glass”, “crystal”, or “water”:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "boosting": {
  5. "positive": {
  6. "match": {
  7. "article_name": "pitcher"
  8. }
  9. },
  10. "negative": {
  11. "match": {
  12. "article_name": "glass crystal water"
  13. }
  14. },
  15. "negative_boost": 0.1
  16. }
  17. }
  18. }

copy

Both documents are still returned, but the document with the word “glass” has a relevance score that is 10 times lower than in the previous case:

  1. {
  2. "took": 13,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.18232156,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0.18232156,
  21. "_source": {
  22. "article_name": "The greatest pitcher in baseball history"
  23. }
  24. },
  25. {
  26. "_index": "testindex",
  27. "_id": "2",
  28. "_score": 0.018232157,
  29. "_source": {
  30. "article_name": "The making of a glass pitcher"
  31. }
  32. }
  33. ]
  34. }
  35. }

Parameters

The following table lists all top-level parameters supported by boosting queries.

ParameterDescription
positiveThe query that a document must match to be returned in the results. Required.
negativeIf a document in the results matches this query, its relevance score is reduced by multiplying its original relevance score (produced by the positive query) by the negative_boost parameter. Required.
negative_boostA floating-point factor between 0 and 1.0 that the original relevance score is multiplied by in order to reduce the relevance of documents that match the negative query. Required.