From d5697dfe8f52c60f533544b6b91a1a90c7d51bb7 Mon Sep 17 00:00:00 2001 From: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com> Date: Fri, 5 Aug 2022 11:02:54 -0400 Subject: [PATCH] Fix divergent Elasticsearch configurations In https://github.com/WordPress/openverse-api/pull/712 Elasticsearch configuration was moved out of the search controller area into the Django settings module. This unifies access to ES from across the application and is part of the refactoring being done to support the ES versioned index, multi-stage deployment. This commit keeps those changes and applies them to the old search_controller.py module that did not exist at the time the configuration change was applied. --- .../api/controllers/search_controller.py | 34 +------------------ api/catalog/api/models/audio.py | 3 +- api/catalog/api/models/image.py | 3 +- api/catalog/api/models/media.py | 3 +- 4 files changed, 7 insertions(+), 36 deletions(-) diff --git a/api/catalog/api/controllers/search_controller.py b/api/catalog/api/controllers/search_controller.py index 86b2a9cd8..045238c86 100644 --- a/api/catalog/api/controllers/search_controller.py +++ b/api/catalog/api/controllers/search_controller.py @@ -403,7 +403,7 @@ def get_sources(index): } } try: - results = es.search(index=index, body=agg_body, request_cache=True) + results = settings.ES.search(index=index, body=agg_body, request_cache=True) buckets = results["aggregations"]["unique_sources"]["buckets"] except NotFoundError: buckets = [{"key": "none_found", "doc_count": 0}] @@ -412,38 +412,6 @@ def get_sources(index): return sources -def _elasticsearch_connect(): - """ - Connect to configured Elasticsearch domain. - - :return: An Elasticsearch connection object. - """ - auth = AWSRequestsAuth( - aws_access_key=settings.AWS_ACCESS_KEY_ID, - aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, - aws_host=settings.ELASTICSEARCH_URL, - aws_region=settings.ELASTICSEARCH_AWS_REGION, - aws_service="es", - ) - auth.encode = lambda x: bytes(x.encode("utf-8")) - _es = Elasticsearch( - host=settings.ELASTICSEARCH_URL, - port=settings.ELASTICSEARCH_PORT, - connection_class=RequestsHttpConnection, - timeout=10, - max_retries=1, - retry_on_timeout=True, - http_auth=auth, - wait_for_status="yellow", - ) - _es.info() - return _es - - -es = _elasticsearch_connect() -connections.connections.add_connection("default", es) - - def _get_result_and_page_count( response_obj: Response, results: List[Hit], page_size: int ) -> Tuple[int, int]: diff --git a/api/catalog/api/models/audio.py b/api/catalog/api/models/audio.py index 9e8e269d1..e9991e786 100644 --- a/api/catalog/api/models/audio.py +++ b/api/catalog/api/models/audio.py @@ -1,5 +1,6 @@ from django.contrib.postgres.fields import ArrayField from django.db import models +from django.conf import settings from uuslug import uuslug @@ -250,7 +251,7 @@ class MatureAudio(AbstractMatureMedia): """Stores all audios that have been flagged as 'mature'.""" def delete(self, *args, **kwargs): - es = search_controller.es + es = settings.ES aud = Audio.objects.get(identifier=self.identifier) es_id = aud.id es.update(index="audio", id=es_id, body={"doc": {"mature": False}}) diff --git a/api/catalog/api/models/image.py b/api/catalog/api/models/image.py index 009fde502..52fd35469 100644 --- a/api/catalog/api/models/image.py +++ b/api/catalog/api/models/image.py @@ -1,4 +1,5 @@ from django.db import models +from django.conf import settings from uuslug import uuslug @@ -77,7 +78,7 @@ class MatureImage(AbstractMatureMedia): """Stores all images that have been flagged as 'mature'.""" def delete(self, *args, **kwargs): - es = search_controller.es + es = settings.ES img = Image.objects.get(identifier=self.identifier) es_id = img.id es.update(index="image", id=es_id, body={"doc": {"mature": False}}) diff --git a/api/catalog/api/models/media.py b/api/catalog/api/models/media.py index 7ca538b36..8bd349c0c 100644 --- a/api/catalog/api/models/media.py +++ b/api/catalog/api/models/media.py @@ -1,5 +1,6 @@ import mimetypes +from django.conf import settings from django.contrib.postgres.fields import ArrayField from django.db import models from django.utils.html import format_html @@ -189,7 +190,7 @@ def save(self, *args, **kwargs): update_required = {MATURE_FILTERED, DEINDEXED} # ES needs updating if self.status in update_required: - es = search_controller.es + es = settings.ES try: media = media_class.objects.get(identifier=self.identifier) except media_class.DoesNotExist: