From 6cf9e04f10f1d1cad810529dab3e48a45c2e42c7 Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Tue, 5 Mar 2024 14:38:19 -0800 Subject: [PATCH] fix: better handling index existence check, better comments --- .../content/search/management/commands/meili_mixin.py | 5 ++++- .../search/management/commands/reindex_studio.py | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/openedx/core/djangoapps/content/search/management/commands/meili_mixin.py b/openedx/core/djangoapps/content/search/management/commands/meili_mixin.py index 8fb85a598ef2..798dfd8a7e47 100644 --- a/openedx/core/djangoapps/content/search/management/commands/meili_mixin.py +++ b/openedx/core/djangoapps/content/search/management/commands/meili_mixin.py @@ -60,7 +60,10 @@ def index_exists(self, index_name: str) -> bool: try: client.get_index(index_name) except MeilisearchError as err: - return False + if err.code == "index_not_found": + return False + else: + raise err return True @contextmanager diff --git a/openedx/core/djangoapps/content/search/management/commands/reindex_studio.py b/openedx/core/djangoapps/content/search/management/commands/reindex_studio.py index 3b46370ebf21..fe0981e24ca4 100644 --- a/openedx/core/djangoapps/content/search/management/commands/reindex_studio.py +++ b/openedx/core/djangoapps/content/search/management/commands/reindex_studio.py @@ -47,16 +47,19 @@ def handle(self, *args, **options): with store.branch_setting(ModuleStoreEnum.Branch.draft_preferred): all_courses = store.get_courses() num_courses = len(all_courses) + + # Some counters so we can track our progress as indexing progresses: num_contexts = num_courses + num_libraries - num_contexts_done = 0 # How many courses/libraries we've done - num_blocks_done = 0 + num_contexts_done = 0 # How many courses/libraries we've indexed + num_blocks_done = 0 # How many individual components/XBlocks we've indexed self.stdout.write(f"Found {num_courses} courses and {num_libraries} libraries.") index_name = settings.MEILISEARCH_INDEX_PREFIX + STUDIO_INDEX_NAME with self.using_temp_index(index_name) as temp_index_name: ############## Configure the index ############## - # usage_key is not the primary key but nevertheless must be unique: + # Mark usage_key as unique (it's not the primary key for the index, but nevertheless must be unique): client.index(temp_index_name).update_distinct_attribute(Fields.usage_key) + # Mark which attributes can be used for filtering/faceted search: client.index(temp_index_name).update_filterable_attributes([ Fields.block_type, Fields.context_key, @@ -95,7 +98,7 @@ def add_with_children(block): self.recurse_children(course, add_with_children) - # Add all the docs in this library at once (usually faster than adding one at a time): + # Add all the docs in this course at once (usually faster than adding one at a time): self.wait_for_meili_task(client.index(temp_index_name).add_documents(docs)) num_contexts_done += 1 num_blocks_done += len(docs)