def test_index_exception(self): """ Ensure that an exception is thrown if we have not indexed properly. """ # Delete the index remove_index() search_url = "/api/v1/repositories/{repo}/search/".format( repo=self.repo.slug) with self.assertRaises(ReindexException): self.client.get(search_url) # Create a mapping with self.assertRaises(ReindexException): # Mapping doesn't exist yet get_conn() conn = get_conn(verify=False) conn.indices.create(INDEX_NAME) conn.indices.refresh() create_mapping() resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) self.assertEqual(json.loads(resp.content.decode('utf-8'))['count'], 0) # Reindex all resources resource_ids = LearningResource.objects.values_list("id", flat=True) index_resources(resource_ids) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) self.assertEqual( json.loads(resp.content.decode('utf-8'))['count'], len(resource_ids))
def index_all(): """ Reindex all objects """ with virtualenv(), env.cd("mootiro_maps"): setup_django() from organization.models import Organization from komoo_resource.models import Resource from need.models import Need from community.models import Community from authentication.models import User from komoo_project.models import Project from search.utils import reset_index, create_mapping, refresh_index, index_object model_list = [Organization, Resource, Need, Community, User, Project] logging.info("Recreating index ... ") reset_index() logging.info("Create mappings ... ") create_mapping() logging.info("Indexing each object ... ") for model in model_list: for obj in model.objects.all(): index_object(obj) logging.info("refreshing index ... ") refresh_index() logging.info("ES indexes rebuilt.")
def index_all(): """ Reindex all objects """ with virtualenv(), env.cd('mootiro_maps'): setup_django() from organization.models import Organization from komoo_resource.models import Resource from need.models import Need from community.models import Community from authentication.models import User from komoo_project.models import Project from search.utils import (reset_index, create_mapping, refresh_index, index_object) model_list = [Organization, Resource, Need, Community, User, Project] logging.info('Recreating index ... ') reset_index() logging.info('Create mappings ... ') create_mapping() logging.info('Indexing each object ... ') for model in model_list: for obj in model.objects.all(): index_object(obj) logging.info('refreshing index ... ') refresh_index() logging.info('ES indexes rebuilt.')
def configure_elasticsearch(): """ configure elasticsearch from scratch """ run_elasticsearch(bg='true') local('sleep 10s') setup_django() from search.utils import reset_index, create_mapping, index_object reset_index() create_mapping() kill_tasks('elasticsearch')
def elasticsearch(): """ configure elasticsearch from scratch """ run_elasticsearch(bg='true') env.run('sleep 10s') setup_django() from search.utils import reset_index, create_mapping reset_index() create_mapping() kill_tasks('elasticsearch')
def test_index_exception(self): """ Ensure that an exception is thrown if we have not indexed properly. """ # Delete the index remove_index() search_url = "/api/v1/repositories/{repo}/search/".format( repo=self.repo.slug ) with self.assertRaises(ReindexException): self.client.get(search_url) # Create a mapping with self.assertRaises(ReindexException): # Mapping doesn't exist yet get_conn() conn = get_conn(verify=False) conn.indices.create(INDEX_NAME) conn.indices.refresh() create_mapping() resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) self.assertEqual(json.loads(resp.content.decode('utf-8'))['count'], 0) # Reindex all resources resource_ids = LearningResource.objects.values_list("id", flat=True) index_resources(resource_ids) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) self.assertEqual( json.loads(resp.content.decode('utf-8'))['count'], len(resource_ids) )
def handle(self, *args, **options): """Refreshes the Elasticsearch index.""" create_mapping() index_resources(LearningResource.objects.values_list("id", flat=True))