From 2ed7b6e037bbcc43353f3741e071940dc7e0b122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Beaul=C3=A9?= Date: Tue, 17 Sep 2024 23:29:39 -0300 Subject: [PATCH] Ignore name of index when fetching settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a situation where an index cannot be updated through this client as the index is aliased, with the client pointing to the alias. As the `GET //_settings` request will only ever return the settings for the specified index (through the alias), it would only have one key, so the name of the key would not matter. We can pop the key to get the settings object for the index through the alias. Signed-off-by: Étienne Beaulé --- CHANGELOG.md | 1 + opensearchpy/helpers/index.py | 2 +- .../test_server/test_helpers/test_index.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 672fd0653..4a1a45cd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed ### Fixed - Fix `Transport.perform_request`'s arguments `timeout` and `ignore` variable usage ([810](https://github.com/opensearch-project/opensearch-py/pull/810)) +- Fix `Index.save` not passing through aliases to the underlying index ([823](https://github.com/opensearch-project/opensearch-py/pull/823)) ### Security ### Dependencies diff --git a/opensearchpy/helpers/index.py b/opensearchpy/helpers/index.py index 93b860080..389b3e936 100644 --- a/opensearchpy/helpers/index.py +++ b/opensearchpy/helpers/index.py @@ -326,7 +326,7 @@ def save(self, using: Optional[OpenSearch] = None) -> Any: body = self.to_dict() settings = body.pop("settings", {}) analysis = settings.pop("analysis", None) - current_settings = self.get_settings(using=using)[self._name]["settings"][ + current_settings = self.get_settings(using=using).popitem()[1]["settings"][ "index" ] if analysis: diff --git a/test_opensearchpy/test_server/test_helpers/test_index.py b/test_opensearchpy/test_server/test_helpers/test_index.py index 5b8250b42..f33597699 100644 --- a/test_opensearchpy/test_server/test_helpers/test_index.py +++ b/test_opensearchpy/test_server/test_helpers/test_index.py @@ -122,3 +122,21 @@ def test_multiple_indices_with_same_doc_type_work(write_client: Any) -> None: assert settings[j]["settings"]["index"]["analysis"] == { "analyzer": {"my_analyzer": {"type": "custom", "tokenizer": "keyword"}} } + + +def test_index_can_be_saved_through_alias_with_settings(write_client: Any) -> None: + raw_index = Index("test-blog", using=write_client) + raw_index.settings(number_of_shards=3, number_of_replicas=0) + raw_index.aliases(**{"blog-alias": {}}) + raw_index.save() + + i = Index("blog-alias", using=write_client) + i.settings(number_of_replicas=1) + i.save() + + assert ( + "1" + == raw_index.get_settings()["test-blog"]["settings"]["index"][ + "number_of_replicas" + ] + )