def test_search_facility_multiple_filters(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Mordal mountains medical clinic') mommy.make(FacilityApproval, facility=facility) facility.is_published = True facility.save() facility_2 = mommy.make(Facility, name='Eye of mordal health center', is_published=False) index_instance('facilities', 'Facility', str(facility.id), 'test_index') index_instance('facilities', 'Facility', str(facility_2.id), 'test_index') url = url + "?search={}&is_published={}".format('mordal', 'false') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_search_facility_using_query_dsl(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Kanyakini') index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') query_dsl = { "from": 0, "size": 30, "query": { "query_string": { "default_field": "name", "query": "Kanyakini" } } } url = url + "?search={}".format(json.dumps(query_dsl)) response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_user_search(self): user = mommy.make(get_user_model(), is_national=True) self.client.force_authenticate(user) county_user = mommy.make(get_user_model()) sub_county_user = mommy.make(get_user_model(), first_name='kijana', last_name='mzee') index_instance('users', 'MflUser', str(sub_county_user.id), 'test_index') url = reverse("api:users:mfl_users_list") url = url + "?search=kijana" response = self.client.get(url) self.assertEquals(200, response.status_code) # the national users are not allowed to see the sub county level users county = mommy.make(County) const = mommy.make(Constituency, county=county) mommy.make(UserCounty, county=county, user=county_user) mommy.make(UserConstituency, user=sub_county_user, constituency=const, created_by=county_user, updated_by=county_user) filtered_response = self.client.get(url) self.assertEquals(200, filtered_response.status_code)
def test_search_facility_using_query_dsl(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Kanyakini') index_instance('facilities', 'Facility', str(facility.id), 'test_index') query_dsl = { "from": 0, "size": 30, "query": { "query_string": { "default_field": "name", "query": "Kanyakini" } } } url = url + "?search={}".format(json.dumps(query_dsl)) response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_search_facility_multiple_filters(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make( Facility, name='Mordal mountains medical clinic') mommy.make(FacilityApproval, facility=facility) facility.is_published = True facility.save() facility_2 = mommy.make( Facility, name='Eye of mordal health center', is_published=False) index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') index_instance( 'facilities', 'Facility', str(facility_2.id), 'test_index') url = url + "?search={}&is_published={}".format('mordal', 'false') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_non_indexable_model(self): obj = mommy.make_recipe( 'mfl_gis.tests.facility_coordinates_recipe') self.assertEquals(1, FacilityCoordinates.objects.count()) index_instance( 'mfl_gis', 'FacilityCoordinates', str(obj.id), 'test_index')
def test_chu_search_using_code(self): facility = mommy.make(Facility) facility_2 = mommy.make(Facility) chu_url = reverse("api:chul:community_health_units_list") chu = mommy.make(CommunityHealthUnit, facility=facility, name='Jericho') mommy.make(CommunityHealthUnit, facility=facility_2) index_instance(chu) chu_search_url = chu_url + "?search={}".format(chu.code) response = self.client.get(chu_search_url) self.assertEquals(200, response.status_code)
def test_chu_search_using_code(self): facility = mommy.make(Facility) facility_2 = mommy.make(Facility) chu_url = reverse("api:chul:community_health_units_list") chu = mommy.make( CommunityHealthUnit, facility=facility, name='Jericho') mommy.make(CommunityHealthUnit, facility=facility_2) index_instance( 'chul', 'CommunityHealthUnit', str(chu.id), 'test_index') chu_search_url = chu_url + "?search={}".format(chu.code) response = self.client.get(chu_search_url) self.assertEquals(200, response.status_code)
def test_chu_search(self): facility = mommy.make(Facility) facility_2 = mommy.make(Facility) chu_url = reverse("api:chul:community_health_units_list") chu = mommy.make(CommunityHealthUnit, facility=facility, name='Jericho') mommy.make(CommunityHealthUnit, facility=facility_2) index_instance('chul', 'CommunityHealthUnit', str(chu.id), 'test_index') chu_search_url = chu_url + "?search=Jericho" response = self.client.get(chu_search_url) self.assertEquals(200, response.status_code)
def test_seach_auto_complete(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Kanyakini') index_instance(facility, 'test_index') url = url + "?search_auto={}".format('Kanya') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_filter_data(self): api = ElasticAPI() api.delete_index('test_index') api.setup_index('test_index') test_facility = mommy.make(Facility, name='test facility') index_instance(test_facility, 'test_index') mommy.make(Facility) qs = Facility.objects.all() search_filter = SearchFilter(name='search') # some weird bug there is a delay in getting the search results for x in range(0, 100): search_filter.filter(qs, 'test') api.delete_index('test_index')
def test_seach_auto_complete(self): url = reverse('api:facilities:facilities_list') self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Kanyakini') index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') url = url + "?search_auto={}".format('Kanya') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def test_filter_data(self): api = ElasticAPI() api.delete_index('test_index') api.setup_index('test_index') test_facility = mommy.make(Facility, name='test facility') index_instance( 'facilities', 'Facility', str(test_facility.id), 'test_index') mommy.make(Facility) qs = Facility.objects.all() search_filter = SearchFilter(name='search') # some weird bug there is a delay in getting the search results for x in range(0, 100): search_filter.filter(qs, 'test') api.delete_index('test_index')
def test_search_facility_using_material_view(self): self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Facility ya kutest material') url = reverse('api:facilities:material') index_instance(facility, 'test_index') url = url + "?search={}".format('material') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def retry_indexing(): """ Indexes the the objects that were not indexed on save """ objects_with_errors = ErrorQueue.objects.filter( error_type='SEARCH_INDEXING_ERROR') for obj in objects_with_errors: obj_path = "{}.models.{}".format(obj.app_label, obj.model_name) model = pydoc.locate(obj_path) try: instance = model.objects.get(id=obj.object_pk) result = index_instance(instance._meta.app_label, instance.__class__.__name__, instance.id) if result: obj.delete() else: obj.retries = obj.retries + 1 if obj.retries > 2: mail_admins(subject="Update Search Index Error", message="Indexing failed records is failing." " Please check and ensure elasticsearch is up") obj.save() except model.DoesNotExist: # The related object is already deleted in the database LOGGER.info("The record to be indexed has been deleted")
def retry_indexing(): """ Indexes the the objects that were not indexed on save """ objects_with_errors = ErrorQueue.objects.filter( error_type='SEARCH_INDEXING_ERROR') for obj in objects_with_errors: obj_path = "{}.models.{}".format(obj.app_label, obj.model_name) model = pydoc.locate(obj_path) try: instance = model.objects.get(id=obj.object_pk) result = index_instance( instance._meta.app_label, instance.__class__.__name__, instance.id) if result: obj.delete() else: obj.retries = obj.retries + 1 if obj.retries > 2: mail_admins( subject="Update Search Index Error", message="Indexing failed records is failing." " Please check and ensure elasticsearch is up" ) obj.save() except model.DoesNotExist: # The related object is already deleted in the database LOGGER.info("The record to be indexed has been deleted")
def test_search_facility_using_material_view(self): self.elastic_search_api = ElasticAPI() self.elastic_search_api.setup_index(index_name='test_index') facility = mommy.make(Facility, name='Facility ya kutest material') url = reverse('api:facilities:material') index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') url = url + "?search={}".format('material') response = self.client.get(url) self.assertEquals(200, response.status_code) self.assertIsInstance(response.data.get('results'), list) self.elastic_search_api.delete_index('test_index')
def handle(self, *args, **options): # optimize this to index in bulk apps_lists = settings.LOCAL_APPS for app_name in apps_lists: app = get_app(app_name) for model in get_models(app): if model and confirm_model_is_indexable(model): all_instances = model.objects.all()[0:100] \ if options.get('test') else model.objects.all() [ index_instance( obj._meta.app_label, obj.__class__.__name__, str(obj.id)) for obj in all_instances] message = "Indexed {} {}".format( all_instances.count(), model._meta.verbose_name_plural.capitalize()) self.stdout.write(message) else: message = "Not indexing model {}".format( model.__name__) self.stdout.write(message) self.stdout.write("Finished indexing")
def handle(self, *args, **options): # optimize this to index in bulk apps_lists = settings.LOCAL_APPS for app_name in apps_lists: app = get_app(app_name) for model in get_models(app): if model and confirm_model_is_indexable(model): all_instances = model.objects.all()[0:100] \ if options.get('test') else model.objects.all() [ index_instance(obj._meta.app_label, obj.__class__.__name__, str(obj.id)) for obj in all_instances ] message = "Indexed {} {}".format( all_instances.count(), model._meta.verbose_name_plural.capitalize()) self.stdout.write(message) else: message = "Not indexing model {}".format(model.__name__) self.stdout.write(message) self.stdout.write("Finished indexing")
def test_seach_facility_by_code(self): mommy.make(Facility, code=17780) api = ElasticAPI() api.delete_index('test_index') api.setup_index('test_index') test_facility = mommy.make(Facility, name='test facility') index_instance('facilities', 'Facility', str(test_facility.id), 'test_index') mommy.make(Facility) qs = Facility.objects.all() search_filter = SearchFilter(name='search') # some weird bug there is a delay in getting the search results for x in range(0, 100): search_filter.filter(qs, 18990) search_filter.filter(qs, 17780) api.delete_index('test_index')
def test_remove_document(self): index_name = 'test_index' self.elastic_search_api.setup_index(index_name=index_name) facility = mommy.make(Facility, name='Fig tree medical clinic') result = index_instance(facility, 'test_index') self.assertEquals(201, result.status_code) self.elastic_search_api.remove_document(index_name, 'facility', str(facility.id)) self.elastic_search_api.delete_index(index_name='test_index')
def test_remove_document(self): index_name = 'test_index' self.elastic_search_api.setup_index(index_name=index_name) facility = mommy.make(Facility, name='Fig tree medical clinic') result = index_instance('facilities', 'Facility', str(facility.id), 'test_index') self.assertTrue(result) self.elastic_search_api.remove_document(index_name, 'facility', str(facility.id)) self.elastic_search_api.delete_index(index_name='test_index')
def test_search_document_no_instance_type(self): index_name = 'test_index' response = self.elastic_search_api.setup_index(index_name=index_name) self.assertEquals(200, response.status_code) facility = mommy.make(Facility, name='Fig tree medical clinic') result = index_instance(facility, 'test_index') self.assertEquals(201, result.status_code) self.elastic_search_api.search_document(index_name=index_name, instance_type=Facility, query='tree')
def test_remove_document(self): index_name = 'test_index' self.elastic_search_api.setup_index(index_name=index_name) facility = mommy.make(Facility, name='Fig tree medical clinic') result = index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') self.assertTrue(result) self.elastic_search_api.remove_document( index_name, 'facility', str(facility.id)) self.elastic_search_api.delete_index(index_name='test_index')
def test_search_document_no_instance_type(self): index_name = 'test_index' response = self.elastic_search_api.setup_index(index_name=index_name) self.assertEquals(200, response.status_code) facility = mommy.make(Facility, name='Fig tree medical clinic') result = index_instance( 'facilities', 'Facility', str(facility.id), 'test_index') self.assertTrue(result) self.elastic_search_api.search_document( index_name=index_name, instance_type=Facility, query='tree')
def test_user_search(self): user = mommy.make(get_user_model(), is_national=True) self.client.force_authenticate(user) county_user = mommy.make(get_user_model()) sub_county_user = mommy.make( get_user_model(), first_name='kijana', last_name='mzee') index_instance( 'users', 'MflUser', str(sub_county_user.id), 'test_index') url = reverse("api:users:mfl_users_list") url = url + "?search=kijana" response = self.client.get(url) self.assertEquals(200, response.status_code) # the national users are not allowed to see the sub county level users county = mommy.make(County) const = mommy.make(Constituency, county=county) mommy.make(UserCounty, county=county, user=county_user) mommy.make( UserConstituency, user=sub_county_user, constituency=const, created_by=county_user, updated_by=county_user) filtered_response = self.client.get(url) self.assertEquals(200, filtered_response.status_code)
def test_index_non_indexable_model(self): job_title = mommy.make(JobTitle) self.assertFalse(index_instance('users', 'JobTitle', job_title.id))
def test_index_document(self): facility = mommy.make(Facility, name='Fig tree medical clinic') self.elastic_search_api.setup_index(index_name='test_index') result = index_instance(facility, 'test_index') self.assertEquals(201, result.status_code)
def test_index_non_indexable_model(self): job_title = mommy.make(JobTitle) self.assertFalse( index_instance('users', 'JobTitle', job_title.id) )
def handle(self, *args, **options): for record in FacilityExportExcelMaterialView.objects.all(): index_instance('facilities', 'FacilityExportExcelMaterialView', record.id) logger.info("indexed instance {0}".format(record.name))
def test_non_indexable_model(self): obj = mommy.make_recipe('mfl_gis.tests.facility_coordinates_recipe') self.assertEquals(1, FacilityCoordinates.objects.count()) index_instance(obj)
def handle(self, *args, **options): for record in FacilityExportExcelMaterialView.objects.all(): index_instance(record) logger.info("indexed instance {0}".format(record.name))