Romeo Mihalcea
Romeo Mihalcea

Reputation: 10252

Django haystack including related content and also boosting it

I'm playing with django-haystack and trying to implement it with elasticsearch (I already did actually). My model has title, content and also a tags field which is a ManyRelatedManager:

tags = models.ManyToManyField( 'Tag', through = 'PostTags' )

My search index object is built as follows:

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

My first question is...how do I include the tags in the PostIndex object? I want to give the tags a much higher boost compared to the title and content.

post_text.txt template:

{{ object.title }}
{{ object.content }}
{% for tag in object.tags.all %}
    {{ tag.name }}
{% endfor %}

Upvotes: 0

Views: 71

Answers (1)

Oliver
Oliver

Reputation: 13021

You could add a multi facet field for the tags and populate & boost that at indexing time. In your PostIndex model:

tags = FacetMultiValueField(boost = 2)

and

def prepare_tags(self, obj):
    return [t.name for t in obj.tags.all()] 

Upvotes: 0

Related Questions