Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django haystack: Boosting search results if the searchterm appears in a specific field

I'm using django-haystack for searching on my site. My problem is, I would like to have search results on top if the search term was found in a specific field. Let's say I search for blog-entries, then I would like to show those results on top where the search term was found in the title field.

I read the haystack documentation about field-boosting but i don't understand how it should work.

like image 819
scherlock Avatar asked Nov 11 '11 11:11

scherlock


1 Answers

You can either:

Amend your search index file e.g.

class BlogEntryIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    title = CharField(model_attr='title', boost=1.125)

NOTE: As pointed out in the comments the below would only boost the term title not the field, use the above.

or you can pass the boost to your SearchQuerySet e.g in your haystack urls file.

sqs = SearchQuerySet().boost('title', 1.125)

urlpatterns = patterns('haystack.views',
    url(r'^$', SearchView(searchqueryset=sqs), name='haystack_search'),
)
like image 193
JamesO Avatar answered Nov 15 '22 04:11

JamesO