|
| 1 | +.. _ref-boost: |
| 2 | + |
| 3 | +===== |
| 4 | +Boost |
| 5 | +===== |
| 6 | + |
| 7 | + |
| 8 | +Scoring is a critical component of good search. Normal full-text searches |
| 9 | +automatically score a document based on how well it matches the query provided. |
| 10 | +However, sometimes you want certain documents to score better than they |
| 11 | +otherwise would. Boosting is a way to achieve this. There are three types of |
| 12 | +boost: |
| 13 | + |
| 14 | +* Term Boost |
| 15 | +* Document Boost |
| 16 | +* Field Boost |
| 17 | + |
| 18 | +.. note:: |
| 19 | + |
| 20 | + Document & Field boost support was added in Haystack 1.1. |
| 21 | + |
| 22 | +Despite all being types of boost, they take place at different times and have |
| 23 | +slightly different effects on scoring. |
| 24 | + |
| 25 | +Term boost happens at query time (when the search query is run) and is based |
| 26 | +around increasing the score is a certain word/phrase is seen. |
| 27 | + |
| 28 | +On the other hand, document & field boosts take place at indexing time (when |
| 29 | +the document is being added to the index). Document boost causes the relevance |
| 30 | +of the entire result to go up, where field boost causes only searches within |
| 31 | +that field to do better. |
| 32 | + |
| 33 | +.. warning: |
| 34 | +
|
| 35 | + Be warned that boost is very, very sensitive & can hurt overall search |
| 36 | + quality if over-zealously applied. Even very small adjustments can affect |
| 37 | + relevance in a big way. |
| 38 | +
|
| 39 | +Term Boost |
| 40 | +========== |
| 41 | + |
| 42 | +Term boosting is achieved by using ``SearchQuerySet.boost``. You provide it |
| 43 | +the term you want to boost on & a floating point value (based around ``1.0`` |
| 44 | +as 100% - no boost). |
| 45 | + |
| 46 | +Example:: |
| 47 | + |
| 48 | + # Slight increase in relevance for documents that include "banana". |
| 49 | + sqs = SearchQuerySet().boost('banana', 1.1) |
| 50 | + |
| 51 | + # Big decrease in relevance for documents that include "blueberry". |
| 52 | + sqs = SearchQuerySet().boost('blueberry', 0.8) |
| 53 | + |
| 54 | +See the :doc:`searchqueryset_api` docs for more details on using this method. |
| 55 | + |
| 56 | + |
| 57 | +Document Boost |
| 58 | +============== |
| 59 | + |
| 60 | +Document boosting is done by adding a ``boost`` field to the prepared data |
| 61 | +``SearchIndex`` creates. The best way to do this is to override |
| 62 | +``SearchIndex.prepare``:: |
| 63 | + |
| 64 | + from haystack import indexes |
| 65 | + from notes.models import Note |
| 66 | + |
| 67 | + |
| 68 | + class NoteSearchIndex(indexes.SearchIndex): |
| 69 | + # Your regular fields here then... |
| 70 | + |
| 71 | + def prepare(self, obj): |
| 72 | + data = super(NoteSearchIndex, self).prepare(obj) |
| 73 | + data['boost'] = 1.1 |
| 74 | + return data |
| 75 | + |
| 76 | + |
| 77 | +Another approach might be to add a new field called ``boost``. However, this |
| 78 | +can skew your schema and is not encouraged. |
| 79 | + |
| 80 | + |
| 81 | +Field Boost |
| 82 | +=========== |
| 83 | + |
| 84 | +Field boosting is enabled by setting the ``boost`` kwarg on the desired field. |
| 85 | +An example of this might be increasing the significance of a ``title``:: |
| 86 | + |
| 87 | + from haystack import indexes |
| 88 | + from notes.models import Note |
| 89 | + |
| 90 | + |
| 91 | + class NoteSearchIndex(indexes.SearchIndex): |
| 92 | + text = indexes.CharField(document=True, use_template=True) |
| 93 | + title = indexes.CharField(model_attr='title', boost=1.125) |
0 commit comments