Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Fix divergent Elasticsearch configurations
Browse files Browse the repository at this point in the history
In #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.
  • Loading branch information
sarayourfriend committed Aug 5, 2022
1 parent c89250a commit d5697df
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
34 changes: 1 addition & 33 deletions api/catalog/api/controllers/search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand All @@ -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]:
Expand Down
3 changes: 2 additions & 1 deletion api/catalog/api/models/audio.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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}})
Expand Down
3 changes: 2 additions & 1 deletion api/catalog/api/models/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.conf import settings

from uuslug import uuslug

Expand Down Expand Up @@ -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}})
Expand Down
3 changes: 2 additions & 1 deletion api/catalog/api/models/media.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d5697df

Please sign in to comment.