Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧(search) add elastic search client custom settings #1615

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions sandbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
15 changes: 12 additions & 3 deletions src/richie/apps/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
# 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():
"""
Utility function to allow to test the `RICHIE_ES_CLIENT_KWARGS` setting.
"""
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)
22 changes: 22 additions & 0 deletions tests/apps/search/test_index_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Test the index client initialization.
"""
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.
"""

@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})