matiascelasco

An example of a Haystack search index

Oct 31st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. from haystack import indexes
  2. from exhibition.models import Exhibition
  3.  
  4.  
  5. class ExhibitionIndex(indexes.SearchIndex, indexes.Indexable):
  6.     text = indexes.EdgeNgramField(document=True, use_template=True)
  7.     home_text = indexes.EdgeNgramField(use_template=True)
  8.     name = indexes.CharField(model_attr='name')
  9.     venue_pk = indexes.IntegerField(model_attr='venue__pk')
  10.     city_pk = indexes.IntegerField(model_attr='venue__city__pk', null=True)
  11.     url_slug = indexes.CharField()
  12.     approved = indexes.CharField()
  13.     country_pk = indexes.CharField()
  14.     continent_pk = indexes.CharField()
  15.     industry_pks = indexes.MultiValueField()
  16.     organization_pks = indexes.MultiValueField()
  17.     tag_names = indexes.MultiValueField()
  18.     start_date = indexes.DateField(model_attr='start_date', null=True)
  19.     end_date = indexes.DateField(model_attr='end_date', null=True)
  20.  
  21.     def prepare_url_slug(self, exhibition):
  22.         return exhibition.url_slug
  23.  
  24.     def prepare_approved(self, exhibition):
  25.         # had to use string instead of booleans due to a elasticsearch gotcha
  26.         # http://stackoverflow.com/questions/24659326/ngramfield-not-working-if-booleanfield-is-present-haystack-elasticsearch-wi
  27.         return 'True' if exhibition.approved else 'False'
  28.  
  29.     def prepare_tag_names(self, exhibition):
  30.         return [t.name for t in exhibition.tags.all()]
  31.  
  32.     def prepare_industry_pks(self, exhibition):
  33.         return [i.pk for i in exhibition.industries.all()]
  34.  
  35.     def prepare_organization_pks(self, exhibition):
  36.         return [o.pk for o in exhibition.organizations.all()]
  37.  
  38.     def prepare_country_pk(self, exhibition):
  39.         # Due to a elasticsearch strange behavior, queries containing the word
  40.         # "IT", which is the primary key of the Country "Italy" were failing,
  41.         # so I had to add some underscodes
  42.         if exhibition.venue and exhibition.venue.city:
  43.             return "_%s_" % exhibition.venue.city.country_id
  44.         else:
  45.             return None
  46.  
  47.     def prepare_continent_pk(self, exhibition):
  48.         # Due to a elasticsearch strange behavior, queries containing the word
  49.         # "AS", which is the primary key of the Continent "Asia" were failing,
  50.         # so I had to add some underscodes
  51.         if exhibition.venue and exhibition.venue.city:
  52.             return "_%s_" % exhibition.venue.city.country.continent.pk
  53.         else:
  54.             return None
  55.  
  56.     def get_model(self):
  57.         return Exhibition
Advertisement
Add Comment
Please, Sign In to add comment