Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from haystack import indexes
- from exhibition.models import Exhibition
- class ExhibitionIndex(indexes.SearchIndex, indexes.Indexable):
- text = indexes.EdgeNgramField(document=True, use_template=True)
- home_text = indexes.EdgeNgramField(use_template=True)
- name = indexes.CharField(model_attr='name')
- venue_pk = indexes.IntegerField(model_attr='venue__pk')
- city_pk = indexes.IntegerField(model_attr='venue__city__pk', null=True)
- url_slug = indexes.CharField()
- approved = indexes.CharField()
- country_pk = indexes.CharField()
- continent_pk = indexes.CharField()
- industry_pks = indexes.MultiValueField()
- organization_pks = indexes.MultiValueField()
- tag_names = indexes.MultiValueField()
- start_date = indexes.DateField(model_attr='start_date', null=True)
- end_date = indexes.DateField(model_attr='end_date', null=True)
- def prepare_url_slug(self, exhibition):
- return exhibition.url_slug
- def prepare_approved(self, exhibition):
- # had to use string instead of booleans due to a elasticsearch gotcha
- # http://stackoverflow.com/questions/24659326/ngramfield-not-working-if-booleanfield-is-present-haystack-elasticsearch-wi
- return 'True' if exhibition.approved else 'False'
- def prepare_tag_names(self, exhibition):
- return [t.name for t in exhibition.tags.all()]
- def prepare_industry_pks(self, exhibition):
- return [i.pk for i in exhibition.industries.all()]
- def prepare_organization_pks(self, exhibition):
- return [o.pk for o in exhibition.organizations.all()]
- def prepare_country_pk(self, exhibition):
- # Due to a elasticsearch strange behavior, queries containing the word
- # "IT", which is the primary key of the Country "Italy" were failing,
- # so I had to add some underscodes
- if exhibition.venue and exhibition.venue.city:
- return "_%s_" % exhibition.venue.city.country_id
- else:
- return None
- def prepare_continent_pk(self, exhibition):
- # Due to a elasticsearch strange behavior, queries containing the word
- # "AS", which is the primary key of the Continent "Asia" were failing,
- # so I had to add some underscodes
- if exhibition.venue and exhibition.venue.city:
- return "_%s_" % exhibition.venue.city.country.continent.pk
- else:
- return None
- def get_model(self):
- return Exhibition
Advertisement
Add Comment
Please, Sign In to add comment