Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

97 lines (63 sloc) 2.899 kB

Boost

Scoring is a critical component of good search. Normal full-text searches automatically score a document based on how well it matches the query provided. However, sometimes you want certain documents to score better than they otherwise would. Boosting is a way to achieve this. There are three types of boost:

  • Term Boost
  • Document Boost
  • Field Boost

Note

Document & Field boost support was added in Haystack 1.1.

Despite all being types of boost, they take place at different times and have slightly different effects on scoring.

Term boost happens at query time (when the search query is run) and is based around increasing the score is a certain word/phrase is seen.

On the other hand, document & field boosts take place at indexing time (when the document is being added to the index). Document boost causes the relevance of the entire result to go up, where field boost causes only searches within that field to do better.

Term Boost

Term boosting is achieved by using SearchQuerySet.boost. You provide it the term you want to boost on & a floating point value (based around 1.0 as 100% - no boost).

Example:

# Slight increase in relevance for documents that include "banana".
sqs = SearchQuerySet().boost('banana', 1.1)

# Big decrease in relevance for documents that include "blueberry".
sqs = SearchQuerySet().boost('blueberry', 0.8)

See the :doc:`searchqueryset_api` docs for more details on using this method.

Document Boost

Document boosting is done by adding a boost field to the prepared data SearchIndex creates. The best way to do this is to override SearchIndex.prepare:

from haystack import indexes
from notes.models import Note


class NoteSearchIndex(indexes.SearchIndex, indexes.Indexable):
    # Your regular fields here then...

    def prepare(self, obj):
        data = super(NoteSearchIndex, self).prepare(obj)
        data['boost'] = 1.1
        return data

Another approach might be to add a new field called boost. However, this can skew your schema and is not encouraged.

Field Boost

Field boosting is enabled by setting the boost kwarg on the desired field. An example of this might be increasing the significance of a title:

from haystack import indexes
from notes.models import Note


class NoteSearchIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title', boost=1.125)

    def get_model(self):
        return Note
Jump to Line
Something went wrong with that request. Please try again.