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

Add get and update meta methods to ESIndex #1896

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions connectors/es/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ async def get_all_docs(self, query=None, sort=None, page_size=DEFAULT_PAGE_SIZE)
if count >= total:
break
offset += len(hits)

async def meta(self):
response = await self.client.indices.get_mapping(index=self.index_name)
# Assume index name is the same pattern and the latest index is always the one with the largest suffix
latest_index = max(response.keys())
return response[latest_index].get("mappings", {}).get("_meta")
Comment on lines +131 to +135
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember, that calls to get_mappings are very expensive on loaded clusters.

How often do we intend to do the calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About every one hour (by the default interval)

I vaguely remember, that calls to get_mappings are very expensive on loaded clusters.

This is new to me, 🤔 get_mappings will only try to get the mappings of the index, not the docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_mappings will only try to get the mappings of the index, not the docs.

True, but afaik it's one of operations that are "stop the world" and block other operations running on this index (or cluster?).

I just highlight it as something to double check with Elasticsearch team


async def update_meta(self, meta):
await self.client.indices.put_mapping(
index=self.index_name, meta=meta, write_index_only=True
)
45 changes: 45 additions & 0 deletions tests/es/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,48 @@ async def test_get_all_docs(mock_responses):
assert doc_count == total

await index.close()


@pytest.mark.asyncio
@pytest.mark.parametrize(
"mappings, expected_meta",
[
({".elastic-connectors-v1": {"mappings": {}}}, None),
(
{".elastic-connectors-v1": {"mappings": {"_meta": {"foo": "bar"}}}},
{"foo": "bar"},
),
(
{
".elastic-connectors-v1": {"mappings": {"_meta": {"foo": "bar"}}},
".elastic-connectors-v2": {"mappings": {"_meta": {"baz": "qux"}}},
},
{"baz": "qux"},
),
],
)
async def test__meta(mappings, expected_meta, mock_responses):
mock_responses.get(
"http://nowhere.com:9200/fake_index/_mapping",
payload=mappings,
headers=headers,
)

index = ESIndex(index_name, config)

meta = await index.meta()
assert meta == expected_meta
await index.close()


@pytest.mark.asyncio
async def test_update_meta(mock_responses):
meta = {"foo": "bar"}
mock_responses.put(
"http://nowhere.com:9200/fake_index/_mapping?write_index_only=true",
payload={"_meta": meta},
headers=headers,
)
index = ESIndex(index_name, config)
await index.update_meta(meta)
await index.close()