def handle_model(self, model, index): if self.verbosity >= 1: print "Checking %s for leftovers." % smart_str( model._meta.verbose_name_plural) if self.verbosity >= 1: print 'Getting database ids...' db_ids = index.index_queryset().values_list('id', flat=True) db_ids = set(smart_str(pk) for pk in db_ids) if self.verbosity >= 1: print 'Getting indexed ids...' model_search_results = SearchQuerySet().models(model) show_progress = self.verbosity >= 2 indexed_ids = model_search_results.get_django_ids( verbose=show_progress) indexed_ids = set(indexed_ids) leftovers = indexed_ids - db_ids count = len(leftovers) if count: if self.verbosity >= 1: print 'Removing %d leftover document%s.' % (count, count != 1 and 's' or '') self.remove_leftovers(model, leftovers) else: if self.verbosity >= 2: print 'Nothing to remove.'
def find_indexed_id(model, newest=None, oldest=None): assert (newest or oldest) and not (newest and oldest) prefix = newest and '-' or '' search = SearchQuerySet().models(model).direct( **{ 'sort': { '_script': { 'type': 'number', 'script': prefix + "org.elasticsearch.common.primitives.Ints.tryParse(doc['django_id'].value)", } } }) try: return search[0].id except IndexError: return None
def model_search(*models): """Create the basic combined search for the specified models.""" if not models: index = get_unified_index() index.build() models = index.indexes.keys() search = SearchQuerySet().models(*models) filters = {} for model in models: for lookup, value in get_index(model).filters().items(): # Make the lookup values optional, meaning that it # will only apply to documents containing the field. filters[lookup] = Optional(value) if filters: search = search.filter(**filters) return search