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

Fix reindex command and some es utils #35463

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 29 additions & 19 deletions corehq/apps/es/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ def _validate_single_index(index):

def reindex(
self, source, dest, wait_for_completion=False,
refresh=False, batch_size=1000, requests_per_second=None, copy_doc_ids=True, query=None,
refresh=False, batch_size=1000, requests_per_second=None,
copy_doc_ids=True, query=None, purge_ids=False
):
"""
Starts the reindex process in elastic search cluster
Expand All @@ -416,6 +417,7 @@ def reindex(
and can be reduced if you encounter scroll timeouts.
:param query: ``dict`` optional parameter to include a term query to filter which documents are included in
the reindex
:param purge_ids: ``bool`` if True, will remove the _id field from the documents
:returns: None if wait_for_completion is True else would return task_id of reindex task
"""

Expand All @@ -435,27 +437,35 @@ def reindex(
"conflicts": "proceed"
}

# Should be removed after ES 5-6 migration
if copy_doc_ids and source == const.HQ_USERS_INDEX_NAME:
# Remove password from form index
if copy_doc_ids or purge_ids:
reindex_body["script"] = {
"lang": "painless",
"source": """
ctx._source.remove('password');
if (!ctx._source.containsKey('doc_id')) {
ctx._source['doc_id'] = ctx._id;
}
"""
}
elif copy_doc_ids:
reindex_body["script"] = {
"lang": "painless",
"source": """
if (!ctx._source.containsKey('doc_id')) {
ctx._source['doc_id'] = ctx._id;
}
"""
"source": ""
}
script_parts = []

if purge_ids:
script_parts.append("""
if (ctx._source.containsKey('_id')) {
ctx._source.remove('_id');
}
""")

if source == const.HQ_USERS_INDEX_NAME:
# Remove password field from users index
script_parts.append("""
ctx._source.remove('password');
""")

if copy_doc_ids:
# Add doc_id field to the documents
script_parts.append("""
if (!ctx._source.containsKey('doc_id')) {
ctx._source['doc_id'] = ctx._id;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work if purge_ids is true?

It's referencing ctx._id, which may have been removed by ctx._source.remove('_id');? I'm not confident about what ctx._source.remove() does, so I could be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It will work if purge_ids is true.

We are removing _id from ctx._source which is different from ctx._id. ctx._id is a read only property.

ctx._source.remove is basically a groovy style syntax to remove an object from a map. So it is just removing the attribute from the object.

}
""")

reindex_body["script"]["source"] = " ".join(script_parts)

reindex_kwargs = {
"wait_for_completion": wait_for_completion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ESSyncUtil:
def __init__(self):
self.es = get_client()

def start_reindex(self, cname, reindex_batch_size=1000, requests_per_second=None):
def start_reindex(self, cname, reindex_batch_size=1000, requests_per_second=None, purge_ids=False):

adapter = doc_adapter_from_cname(cname)

Expand All @@ -58,7 +58,7 @@ def start_reindex(self, cname, reindex_batch_size=1000, requests_per_second=None
logger.info("Starting ReIndex process")
task_id = es_manager.reindex(
source_index, destination_index,
requests_per_second=requests_per_second, batch_size=reindex_batch_size
requests_per_second=requests_per_second, batch_size=reindex_batch_size, purge_ids=purge_ids
)
logger.info(f"Copying docs from index {source_index} to index {destination_index}")
task_number = task_id.split(':')[1]
Expand Down