7

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.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
scherlock
  • 217
  • 3
  • 8

1 Answers1

5

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'),
)
JamesO
  • 25,178
  • 4
  • 40
  • 42
  • Hello again, Thanks for the quick response! I know these two variants from the documentation and the first variant looks interesting but do i have to do something else to make it work? I already tried it but it seems to change nothing at all... The second variant doesn't help me much because it only boosts a searchterm not a field, at least thats how i understand it. – scherlock Nov 11 '11 at 13:38
  • Which search backend are you using? – JamesO Nov 11 '11 at 14:33
  • strange i've got similar setup with whoosh that seems to be working fine, are you setting the order_by to 'score'? although i think that might be the default?? – JamesO Nov 11 '11 at 15:42
  • Tried it, but that wasn't the problem either... Thanks for your untiring help! – scherlock Nov 11 '11 at 16:11
  • @JamesO: doesn't SearchQuerySet().boost('title', 1.5) perform a boost when the _term_ 'title' is searched for... not the _field_ 'title'? – ForeverLearnNeverMaster Jan 10 '12 at 05:26
  • @ForeverLearnNeverMaster - yes i think so, that's what scherlock pointed out in his first comment, apologies i should have edited the answer. – JamesO Jan 10 '12 at 08:51