class SongIndex(SearchIndex, Indexable): text = CharField(document=True, use_template=True) artist = CharField(model_attr='album__artist') album = CharField(model_attr='album') def get_model(self): return Song
class ProductBrandIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True) autocomplete_index = EdgeNgramField(use_template=True) short = CharField() def prepare_short(self, obj): return obj.name
class UserIndex(SearchIndex, Indexable): title = CharField() text = CharField(document=True) text_for_highlighting = CharField(indexed=False) url = CharField(model_attr="get_absolute_url", indexed=False) def get_model(self): return User def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.all() ################################### def prepare_title(self, obj): return obj.screen_name def prepare_text(self, obj): return " ".join([ self.prepare_title(obj), self.prepare_text_for_highlighting(obj) ]) def prepare_text_for_highlighting(self, obj): return obj.description.text if obj.description else ""
class StoryIndex(SearchIndex): text = CharField(document=True, use_template=True) author = CharField(faceted=True) pub_date = DateTimeField(model_attr='pub_date', faceted=True) tags = MultiValueField(model_attr='tags', faceted=True) def get_queryset(self): return Story.objects.filter(pub_date__lte=datetime.datetime.now()) def prepare_author(self, obj): try: sauthor = StoryAuthor.objects.filter(story__pub_date= \ self.prepared_data['pub_date'])[0] author = sauthor.author except IndexError: author = u'Journal Staff' return author def prepare_tags(self, obj): tags = Tag.objects.get_for_object(Story.objects.filter(pub_date=\ self.prepared_data['pub_date'])[0]) if not tags is None: return tags else: return u'No Tags'
class OrganizationIndex(SearchIndex, Indexable): title = CharField() text = CharField(document=True) url = CharField(model_attr="get_absolute_url", indexed=False) def get_model(self): return Organization def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.all() ################################### def prepare_title(self, obj): return obj.title def prepare_text(self, obj): return " ".join([ obj.title, obj.long_title, obj.description.text if obj.description else "" ])
class DiscussionPostIndex(SearchIndex, Indexable): """ Post proxy model """ title = CharField(boost=5) text = CharField(document=True) url = CharField(indexed=False) last_changed = CharField(indexed=False) # PERM FIELDS external_access = BooleanField(indexed=False) node_users = MultiValueField(indexed=False) node_visibility = CharField(indexed=False) def get_model(self): return DiscussionPostProxy def index_queryset(self, using=None): """Used when the entire index for model is updated.""" qs = self.get_model().objects.filter( deleted=False, post_type=POST_TYPE_THREAD_POST, thread__thread_type=THREAD_TYPE_DISCUSSION, thread__node__id__gte=1, ) return qs def prepare_title(self, obj): return obj.thread.node.title_with_status() def prepare_text(self, obj): data = [] data.append(obj.thread.node.title) data.append(obj.text) return " ".join([ strip_tags(ch).strip() for ch in data ]) def prepare_url(self, obj): return obj.get_absolute_url() def prepare_last_changed(self, obj): return obj.thread.render_last_changed() ################################### def prepare_node_users(self, obj): if obj.thread.node.visibility == NODE_VISIBILITY_PRIVATE: # store users only for private nodes return list(obj.thread.node.node_users.values_list("user_id", flat=True)) return [] def prepare_external_access(self, obj): return obj.thread.external_access def prepare_node_visibility(self, obj): return obj.thread.node.visibility
class EventIndex(SearchIndex): text = CharField(document=True, use_template=True) creator = CharField(model_attr='creator') datetime = DateTimeField(model_attr='time') created_at = DateTimeField(model_attr='created_at') market = CharField() def prepare_market(self, obj): return Event.market.name
class ProductIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True) name = CharField(model_attr="name") description = CharField(model_attr="description") product_number = CharField(model_attr="product_number") category = CharField(model_attr="category") def index_queryset(self): return Product.objects.filter(is_active=True)
class PageIndex(SearchIndex): """Search index for pages content.""" text = CharField(document=True, use_template=True) title = CharField(model_attr='title') url = CharField(model_attr='get_absolute_url') publication_date = DateTimeField(model_attr='publication_date') def get_queryset(self): """Used when the entire index for model is updated.""" return Page.objects.published()
class BlogIndex(SearchIndex, Indexable): text=CharField(document=True, use_template=True) author=CharField(model_attr='author', faceted=True) title=CharField(model_attr='title', use_template=True) #, indexed=False) body=EdgeNgramField(model_attr='body', use_template=True) timestamp=DateTimeField(model_attr='created') def get_model(self): return Blog def index_queryset(self, using=None): return self.get_model().objects.filter(created__lte=now) def prepare_body(self, obj): return obj.body def prepare_text(self, obj): return obj.text
class MemberIndex(SearchIndex, Indexable): text=CharField(document=True, use_template=True) identifier=CharField(model_attr='identifier') email=CharField(model_attr='email', use_template=True) cellular=EdgeNgramField(model_attr='cellular', use_template=True) #rendered=CharField(use_template=True, indexed=False) timestamp=DateTimeField(model_attr='created') def get_model(self): return Member def index_queryset(self, using=None): return self.get_model().objects.filter(created__lte=now) def prepare_cellular(self, obj): return obj.cellular
class IngredientIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True, template_name="search/indexes/generic_name_index.html") autocomplete_index = EdgeNgramField( use_template=True, template_name="search/indexes/generic_name_index.html") short = CharField() def prepare_short(self, obj): return obj.name
class DeviceModelIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True) quick = CharField(null=True) short = CharField() autocomplete_index = EdgeNgramField(use_template=True) def prepare_short(self, obj): return obj.name def get_queryset(self): return DeviceModel.objects.all()
class TargetValueIndex(CelerySearchIndex, Indexable): '''Base search index which will be shared between various search indexes in targeting. It's strongly related to django-haystack''' # text field with document=True is required text = CharField(document=True) value = AutocompleteField(model_attr='value') category = CharField(model_attr='category') rank = IntegerField() def get_model(self): return TargetValue def prepare_rank(self, target_value): if not target_value.category == dimensions.g_location: return 1 dimension_rank = ((dimensions.city, 3), (dimensions.region, 2), (dimensions.country, 1)) values = target_value.value_dict for dimension, rank in dimension_rank: if dimension in values: # Return rank of lowest hierarchy only return rank def prepare_value(self, target_value): ''' Add hierarchy name to location target values, to enable filtering by hierarchy Example: 'USA' -> 'USA country' 'USA;Wisconsin' -> 'USA;Wisconsin state' 'USA;Pensylvania;California' -> ''USA;Pensylvania;California city' ''' if not target_value.category == dimensions.g_location: return target_value.value # Hierarchy names which are appended to index values location_hierarchies = ['country', 'state', 'city'] values = list(target_value.value_list) last_index = len(values) - 1 hierarchy = location_hierarchies[last_index] values[last_index] = "%s %s" % (values[last_index], hierarchy) return TargetMap.pack(values) def index_queryset(self, **kwargs): '''Returns query on which haystack will perform indexing''' return self.get_model().objects.representants().exclude( category=dimensions.g_publisher)
class InvestigatorIndex(SearchIndex): text = CharField(document=True, use_template=True) first_name = CharField(model_attr='user__first_name') last_name = CharField(model_attr='user__last_name') affiliation = CharField(model_attr='affiliation') def prepare(self, object): self.prepared_data = truncate_fields( super(InvestigatorIndex, self).prepare(object)) return self.prepared_data class Meta: pass
class OrganismIndex(SearchIndex): text = CharField(document=True, use_template=True) name = CharField(model_attr='name') # Hack to avoid error: "xapian.InvalidArgumentError: Term too long (> 245)" # See: https://groups.google.com/forum/?fromgroups#!topic/django-haystack/hRJKcPNPXqw def prepare(self, object): self.prepared_data = truncate_fields( super(OrganismIndex, self).prepare(object)) return self.prepared_data class Meta: pass
class ContactIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True) given_name = CharField(model_attr="given_name") family_name = CharField(model_attr="family_name") role = CharField(model_attr="role") organization = CharField(model_attr="organization") address_1 = CharField(model_attr="address_line_1") address_2 = CharField(model_attr="address_line_2") address_3 = CharField(model_attr="address_line_3") note = CharField(model_attr="note", null=True) tag_list = MultiValueField(model_attr="tag_list", null=True)
class PageIndex(SearchIndex, Indexable): """ Haystack seach index """ name = CharField(model_attr='name') text = CharField(document=True, use_template=True) updated_at = DateTimeField(model_attr='updated_at') def get_model(self): return Page def index_queryset(self, *args, **kwargs): """ Используется, когда весь индекс для модели обновляется. TODO: Check this """ return Page.objects.select_related().all()
class PageIndex(SearchIndex, Indexable): """Search index for pages content.""" text = CharField(document=True, use_template=True) title = CharField(model_attr='title') url = CharField(model_attr='get_absolute_url') publication_date = DateTimeField(model_attr='publication_date') def index_queryset(self, using=None): return self.get_model().objects.published() def should_update(self, instance, **kwargs): return instance.status == Page.PUBLISHED def get_model(self): return Page
class MemberIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True) name = CharField(model_attr='user') autocomplete_index = EdgeNgramField(use_template=True) short = CharField() def prepare_short(self, obj): return obj.user.get_full_name() def prepare_name(self, obj): return "%s <%s>" % (obj.user.get_full_name(), obj.user.email) def get_queryset(self): """Used when the entire index for model is updated.""" return Member.objects.all()
class TradeElementIndex(RealTimeSearchIndex): text = CharField(document=True, use_template=True, template_name="search/indexes/generic_name_index.html") quick = CharField(null=True) short = CharField() autocomplete_index = EdgeNgramField( use_template=True, template_name="search/indexes/generic_name_index.html") def prepare_short(self, obj): return obj.name def get_queryset(self): return TradeElement.objects.all()
class SpeakerIndex(SearchIndex, Indexable): """ We index all actual speakers. """ title = CharField() text = CharField(document=True) url = CharField() def prepare(self, obj): data = super(SpeakerIndex, self).prepare(obj) short_info = "" try: profile = Profile.objects.get(user=obj.user) short_info = profile.short_info except: pass data['title'] = unicode(obj) data['text'] = data['title'] + short_info data['url'] = obj.get_absolute_url() return data def get_model(self): return models.Speaker def index_queryset(self, using=None): c = connection.cursor() c.execute(''' SELECT distinct(s.*) FROM schedule_session as session, speakers_speaker as s WHERE session.speaker_id = s.id UNION SELECT distinct(s.*) FROM schedule_session_additional_speakers as session, speakers_speaker as s WHERE session.speaker_id = s.id ''') ids = [r[0] for r in c.fetchall()] return models.Speaker.objects.filter(id__in=ids).select_related('user')
class LinkIndex(SearchIndex, Indexable): text = CharField(document=True, use_template=True) date_available = DateTimeField(model_attr='date_available') date_update = DateTimeField(model_attr='date_update') tags = MultiValueField(null=True) def prepare_tags(self, obj): if not obj.tags: return tags = [] for tag in obj.get_tags() or []: tags.append(tag.slug) tags.append(tag.name) return tags def get_model(self): return Link def get_updated_field(self): return 'date_update' def index_queryset(self, using=None): return Link.objects.filter( date_available__lte=timezone.now(), published=True)
class BasicPostIndex(SearchIndex): text = CharField(document=True, use_template=True) pub_date = DateTimeField(model_attr='date_published') def index_queryset(self): """Used when the entire index for model is updated.""" return BasicPost.objects.published()
class ThreadIndex(SearchIndex): text = CharField(document=True, use_template=True) creator = CharField(model_attr='creator') datetime = DateTimeField() creation_date = DateTimeField() # TODO: this shit sucks and is could be done without touching # the db most of the time... leave creation date alone and manually # update datetime def prepare_datetime(self, obj): return Post.objects.filter( thread=obj).order_by('-created_at')[0].created_at def prepare_creation_date(self, obj): return Post.objects.filter( thread=obj).order_by('created_at')[0].created_at
class PostIndex(SearchIndex): text = CharField(document=True, use_template=True) def get_updated_field(self): return u'created' def index_queryset(self): return Post.published_objects.all()
class AnimeIndex(SearchIndex, Indexable): text = CharField(document=True) name = CharField(model_attr='name') orig_name = CharField(model_attr='orig_name', null=True) # pub_date = DateTimeField(model_attr='pub_date', null=True) def get_model(self): return Anime def index_queryset(self, using=None): return self.get_model().objects.all() def prepare(self, obj): data = super(AnimeIndex, self).prepare(obj) data['text'] = data['name'] return data
class PostIndex(SearchIndex, Indexable): text = CharField(document=True) def get_model(self): return Post def index_queryset(self, using=None): return self.get_model().objects.all()
class LotIndex(RealTimeSearchIndex): """ Indexes lots.models.Lot instances """ # remember to respect haystack primary field convention name! # by default, stored and indexed parameters are True summary = CharField(document=True, use_template=True) title = CharField(model_attr="title") description = CharField(model_attr="description", indexed=False, null=True) state = CharField(model_attr="get_state_display") shop_id = IntegerField(model_attr="shop__id", stored=False) shop_name = CharField(model_attr="shop__name") category = CharField(model_attr="category") subcategory = CharField(model_attr="subcategory", null=True) added_at = DateTimeField(model_attr="date_time", stored=False) # def get_queryset(self): def index_queryset(self): # allow to search only active lots return Lot.objects.filter(state="A")
class TorrentIndex(SearchIndex, Indexable): text = CharField(document=True) title = CharField(model_attr='title') team_name = CharField(model_attr='team_name', null=True) pub_date = DateTimeField(model_attr='pub_date', null=True) author = CharField(model_attr='author', null=True) category = CharField(model_attr='category', null=True) def get_model(self): return Torrent def index_queryset(self, using=None): return self.get_model().objects.all() def prepare(self, obj): data = super(TorrentIndex, self).prepare(obj) data['text'] = data['title'] return data