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 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 setUp(self): # Ensure we have results. total = SearchQuerySet().count() self.assertTrue(total) # Get some real coords from the index point = point_from_long_lat((151.9491543, -27.4581498)) distance = Distance(km=100000000) result = (SearchQuerySet().dwithin('geoposition', point, distance).distance( 'geoposition', point).order_by('distance'))[0] self.point = point_from_lat_long(result.geoposition)
def test_radius_filter(self): # Perform a standard dwithin search and find the number of results. distance = Distance(km=1) normal_query = (SearchQuerySet().dwithin('geoposition', self.point, distance)) normal_count = normal_query.count() self.assertTrue(normal_count) # Use the custom method and see that it returns the same number. query = (SearchQuerySet().radius_filter(0, 1, geoposition=self.point, order_by='distance')) self.assertEqual(query.count(), normal_count)
def test_range_search(self): counts = set() for distance in range(0, 100, 10): count = (SearchQuerySet().radius_filter( distance, distance + 10, geoposition=self.point).count()) counts.add(count) self.assertTrue(len(counts) > 1)
def search(cls): """ Perform a basic, default search for this content type. Implement this in each Index class. """ return SearchQuerySet()
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
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 test_fake_geo_stuff(self): distance = Distance(km=1000) query = (SearchQuerySet().dwithin('geoposition', self.point, distance).distance( 'geoposition', self.point)) self.assertTrue(query.count())
def rebuild_items(self, identifiers): return SearchQuerySet().filter(id__in=identifiers)