telemeta.search_indexes module
# -*- coding: utf-8 -*- # Copyright (C) 2015 Angy Fils-Aimé, Killian Mary # This file is part of Telemeta. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from haystack import indexes from telemeta.models import * from haystack.query import SearchQuerySet class KeywordField(indexes.CharField): field_type = 'keyword' class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_acces = indexes.CharField(model_attr='collection__public_access', faceted=True) item_status = indexes.CharField(model_attr='collection__document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) recording_context = indexes.CharField(model_attr='collection__recording_context', default='', faceted=True) physical_format = indexes.CharField(model_attr='collection__physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') location_principal = indexes.CharField(null='None', boost=1.05) location_relation = indexes.CharField() ethnic_group = indexes.CharField(model_attr='ethnic_group', default='') instruments = indexes.CharField(default='') collectors = indexes.CharField(model_attr='collector', default='') recorded_from_date = indexes.DateField(model_attr='recorded_from_date', null='None') recorded_to_date = indexes.DateField(model_attr='recorded_to_date', null='None') year_published = indexes.IntegerField(model_attr='collection__year_published', default='') def prepare_digitized(self, obj): if obj.file.name: return True elif '/' in obj.url: return True else: return False def get_model(self): return MediaItem def prepare_location_principal(self, obj): if obj.location is not None: return u"".join(obj.location.name) else: return None def prepare_location_relation(self, obj): location = [] if obj.location is not None: location_alias = LocationAlias.objects.filter(location__name=obj.location) location_rela = LocationRelation.objects.filter(location__name=obj.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if obj.location.current_location is not None: location.append(obj.location.current_location.name) #print u"".join(' ' + local for local in location).encode("utf-8") #print u"%s".encode("utf-8") % location #print [local for local in location] return u"".join('|' + local for local in location) def prepare_instruments(self, obj): item = MediaItemPerformance.objects.all().filter(media_item__exact=obj) instruments = [] for material in item: if material.instrument is not None: instruments.append(material.instrument.name) if material.alias is not None: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments) def prepare_collectors(self, obj): collectors = [] collectors.append(obj.collection.collector) collectors.append(obj.collector) return u"".join('; ' + collector for collector in collectors) class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_acces = indexes.CharField(model_attr='public_access', faceted=True) item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) physical_format = indexes.CharField(model_attr='physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') location_principal = indexes.CharField(default='', boost=1.05) location_relation = indexes.CharField() ethnic_group = indexes.CharField(default='') instruments = indexes.CharField(default='') collectors = indexes.CharField(model_attr='collector', default='') recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaCollection def prepare_location_principal(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: collec_location.append(item.location.name) return u"".join('|' + location for location in collec_location) def prepare_location_relation(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: location_alias = LocationAlias.objects.filter(location__name=item.location) location_rela = LocationRelation.objects.filter(location__name=item.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if item.location.current_location is not None: location.append(item.location.current_location.name) for name in location: if name and not name in collec_location: collec_location.append(name) return u"".join('|' + location for location in collec_location) def prepare_ethnic_group(self, obj): return "%s" % obj.ethnic_groups() def prepare_instruments(self, obj): instruments = [] items = obj.items.all() for item in items: materials = MediaItemPerformance.objects.all().filter(media_item__exact=item) for material in materials: if material.instrument and not material.instrument in instruments: instruments.append(material.instrument.name) if material.alias and not material.alias in instruments: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments) def prepare_recorded_from_date(self, obj): if obj.recorded_from_year != 0: return datetime.date(int(obj.recorded_from_year), 01, 01) else: return None def prepare_recorded_to_date(self, obj): if obj.recorded_to_year != 0: return datetime.date(int(obj.recorded_to_year), 01, 01) else: return None class MediaCorpusIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) #item_acces = indexes.CharField(model_attr='public_access', faceted=True) #item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) #media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) #recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) #physical_format = indexes.CharField(model_attr='collection__physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') #location_principal = indexes.CharField(default='', boost=1.05) #location_relation = indexes.CharField() #ethnic_group = indexes.CharField(default='') #instruments = indexes.NgramField(default='') #collectors = indexes.NgramField(model_attr='collector', default='') #recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) #year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaCorpus class MediaFondsIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) #item_acces = indexes.CharField(model_attr='public_access', faceted=True) #item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) #media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) #recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) #physical_format = indexes.CharField(model_attr='physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') #location_principal = indexes.CharField(default='', boost=1.05) #location_relation = indexes.CharField() #ethnic_group = indexes.CharField(default='') #instruments = indexes.NgramField(default='') #collectors = indexes.NgramField(model_attr='collector', default='') #recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) #recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) #year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaFonds class LocationIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return Location def index_queryset(self, using=None): loc = MediaItem.objects.values('location') old = Location.objects.filter(current_location__in=loc).values('id') anc = LocationRelation.objects.filter(location__in=loc).values('ancestor_location') return Location.objects.filter(Q(pk__in=loc)|Q(pk__in=old)|Q(pk__in=anc)) class LocationAliasIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return LocationAlias def index_queryset(self, using=None): l = MediaItem.objects.values('location') return LocationAlias.objects.filter(location__in=l) class InstrumentIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return Instrument def index_queryset(self, using=None): instrus = MediaItemPerformance.objects.values('instrument') return Instrument.objects.filter(pk__in=instrus) class InstrumentAliasIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return InstrumentAlias def index_queryset(self, using=None): instrualias = MediaItemPerformance.objects.values('alias') return InstrumentAlias.objects.filter(pk__in=instrualias)
Module variables
var ITEM_PUBLIC_ACCESS_CHOICES
var ITEM_TRANSODING_STATUS
var PUBLIC_ACCESS_CHOICES
var SCOPE_CHOICES
var TYPE_CHOICES
var app_name
var code_linesep
var collection_code_regex
var collection_published_code_regex
var collection_unpublished_code_regex
var default_decoding
var default_encoding
var engine
var eol
var ext
var item_code_regex
var item_published_code_regex
var item_unpublished_code_regex
var mime_type
var private_extra_types
var public_extra_types
var resource_code_regex
var strict_code
Classes
class InstrumentAliasIndex
class InstrumentAliasIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return InstrumentAlias def index_queryset(self, using=None): instrualias = MediaItemPerformance.objects.values('alias') return InstrumentAlias.objects.filter(pk__in=instrualias)
Ancestors (in MRO)
- InstrumentAliasIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var fields
var haystack_use_for_indexing
var objects
var text
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return InstrumentAlias
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
def index_queryset(self, using=None): instrualias = MediaItemPerformance.objects.values('alias') return InstrumentAlias.objects.filter(pk__in=instrualias)
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class InstrumentIndex
class InstrumentIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return Instrument def index_queryset(self, using=None): instrus = MediaItemPerformance.objects.values('instrument') return Instrument.objects.filter(pk__in=instrus)
Ancestors (in MRO)
- InstrumentIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var fields
var haystack_use_for_indexing
var objects
var text
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return Instrument
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
def index_queryset(self, using=None): instrus = MediaItemPerformance.objects.values('instrument') return Instrument.objects.filter(pk__in=instrus)
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class KeywordField
class KeywordField(indexes.CharField): field_type = 'keyword'
Ancestors (in MRO)
- KeywordField
- haystack.fields.CharField
- haystack.fields.SearchField
- __builtin__.object
Class variables
var field_type
Instance variables
var default
Returns the default value for the field.
Methods
def __init__(
self, **kwargs)
def __init__(self, **kwargs): if kwargs.get('facet_class') is None: kwargs['facet_class'] = FacetCharField super(CharField, self).__init__(**kwargs)
def convert(
self, value)
def convert(self, value): if value is None: return None return six.text_type(value)
def has_default(
self)
Returns a boolean of whether this field has a default value.
def has_default(self): """Returns a boolean of whether this field has a default value.""" return self._default is not NOT_PROVIDED
def prepare(
self, obj)
def prepare(self, obj): return self.convert(super(CharField, self).prepare(obj))
def prepare_template(
self, obj)
Flattens an object for indexing.
This loads a template
(search/indexes/{app_label}/{model_name}_{field_name}.txt
) and
returns the result of rendering that template. object
will be in
its context.
def prepare_template(self, obj): """ Flattens an object for indexing. This loads a template (``search/indexes/{app_label}/{model_name}_{field_name}.txt``) and returns the result of rendering that template. ``object`` will be in its context. """ if self.instance_name is None and self.template_name is None: raise SearchFieldError("This field requires either its instance_name variable to be populated or an explicit template_name in order to load the correct template.") if self.template_name is not None: template_names = self.template_name if not isinstance(template_names, (list, tuple)): template_names = [template_names] else: app_label, model_name = get_model_ct_tuple(obj) template_names = ['search/indexes/%s/%s_%s.txt' % (app_label, model_name, self.instance_name)] t = loader.select_template(template_names) return t.render(Context({'object': obj}))
def set_instance_name(
self, instance_name)
def set_instance_name(self, instance_name): self.instance_name = instance_name if self.index_fieldname is None: self.index_fieldname = self.instance_name
class LocationAliasIndex
class LocationAliasIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return LocationAlias def index_queryset(self, using=None): l = MediaItem.objects.values('location') return LocationAlias.objects.filter(location__in=l)
Ancestors (in MRO)
- LocationAliasIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var fields
var haystack_use_for_indexing
var objects
var text
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return LocationAlias
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
def index_queryset(self, using=None): l = MediaItem.objects.values('location') return LocationAlias.objects.filter(location__in=l)
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class LocationIndex
class LocationIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) def get_model(self): return Location def index_queryset(self, using=None): loc = MediaItem.objects.values('location') old = Location.objects.filter(current_location__in=loc).values('id') anc = LocationRelation.objects.filter(location__in=loc).values('ancestor_location') return Location.objects.filter(Q(pk__in=loc)|Q(pk__in=old)|Q(pk__in=anc))
Ancestors (in MRO)
- LocationIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var fields
var haystack_use_for_indexing
var objects
var text
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return Location
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
def index_queryset(self, using=None): loc = MediaItem.objects.values('location') old = Location.objects.filter(current_location__in=loc).values('id') anc = LocationRelation.objects.filter(location__in=loc).values('ancestor_location') return Location.objects.filter(Q(pk__in=loc)|Q(pk__in=old)|Q(pk__in=anc))
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class MediaCollectionIndex
class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_acces = indexes.CharField(model_attr='public_access', faceted=True) item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) physical_format = indexes.CharField(model_attr='physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') location_principal = indexes.CharField(default='', boost=1.05) location_relation = indexes.CharField() ethnic_group = indexes.CharField(default='') instruments = indexes.CharField(default='') collectors = indexes.CharField(model_attr='collector', default='') recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaCollection def prepare_location_principal(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: collec_location.append(item.location.name) return u"".join('|' + location for location in collec_location) def prepare_location_relation(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: location_alias = LocationAlias.objects.filter(location__name=item.location) location_rela = LocationRelation.objects.filter(location__name=item.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if item.location.current_location is not None: location.append(item.location.current_location.name) for name in location: if name and not name in collec_location: collec_location.append(name) return u"".join('|' + location for location in collec_location) def prepare_ethnic_group(self, obj): return "%s" % obj.ethnic_groups() def prepare_instruments(self, obj): instruments = [] items = obj.items.all() for item in items: materials = MediaItemPerformance.objects.all().filter(media_item__exact=item) for material in materials: if material.instrument and not material.instrument in instruments: instruments.append(material.instrument.name) if material.alias and not material.alias in instruments: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments) def prepare_recorded_from_date(self, obj): if obj.recorded_from_year != 0: return datetime.date(int(obj.recorded_from_year), 01, 01) else: return None def prepare_recorded_to_date(self, obj): if obj.recorded_to_year != 0: return datetime.date(int(obj.recorded_to_year), 01, 01) else: return None
Ancestors (in MRO)
- MediaCollectionIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var code
var collectors
var digitized
var ethnic_group
var fields
var haystack_use_for_indexing
var instruments
var item_acces
var item_status
var location_principal
var location_relation
var media_type
var objects
var physical_format
var recorded_from_date
var recorded_to_date
var recording_context
var text
var title
var year_published
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return MediaCollection
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
Get the default QuerySet to index when doing a full update.
Subclasses can override this method to avoid indexing certain objects.
def index_queryset(self, using=None): """ Get the default QuerySet to index when doing a full update. Subclasses can override this method to avoid indexing certain objects. """ return self.get_model()._default_manager.all()
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def prepare_digitized(
self, obj)
def prepare_digitized(self, obj): return obj.has_mediafile
def prepare_ethnic_group(
self, obj)
def prepare_ethnic_group(self, obj): return "%s" % obj.ethnic_groups()
def prepare_instruments(
self, obj)
def prepare_instruments(self, obj): instruments = [] items = obj.items.all() for item in items: materials = MediaItemPerformance.objects.all().filter(media_item__exact=item) for material in materials: if material.instrument and not material.instrument in instruments: instruments.append(material.instrument.name) if material.alias and not material.alias in instruments: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments)
def prepare_location_principal(
self, obj)
def prepare_location_principal(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: collec_location.append(item.location.name) return u"".join('|' + location for location in collec_location)
def prepare_location_relation(
self, obj)
def prepare_location_relation(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: location_alias = LocationAlias.objects.filter(location__name=item.location) location_rela = LocationRelation.objects.filter(location__name=item.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if item.location.current_location is not None: location.append(item.location.current_location.name) for name in location: if name and not name in collec_location: collec_location.append(name) return u"".join('|' + location for location in collec_location)
def prepare_recorded_from_date(
self, obj)
def prepare_recorded_from_date(self, obj): if obj.recorded_from_year != 0: return datetime.date(int(obj.recorded_from_year), 01, 01) else: return None
def prepare_recorded_to_date(
self, obj)
def prepare_recorded_to_date(self, obj): if obj.recorded_to_year != 0: return datetime.date(int(obj.recorded_to_year), 01, 01) else: return None
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class MediaCorpusIndex
class MediaCorpusIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) #item_acces = indexes.CharField(model_attr='public_access', faceted=True) #item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) #media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) #recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) #physical_format = indexes.CharField(model_attr='collection__physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') #location_principal = indexes.CharField(default='', boost=1.05) #location_relation = indexes.CharField() #ethnic_group = indexes.CharField(default='') #instruments = indexes.NgramField(default='') #collectors = indexes.NgramField(model_attr='collector', default='') #recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) #year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaCorpus
Ancestors (in MRO)
- MediaCorpusIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var code
var digitized
var fields
var haystack_use_for_indexing
var objects
var recorded_to_date
var text
var title
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return MediaCorpus
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
Get the default QuerySet to index when doing a full update.
Subclasses can override this method to avoid indexing certain objects.
def index_queryset(self, using=None): """ Get the default QuerySet to index when doing a full update. Subclasses can override this method to avoid indexing certain objects. """ return self.get_model()._default_manager.all()
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def prepare_digitized(
self, obj)
def prepare_digitized(self, obj): return obj.has_mediafile
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class MediaFondsIndex
class MediaFondsIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) #item_acces = indexes.CharField(model_attr='public_access', faceted=True) #item_status = indexes.CharField(model_attr='document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) #media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) #recording_context = indexes.CharField(model_attr='recording_context', default='', faceted=True) #physical_format = indexes.CharField(model_attr='physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') #location_principal = indexes.CharField(default='', boost=1.05) #location_relation = indexes.CharField() #ethnic_group = indexes.CharField(default='') #instruments = indexes.NgramField(default='') #collectors = indexes.NgramField(model_attr='collector', default='') #recorded_from_date = indexes.DateField(model_attr='recorded_from_year', null=True) #recorded_to_date = indexes.DateField(model_attr='recorded_to_year', null=True) #year_published = indexes.IntegerField(model_attr='year_published', default='') def prepare_digitized(self, obj): return obj.has_mediafile def get_model(self): return MediaFonds
Ancestors (in MRO)
- MediaFondsIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var code
var digitized
var fields
var haystack_use_for_indexing
var objects
var text
var title
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return MediaFonds
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
Get the default QuerySet to index when doing a full update.
Subclasses can override this method to avoid indexing certain objects.
def index_queryset(self, using=None): """ Get the default QuerySet to index when doing a full update. Subclasses can override this method to avoid indexing certain objects. """ return self.get_model()._default_manager.all()
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def prepare_digitized(
self, obj)
def prepare_digitized(self, obj): return obj.has_mediafile
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])
class MediaItemIndex
class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_acces = indexes.CharField(model_attr='collection__public_access', faceted=True) item_status = indexes.CharField(model_attr='collection__document_status', faceted=True) digitized = indexes.BooleanField(default=False, faceted=True) media_type = indexes.CharField(model_attr='media_type', null='None', faceted=True) recording_context = indexes.CharField(model_attr='collection__recording_context', default='', faceted=True) physical_format = indexes.CharField(model_attr='collection__physical_format', default='', faceted=True) #content_auto = indexes.EdgeNgramField(model_attr='content') #advance search title = indexes.CharField(model_attr='title') code = KeywordField(model_attr='code', default='') location_principal = indexes.CharField(null='None', boost=1.05) location_relation = indexes.CharField() ethnic_group = indexes.CharField(model_attr='ethnic_group', default='') instruments = indexes.CharField(default='') collectors = indexes.CharField(model_attr='collector', default='') recorded_from_date = indexes.DateField(model_attr='recorded_from_date', null='None') recorded_to_date = indexes.DateField(model_attr='recorded_to_date', null='None') year_published = indexes.IntegerField(model_attr='collection__year_published', default='') def prepare_digitized(self, obj): if obj.file.name: return True elif '/' in obj.url: return True else: return False def get_model(self): return MediaItem def prepare_location_principal(self, obj): if obj.location is not None: return u"".join(obj.location.name) else: return None def prepare_location_relation(self, obj): location = [] if obj.location is not None: location_alias = LocationAlias.objects.filter(location__name=obj.location) location_rela = LocationRelation.objects.filter(location__name=obj.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if obj.location.current_location is not None: location.append(obj.location.current_location.name) #print u"".join(' ' + local for local in location).encode("utf-8") #print u"%s".encode("utf-8") % location #print [local for local in location] return u"".join('|' + local for local in location) def prepare_instruments(self, obj): item = MediaItemPerformance.objects.all().filter(media_item__exact=obj) instruments = [] for material in item: if material.instrument is not None: instruments.append(material.instrument.name) if material.alias is not None: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments) def prepare_collectors(self, obj): collectors = [] collectors.append(obj.collection.collector) collectors.append(obj.collector) return u"".join('; ' + collector for collector in collectors)
Ancestors (in MRO)
- MediaItemIndex
- haystack.indexes.SearchIndex
- thread._local
- haystack.constants.Indexable
- __builtin__.object
Class variables
var code
var collectors
var digitized
var ethnic_group
var fields
var haystack_use_for_indexing
var instruments
var item_acces
var item_status
var location_principal
var location_relation
var media_type
var objects
var physical_format
var recorded_from_date
var recorded_to_date
var recording_context
var text
var title
var year_published
Methods
def __init__(
self)
def __init__(self): self.prepared_data = None content_fields = [] self.field_map = dict() for field_name, field in self.fields.items(): #form field map self.field_map[field.index_fieldname] = field_name if field.document is True: content_fields.append(field_name) if not len(content_fields) == 1: raise SearchFieldError("The index '%s' must have one (and only one) SearchField with document=True." % self.__class__.__name__)
def build_queryset(
self, using=None, start_date=None, end_date=None)
Get the default QuerySet to index when doing an index update.
Subclasses can override this method to take into account related model modification times.
The default is to use SearchIndex.index_queryset
and filter
based on SearchIndex.get_updated_field
def build_queryset(self, using=None, start_date=None, end_date=None): """ Get the default QuerySet to index when doing an index update. Subclasses can override this method to take into account related model modification times. The default is to use ``SearchIndex.index_queryset`` and filter based on ``SearchIndex.get_updated_field`` """ extra_lookup_kwargs = {} model = self.get_model() updated_field = self.get_updated_field() update_field_msg = ("No updated date field found for '%s' " "- not restricting by age.") % model.__name__ if start_date: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = start_date else: warnings.warn(update_field_msg) if end_date: if updated_field: extra_lookup_kwargs['%s__lte' % updated_field] = end_date else: warnings.warn(update_field_msg) index_qs = None if hasattr(self, 'get_queryset'): warnings.warn("'SearchIndex.get_queryset' was deprecated in Haystack v2. Please rename the method 'index_queryset'.") index_qs = self.get_queryset() else: index_qs = self.index_queryset(using=using) if not hasattr(index_qs, 'filter'): raise ImproperlyConfigured("The '%r' class must return a 'QuerySet' in the 'index_queryset' method." % self) # `.select_related()` seems like a good idea here but can fail on # nullable `ForeignKey` as well as what seems like other cases. return index_qs.filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
def clear(
self, using=None)
Clears the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def clear(self, using=None): """ Clears the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.clear(models=[self.get_model()])
def full_prepare(
self, obj)
def full_prepare(self, obj): self.prepared_data = self.prepare(obj) for field_name, field in self.fields.items(): # Duplicate data for faceted fields. if getattr(field, 'facet_for', None): source_field_name = self.fields[field.facet_for].index_fieldname # If there's data there, leave it alone. Otherwise, populate it # with whatever the related field has. if self.prepared_data[field_name] is None and source_field_name in self.prepared_data: self.prepared_data[field.index_fieldname] = self.prepared_data[source_field_name] # Remove any fields that lack a value and are ``null=True``. if field.null is True: if self.prepared_data[field.index_fieldname] is None: del(self.prepared_data[field.index_fieldname]) return self.prepared_data
def get_content_field(
self)
Returns the field that supplies the primary document to be indexed.
def get_content_field(self): """Returns the field that supplies the primary document to be indexed.""" for field_name, field in self.fields.items(): if field.document is True: return field.index_fieldname
def get_field_weights(
self)
Returns a dict of fields with weight values
def get_field_weights(self): """Returns a dict of fields with weight values""" weights = {} for field_name, field in self.fields.items(): if field.boost: weights[field_name] = field.boost return weights
def get_model(
self)
def get_model(self): return MediaItem
def get_updated_field(
self)
Get the field name that represents the updated date for the model.
If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name.
def get_updated_field(self): """ Get the field name that represents the updated date for the model. If specified, this is used by the reindex command to filter out results from the QuerySet, enabling you to reindex only recent records. This method should either return None (reindex everything always) or a string of the Model's DateField/DateTimeField name. """ return None
def index_queryset(
self, using=None)
Get the default QuerySet to index when doing a full update.
Subclasses can override this method to avoid indexing certain objects.
def index_queryset(self, using=None): """ Get the default QuerySet to index when doing a full update. Subclasses can override this method to avoid indexing certain objects. """ return self.get_model()._default_manager.all()
def load_all_queryset(
self)
Provides the ability to override how objects get loaded in conjunction
with SearchQuerySet.load_all
.
This is useful for post-processing the results from the query, enabling
things like adding select_related
or filtering certain data.
By default, returns all()
on the model's default manager.
def load_all_queryset(self): """ Provides the ability to override how objects get loaded in conjunction with ``SearchQuerySet.load_all``. This is useful for post-processing the results from the query, enabling things like adding ``select_related`` or filtering certain data. By default, returns ``all()`` on the model's default manager. """ return self.get_model()._default_manager.all()
def prepare(
self, obj)
Fetches and adds/alters data before indexing.
def prepare(self, obj): """ Fetches and adds/alters data before indexing. """ self.prepared_data = { ID: get_identifier(obj), DJANGO_CT: get_model_ct(obj), DJANGO_ID: force_text(obj.pk), } for field_name, field in self.fields.items(): # Use the possibly overridden name, which will default to the # variable name of the field. self.prepared_data[field.index_fieldname] = field.prepare(obj) if hasattr(self, "prepare_%s" % field_name): value = getattr(self, "prepare_%s" % field_name)(obj) self.prepared_data[field.index_fieldname] = value return self.prepared_data
def prepare_collectors(
self, obj)
def prepare_collectors(self, obj): collectors = [] collectors.append(obj.collection.collector) collectors.append(obj.collector) return u"".join('; ' + collector for collector in collectors)
def prepare_digitized(
self, obj)
def prepare_digitized(self, obj): if obj.file.name: return True elif '/' in obj.url: return True else: return False
def prepare_instruments(
self, obj)
def prepare_instruments(self, obj): item = MediaItemPerformance.objects.all().filter(media_item__exact=obj) instruments = [] for material in item: if material.instrument is not None: instruments.append(material.instrument.name) if material.alias is not None: instruments.append(material.alias.name) return u"".join('|' + instru for instru in instruments)
def prepare_location_principal(
self, obj)
def prepare_location_principal(self, obj): if obj.location is not None: return u"".join(obj.location.name) else: return None
def prepare_location_relation(
self, obj)
def prepare_location_relation(self, obj): location = [] if obj.location is not None: location_alias = LocationAlias.objects.filter(location__name=obj.location) location_rela = LocationRelation.objects.filter(location__name=obj.location) for rela in location_rela: location.append(rela.ancestor_location.name) for alias in location_alias: location.append(alias.alias) if obj.location.current_location is not None: location.append(obj.location.current_location.name) #print u"".join(' ' + local for local in location).encode("utf-8") #print u"%s".encode("utf-8") % location #print [local for local in location] return u"".join('|' + local for local in location)
def read_queryset(
self, using=None)
Get the default QuerySet for read actions.
Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects.
def read_queryset(self, using=None): """ Get the default QuerySet for read actions. Subclasses can override this method to work with other managers. Useful when working with default managers that filter some objects. """ return self.index_queryset(using=using)
def reindex(
self, using=None)
Completely clear the index for this model and rebuild it.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def reindex(self, using=None): """ Completely clear the index for this model and rebuild it. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ self.clear(using=using) self.update(using=using)
def remove_object(
self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class's post-delete hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def remove_object(self, instance, using=None, **kwargs): """ Remove an object from the index. Attached to the class's post-delete hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.remove(instance, **kwargs)
def should_update(
self, instance, **kwargs)
Determine if an object should be updated in the index.
It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed.
By default, returns True (always reindex).
def should_update(self, instance, **kwargs): """ Determine if an object should be updated in the index. It's useful to override this when an object may save frequently and cause excessive reindexing. You should check conditions on the instance and return False if it is not to be indexed. By default, returns True (always reindex). """ return True
def update(
self, using=None)
Updates the entire index.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update(self, using=None): """ Updates the entire index. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ backend = self._get_backend(using) if backend is not None: backend.update(self, self.index_queryset(using=using))
def update_object(
self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class's post-save hook.
If using
is provided, it specifies which connection should be
used. Default relies on the routers to decide which backend should
be used.
def update_object(self, instance, using=None, **kwargs): """ Update the index for a single object. Attached to the class's post-save hook. If ``using`` is provided, it specifies which connection should be used. Default relies on the routers to decide which backend should be used. """ # Check to make sure we want to index this first. if self.should_update(instance, **kwargs): backend = self._get_backend(using) if backend is not None: backend.update(self, [instance])