def test_get_only_by_hash_index(self): """ """ doc1 = SimpleIndexQuery.get_by_example_hash( collection=self.test_1_col, index_id=self.hash_index.index_type_obj.id, example_data={'username': self.col1_doc1.username}) self.assertDocumentsEqual(doc1, self.col1_doc1)
def test_get_only_by_hash_index(self): """ """ doc1 = SimpleIndexQuery.get_by_example_hash(collection=self.test_1_col, index_id=self.hash_index.index_type_obj.id, example_data={ 'username': self.col1_doc1.username }) self.assertDocumentsEqual(doc1, self.col1_doc1)
def test_skiplist_by_example(self): """ """ docs_with_rating = SimpleIndexQuery.get_by_example_skiplist( collection=self.test_1_col, index_id=self.skiplist_index.index_type_obj.id, example_data={'rated': 4}) self.assertNotEqual(docs_with_rating, None) self.assertEqual(len(docs_with_rating), 2) paris_doc = docs_with_rating[0] koeln_doc = docs_with_rating[1] self.assertDocumentsEqual(paris_doc, self.col1_doc1) self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
def test_fulltext_search(self): """ """ docs_with_description = SimpleIndexQuery.fulltext( collection=self.test_1_col, attribute='description', example_text='city', index_id=self.fulltext_index.index_type_obj.id) self.assertNotEqual(docs_with_description, None) self.assertEqual(len(docs_with_description), 2) paris_doc = docs_with_description[0] berlin_doc = docs_with_description[1] self.assertDocumentsEqual(paris_doc, self.col1_doc1) self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
def test_skiplist_by_example(self): """ """ docs_with_rating = SimpleIndexQuery.get_by_example_skiplist( collection=self.test_1_col, index_id=self.skiplist_index.index_type_obj.id, example_data={ 'rated': 4 } ) self.assertNotEqual(docs_with_rating, None) self.assertEqual(len(docs_with_rating), 2) paris_doc = docs_with_rating[0] koeln_doc = docs_with_rating[1] self.assertDocumentsEqual(paris_doc, self.col1_doc1) self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
def test_fulltext_search(self): """ """ docs_with_description = SimpleIndexQuery.fulltext( collection=self.test_1_col, attribute='description', example_text='city', index_id=self.fulltext_index.index_type_obj.id ) self.assertNotEqual(docs_with_description, None) self.assertEqual(len(docs_with_description), 2) paris_doc = docs_with_description[0] berlin_doc = docs_with_description[1] self.assertDocumentsEqual(paris_doc, self.col1_doc1) self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
def test_near_position(self): """ """ # 50.850278, 4.348611 => Brussels near_docs = SimpleIndexQuery.near( collection=self.test_1_col, latitude=50.850278, longitude=4.348611, index_id=self.geo_index.index_type_obj.id, limit=2) self.assertEqual(len(near_docs), 2) koeln_doc = near_docs[0] paris_doc = near_docs[1] self.assertDocumentsEqual(koeln_doc, self.col1_doc2) self.assertDocumentsEqual(paris_doc, self.col1_doc1)
def test_skiplist_range(self): """ """ docs_with_rating = SimpleIndexQuery.range( collection=self.test_1_col, attribute='rated', left=8, right=10, closed=True, index_id=self.skiplist_index.index_type_obj.id, ) self.assertNotEqual(docs_with_rating, None) self.assertEqual(len(docs_with_rating), 2) berlin_doc = docs_with_rating[0] rome_doc = docs_with_rating[1] self.assertDocumentsEqual(berlin_doc, self.col1_doc3) self.assertDocumentsEqual(rome_doc, self.col1_doc4)
def test_near_position(self): """ """ # 50.850278, 4.348611 => Brussels near_docs = SimpleIndexQuery.near( collection=self.test_1_col, latitude=50.850278, longitude=4.348611, index_id=self.geo_index.index_type_obj.id, limit=2 ) self.assertEqual(len(near_docs), 2) koeln_doc = near_docs[0] paris_doc = near_docs[1] self.assertDocumentsEqual(koeln_doc, self.col1_doc2) self.assertDocumentsEqual(paris_doc, self.col1_doc1)
def test_within_position(self): """ """ # 52.370278, 9.733056 => Hannover (Germany) radius_kilometers = 400 radius_meters = radius_kilometers * 1000 in_radius_docs = SimpleIndexQuery.within( collection=self.test_1_col, latitude=52.370278, longitude=9.733056, radius=radius_meters, index_id=self.geo_index.index_type_obj.id) self.assertEqual(len(in_radius_docs), 2) koeln_doc = in_radius_docs[0] berlin_doc = in_radius_docs[1] self.assertDocumentsEqual(koeln_doc, self.col1_doc2) self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
def test_within_position(self): """ """ # 52.370278, 9.733056 => Hannover (Germany) radius_kilometers = 400 radius_meters = radius_kilometers * 1000 in_radius_docs = SimpleIndexQuery.within( collection=self.test_1_col, latitude= 52.370278, longitude=9.733056, radius=radius_meters, index_id=self.geo_index.index_type_obj.id ) self.assertEqual(len(in_radius_docs), 2) koeln_doc = in_radius_docs[0] berlin_doc = in_radius_docs[1] self.assertDocumentsEqual(koeln_doc, self.col1_doc2) self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
def _generate_cache(self): """ """ super(IndexQueryset, self)._generate_cache() index_field = getattr(self._manager._model_class, self._index) result = None # All have these attributes if 'skip' in self._filters: skip = self._filters['skip'] else: skip = None if 'limit' in self._filters: limit = self._filters['limit'] else: limit = None # Hash index if index_field.index_type_obj.type_name == 'hash': result = SimpleIndexQuery.get_by_example_hash( collection=index_field.collection, index_id=index_field.index_type_obj.id, example_data=self._filters, allow_multiple=True, skip=skip, limit=limit, ) # Skiplist index if index_field.index_type_obj.type_name == 'skiplist': range_query = 'left' in self._filters range_query = range_query and 'right' in self._filters range_query = range_query and 'closed' in self._filters range_query = range_query and 'attribute' in self._filters # Range query if range_query: result = SimpleIndexQuery.range( collection=index_field.collection, index_id=index_field.index_type_obj.id, attribute=self._filters['attribute'], left=self._filters['left'], right=self._filters['right'], closed=self._filters['closed'], skip=skip, limit=limit, ) # Normal search query else: result = SimpleIndexQuery.get_by_example_skiplist( collection=index_field.collection, index_id=index_field.index_type_obj.id, example_data=self._filters, allow_multiple=True, skip=skip, limit=limit, ) # Fulltext index if index_field.index_type_obj.type_name == 'fulltext': result = SimpleIndexQuery.fulltext( collection=index_field.collection, index_id=index_field.index_type_obj.id, attribute=self._filters['attribute'], example_text=self._filters['example_text'], skip=skip, limit=limit, ) # Cap constraint if index_field.index_type_obj.type_name == 'cap': pass # Geo index if index_field.index_type_obj.type_name == 'geo': if 'radius' in self._filters: result = SimpleIndexQuery.within( collection=index_field.collection, index_id=index_field.index_type_obj.id, latitude=self._filters['latitude'], longitude=self._filters['longitude'], radius=self._filters['radius'], distance=self._filters['distance'], skip=skip, limit=limit, ) else: result = SimpleIndexQuery.near( collection=index_field.collection, index_id=index_field.index_type_obj.id, latitude=self._filters['latitude'], longitude=self._filters['longitude'], distance=self._filters['distance'], skip=skip, limit=limit, ) # Save cache if isinstance(result, list): self._cache = result else: self._cache.append(result)