Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created February 9, 2012 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toastdriven/1781529 to your computer and use it in GitHub Desktop.
Save toastdriven/1781529 to your computer and use it in GitHub Desktop.
from haystack import indexes
# The easiest, per-object approach.
class CustomSearchIndex(indexes.SearchIndex, indexes.Indexable):
# The usual fields.
def prepare(self, obj):
prepared = super(CustomSearchIndex, self).prepare(obj)
# Assuming there's a relation...
for tag_set in obj.tag_sets.filter(is_official=True):
tags = [tag.name for tag in tag_set.tags.all()]
prepared["%s_tagset" % tag_set.name] = tags
prepared["%s__exact_tagset" % tag_set.name] = tags
return prepared
# In the solr.xml, add:
<dynamicField name="*_tagset" type="string" indexed="true" stored="true" multiValued="true" />
@ghing
Copy link

ghing commented Feb 9, 2012

<daniellindsley> The part I'm unsure about is the faceting. You may need more than one dynamic field type.
<daniellindsley> Would be just changing the suffix on the key in ``prepare`` dict & adding another field to ``solr.xml`` like the one shown.
<ghing> daniellindsley: ok.  thank you very much
<daniellindsley> You'll probably have to play with it a bit, but that's the general approach I'd take.
<ghing> ok cool.
<daniellindsley> It's possible to put new fields on the index (typically in ``__init__``, doing things like ``self.fields['new_fieldname'] = indexes.CharField(...)``) but that's not typically per-object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment