From 820b1cc15384741757082d3b23441db24b3c4dd9 Mon Sep 17 00:00:00 2001 From: Ivo Branco Date: Thu, 24 Feb 2022 15:55:25 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(search)=20add=20elastic=20search?= =?UTF-8?q?=20client=20custom=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add settings that permit to configure elastic search client with custom configuration like custom timeouts. Closes #1613 --- CHANGELOG.md | 5 +++++ sandbox/settings.py | 4 ++++ src/richie/apps/search/__init__.py | 12 +++++++++--- tests/apps/search/test_index_client.py | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/apps/search/test_index_client.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 968f615820..6a52902f89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ Versioning](https://semver.org/spec/v2.0.0.html). ## [Unrealeased] +## Added + +- Add settings that permit to configure elastic search client with custom + configuration like custom timeouts. + ## [2.15.0] - 2022-06-22 ### Added diff --git a/sandbox/settings.py b/sandbox/settings.py index 22278c47b0..78107488a6 100644 --- a/sandbox/settings.py +++ b/sandbox/settings.py @@ -271,6 +271,10 @@ class Base(StyleguideMixin, DRFMixin, RichieCoursesConfigurationMixin, Configura default="richie", environ_name="RICHIE_ES_INDICES_PREFIX", environ_prefix=None ) RICHIE_ES_STATE_WEIGHTS = values.ListValue(None) + # Example to change the timeout: RICHIE_ES_CLIENT_KWARGS={'timeout': 30} + RICHIE_ES_CLIENT_KWARGS = values.DictValue( + {}, environ_name="RICHIE_ES_CLIENT_KWARGS", environ_prefix=None + ) # LTI Content RICHIE_LTI_PROVIDERS = { diff --git a/src/richie/apps/search/__init__.py b/src/richie/apps/search/__init__.py index a797526476..cd316016b0 100644 --- a/src/richie/apps/search/__init__.py +++ b/src/richie/apps/search/__init__.py @@ -9,8 +9,14 @@ # pylint: disable=invalid-name default_app_config = "richie.apps.search.apps.SearchConfig" -ES_CLIENT = ElasticsearchClientCompat7to6( - getattr(settings, "RICHIE_ES_HOST", ["elasticsearch"]) -) + +def new_elasticsearch_client(): + return ElasticsearchClientCompat7to6( + getattr(settings, "RICHIE_ES_HOST", ["elasticsearch"]), + **getattr(settings, "RICHIE_ES_CLIENT_KWARGS", {}), + ) + + +ES_CLIENT = new_elasticsearch_client() ES_INDICES_CLIENT = ElasticsearchIndicesClientCompat7to6(ES_CLIENT) diff --git a/tests/apps/search/test_index_client.py b/tests/apps/search/test_index_client.py new file mode 100644 index 0000000000..2a76693a1d --- /dev/null +++ b/tests/apps/search/test_index_client.py @@ -0,0 +1,18 @@ +from django.test import TestCase +from django.test.utils import override_settings +from richie.apps.search import new_elasticsearch_client + + +class IndexClientTestCase(TestCase): + """ + Test the index client extra vars + """ + + @override_settings(RICHIE_ES_CLIENT_KWARGS={"timeout": 99}) + def test_index_client_kwargs(self): + """ + Test `RICHIE_ES_CLIENT_KWARGS` setting, that allows to pass extra configurations to the + elastic search index client. + """ + es_client_to_test = new_elasticsearch_client() + self.assertEqual(es_client_to_test.transport.kwargs, {"timeout": 99})