def test_post_category_not_found(self): """ Assert that when a category gets searched on as a POST request, all results are returned if there are no results for this category. """ self.request_data.update({'process_search_btn':True}) self.set_all_to_same_category() haystack_site.get_index(Coupon).update() self.response = self.client.post(reverse('all-coupons'), self.request_data) self.assert_all_returned()
def setUp(self): super(SearchCouponsTestCase, self).setUp() self.slot_list = SLOT_FACTORY.create_slots(create_count=5) haystack_site.get_index(Coupon).reindex() self.coupon_list = SLOT_FACTORY.get_active_coupons( slot_list=self.slot_list) self.coupon0 = self.coupon_list[0] self.all_coupons = ALL_COUPONS.get_all_coupons( Site.objects.get(id=2))[0] self.request_data = None self.response = None
def test_get_category_not_found(self): """ Assert for a GET search for a category without any coupons, all coupons are returned. """ self.set_all_to_same_category() haystack_site.get_index(Coupon).update() self.response = self.client.get(reverse('all-coupons'), self.request_data) self.assertEqual(self.response.status_code, 200) LOG.debug("test_get_category_not_found = %s" % self.response.__dict__) self.assert_all_returned()
def test_get_both_not_found(self): """ Assert that when a category and query string gets searched on as a GET request, all coupons are returned because there is not match for this search set. """ self.request_data = {'q': 'Return all coupons', 'cat': '1'} self.set_all_to_same_category() haystack_site.get_index(Coupon).update() self.response = self.client.get(reverse('all-coupons'), self.request_data) self.assert_all_returned()
def test_get_both_one_found(self): """ Assert that when a category and query string gets searched on as a GET request, all matched category results return because the string was not found. """ self.request_data = {'q': 'No match string', 'cat': '1'} self.set_all_to_same_category() self.coupon0.offer.business.categories = [1] haystack_site.get_index(Coupon).update() self.response = self.client.get(reverse('all-coupons'), self.request_data) self.assert_one_returned()
def more_like_this(self, model_instance): from haystack.sites import site, NotRegistered index = site.get_index(model_instance.__class__) field_name = index.get_content_field() raw_results = self.conn.more_like_this("id:%s" % self.get_identifier(model_instance), field_name, fl="*,score") return self._process_results(raw_results)
def reindex(instance): try: index = site.get_index(instance.__class__) except NotRegistered: return if isinstance(index, SearchIndex): res = index.enqueue_save(instance)
def index_all_business_coupons(business): """ Index all of this businesses coupons. """ index = None for offer in business.offers.all(): if not index: index = haystack_site.get_index(offer.coupons.model) for coupon in offer.coupons.all(): index.update_object(coupon)
def handle(self, **options): since = datetime.datetime.now() - REINDEX_INTERVAL objects = {} for content_type_id, object_id in Visit.objects.filter(timestamp__gte=since).values_list("content_type_id", "object_id"): if content_type_id not in objects: objects[content_type_id] = [object_id] else: objects[content_type_id].append(object_id) for content_type_id, object_ids in objects.items(): model = ContentType.objects.get(id=content_type_id).model_class() index = site.get_index(model) for ids in chunks(object_ids, getattr(settings, 'HAYSTACK_BATCH_SIZE', 1000)): qs = model.objects.filter(id__in=ids) index.backend.update(index, qs)
def handle_app(self, app, **options): # Cause the default site to load. from haystack import handle_registrations handle_registrations() from django.db.models import get_models from haystack.sites import site, NotRegistered for model in get_models(app): try: index = site.get_index(model) except NotRegistered: if self.verbosity >= 2: print "Skipping '%s' - no index." % model continue extra_lookup_kwargs = {} updated_field = index.get_updated_field() if self.age: if updated_field: extra_lookup_kwargs['%s__gte' % updated_field] = datetime.datetime.now() - datetime.timedelta(hours=self.age) else: if self.verbosity >= 2: print "No updated date field found for '%s' - not restricting by age." % model.__name__ # DRL_TODO: .select_related() seems like a good idea here but # can cause empty QuerySets. Why? qs = index.get_query_set().filter(**extra_lookup_kwargs).order_by(model._meta.pk.name) total = qs.count() if self.verbosity >= 1: print "Indexing %d %s." % (total, smart_str(model._meta.verbose_name_plural)) for start in range(0, total, self.batchsize): end = min(start + self.batchsize, total) if self.verbosity >= 2: print " indexing %s - %d of %d." % (start+1, end, total) # Get a clone of the QuerySet so that the cache doesn't bloat up # in memory. Useful when reindexing large amounts of data. small_cache_qs = qs.all() index.backend.update(index, small_cache_qs[start:end])
def save(self, *args, **kwargs): super(Offer, self).save(*args, **kwargs) index = haystack_site.get_index(Coupon) for coupon in self.coupons.all(): index.update_object(coupon) return self
def update_index(self): """ Update the coupon index for this specific coupon """ index = haystack_site.get_index(Coupon) index.update_object(self)