def remove_from_index(instance, mapping): """ Utility function for signal listeners to remove from Elasticsearch. Currently uses synchronous tasks. And because of that all exceptions are caught, so failures will not interfere with the regular model updates. """ if settings.ES_DISABLED: return logger.info(u'Removing instance %s: %s' % (instance.__class__.__name__, instance.pk)) # Extract all aliases available. aliases = list( itertools.chain(*[ v['aliases'].keys() for v in es.indices.get_aliases().itervalues() if 'aliases' in v ])) for index in [DEFAULT_INDEX, NEW_INDEX]: try: if index in aliases: tasks.unindex_objects(mapping, [instance.id], es=es, index=index) es.indices.refresh(index) except NotFoundError, e: logger.warn('Not found in index instance %s: %s' % (instance.__class__.__name__, instance.pk)) except Exception, e: logger.error(traceback.format_exc(e))
def remove_from_index(instance, mapping): """ Utility function for signal listeners to remove from Elasticsearch. Currently uses synchronous tasks. And because of that all exceptions are caught, so failures will not interfere with the regular model updates. """ logger.info('Removing instance %s' % instance) try: tasks.unindex_objects(mapping, [instance.id], index=settings.ES_INDEXES['default']) except Exception, e: logger.error(traceback.format_exc(e))
def unindex(self, index_name): ''' Removing deleted objects from our index-enabled models. ''' for mappingClass in self.MAPPINGS: model = mappingClass.get_model() self.stdout.write('Removing deleted %s objects from index %s' % (model, index_name)) model_objs = model.objects.filter(is_deleted=True) model_ids = list(model_objs.values_list('pk', flat=True)) if model_ids: try: tasks.unindex_objects(mappingClass, model_ids, index=index_name) except: pass
def remove_from_index(instance, mapping): """ Utility function for signal listeners to remove from Elasticsearch. Currently uses synchronous tasks. And because of that all exceptions are caught, so failures will not interfere with the regular model updates. """ if settings.ES_DISABLED: return logger.info(u'Removing instance %s: %s' % (instance.__class__.__name__, instance.pk)) try: main_index_with_type = get_index_name(main_index, mapping) tasks.unindex_objects(mapping, [instance.id], es=es, index=main_index_with_type) es.indices.refresh(main_index_with_type) except NotFoundError, e: logger.warn('Not found in index instance %s: %s' % (instance.__class__.__name__, instance.pk))
def test_tasks(self): documents = [ {'id': 1, 'name': 'odin skullcrusher'}, {'id': 2, 'name': 'heimdall kneebiter'}, {'id': 3, 'name': 'erik rose'} ] for doc in documents: FakeModel(**doc) # Test index_objects task index_objects(FakeDjangoMappingType, [1, 2, 3]) FakeDjangoMappingType.refresh_index() eq_(FakeDjangoMappingType.search().count(), 3) # Test unindex_objects task unindex_objects(FakeDjangoMappingType, [1, 2, 3]) FakeDjangoMappingType.refresh_index() eq_(FakeDjangoMappingType.search().count(), 0)
def test_tasks(self): documents = [ {"id": 1, "name": "odin skullcrusher"}, {"id": 2, "name": "heimdall kneebiter"}, {"id": 3, "name": "erik rose"}, ] for doc in documents: FakeModel(**doc) # Test index_objects task index_objects(FakeDjangoMappingType, [1, 2, 3]) FakeDjangoMappingType.refresh_index() eq_(FakeDjangoMappingType.search().count(), 3) # Test unindex_objects task unindex_objects(FakeDjangoMappingType, [1, 2, 3]) FakeDjangoMappingType.refresh_index() eq_(FakeDjangoMappingType.search().count(), 0)
def remove_from_index(instance, mapping): """ Utility function for signal listeners to remove from Elasticsearch. Currently uses synchronous tasks. And because of that all exceptions are caught, so failures will not interfere with the regular model updates. """ if settings.ES_DISABLED: return logger.info(u'Removing instance %s: %s' % (instance.__class__.__name__, instance.pk)) # Extract all aliases available. aliases = list(itertools.chain(*[v['aliases'].keys() for v in es.indices.get_aliases().itervalues() if 'aliases' in v])) for index in [DEFAULT_INDEX, NEW_INDEX]: try: if index in aliases: tasks.unindex_objects(mapping, [instance.id], es=es, index=index) es.indices.refresh(index) except NotFoundError, e: logger.warn('Not found in index instance %s: %s' % (instance.__class__.__name__, instance.pk)) except Exception, e: logger.error(traceback.format_exc(e))