Skip to content

Commit

Permalink
Ignore name of index when fetching settings
Browse files Browse the repository at this point in the history
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 /<index>/_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é <beauleetienne0@gmail.com>
  • Loading branch information
tienne-B committed Sep 18, 2024
1 parent a24b9f3 commit 263b708
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/helpers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"][

Check warning on line 329 in opensearchpy/helpers/index.py

View check run for this annotation

Codecov / codecov/patch

opensearchpy/helpers/index.py#L329

Added line #L329 was not covered by tests
"index"
]
if analysis:
Expand Down
15 changes: 15 additions & 0 deletions test_opensearchpy/test_server/test_helpers/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ 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"]
)

0 comments on commit 263b708

Please sign in to comment.