1919from meilisearch .models .task import TaskInfo
2020from opaque_keys import OpaqueKey
2121from opaque_keys .edx .keys import UsageKey
22- from opaque_keys .edx .locator import LibraryContainerLocator , LibraryLocatorV2 , LibraryCollectionLocator
22+ from opaque_keys .edx .locator import LibraryCollectionLocator , LibraryContainerLocator , LibraryLocatorV2
2323from openedx_learning .api import authoring as authoring_api
24- from common .djangoapps .student .roles import GlobalStaff
2524from rest_framework .request import Request
25+
2626from common .djangoapps .student .role_helpers import get_course_roles
27+ from common .djangoapps .student .roles import GlobalStaff
2728from openedx .core .djangoapps .content .course_overviews .models import CourseOverview
28- from openedx .core .djangoapps .content .search .models import get_access_ids_for_request , IncrementalIndexCompleted
2929from openedx .core .djangoapps .content .search .index_config import (
3030 INDEX_DISTINCT_ATTRIBUTE ,
3131 INDEX_FILTERABLE_ATTRIBUTES ,
32- INDEX_SEARCHABLE_ATTRIBUTES ,
33- INDEX_SORTABLE_ATTRIBUTES ,
3432 INDEX_RANKING_RULES ,
33+ INDEX_SEARCHABLE_ATTRIBUTES ,
34+ INDEX_SORTABLE_ATTRIBUTES
3535)
36+ from openedx .core .djangoapps .content .search .models import IncrementalIndexCompleted , get_access_ids_for_request
3637from openedx .core .djangoapps .content_libraries import api as lib_api
3738from xmodule .modulestore .django import modulestore
3839
3940from .documents import (
4041 Fields ,
4142 meili_id_from_opaque_key ,
42- searchable_doc_for_course_block ,
43+ searchable_doc_collections ,
4344 searchable_doc_for_collection ,
4445 searchable_doc_for_container ,
46+ searchable_doc_for_course_block ,
4547 searchable_doc_for_library_block ,
4648 searchable_doc_for_key ,
4749 searchable_doc_collections ,
4850 searchable_doc_tags ,
49- searchable_doc_tags_for_collection ,
51+ searchable_doc_tags_for_collection
5052)
5153
5254log = logging .getLogger (__name__ )
@@ -488,6 +490,7 @@ def index_container_batch(batch, num_done, library_key) -> int:
488490 )
489491 doc = searchable_doc_for_container (container_key )
490492 doc .update (searchable_doc_tags (container_key ))
493+ doc .update (searchable_doc_collections (container_key ))
491494 docs .append (doc )
492495 except Exception as err : # pylint: disable=broad-except
493496 status_cb (f"Error indexing container { container .key } : { err } " )
@@ -710,15 +713,15 @@ def upsert_library_collection_index_doc(library_key: LibraryLocatorV2, collectio
710713 If the Collection is not found or disabled (i.e. soft-deleted), then delete it from the search index.
711714 """
712715 doc = searchable_doc_for_collection (library_key , collection_key )
713- update_components = False
716+ update_items = False
714717
715718 # Soft-deleted/disabled collections are removed from the index
716719 # and their components updated.
717720 if doc .get ('_disabled' ):
718721
719722 _delete_index_doc (doc [Fields .id ])
720723
721- update_components = True
724+ update_items = True
722725
723726 # Hard-deleted collections are also deleted from the index,
724727 # but their components are automatically updated as part of the deletion process, so we don't have to.
@@ -736,10 +739,12 @@ def upsert_library_collection_index_doc(library_key: LibraryLocatorV2, collectio
736739 _update_index_docs ([doc ])
737740
738741 # Asynchronously update the collection's components "collections" field
739- if update_components :
740- from .tasks import update_library_components_collections as update_task
742+ if update_items :
743+ from .tasks import update_library_components_collections as update_components_task
744+ from .tasks import update_library_components_collections as update_containers_task
741745
742- update_task .delay (str (library_key ), collection_key )
746+ update_components_task .delay (str (library_key ), collection_key )
747+ update_containers_task .delay (str (library_key ), collection_key )
743748
744749
745750def update_library_components_collections (
@@ -774,6 +779,38 @@ def update_library_components_collections(
774779 _update_index_docs (docs )
775780
776781
782+ def update_library_containers_collections (
783+ library_key : LibraryLocatorV2 ,
784+ collection_key : str ,
785+ batch_size : int = 1000 ,
786+ ) -> None :
787+ """
788+ Updates the "collections" field for all containers associated with a given Library Collection.
789+
790+ Because there may be a lot of containers, we send these updates to Meilisearch in batches.
791+ """
792+ library = lib_api .get_library (library_key )
793+ containers = authoring_api .get_collection_containers (library .learning_package_id , collection_key )
794+
795+ paginator = Paginator (containers , batch_size )
796+ for page in paginator .page_range :
797+ docs = []
798+
799+ for container in paginator .page (page ).object_list :
800+ container_key = lib_api .library_container_locator (
801+ library_key ,
802+ container ,
803+ )
804+ doc = searchable_doc_collections (container_key )
805+ docs .append (doc )
806+
807+ log .info (
808+ f"Updating document.collections for library { library_key } containers"
809+ f" page { page } / { paginator .num_pages } "
810+ )
811+ _update_index_docs (docs )
812+
813+
777814def upsert_library_container_index_doc (container_key : LibraryContainerLocator ) -> None :
778815 """
779816 Creates, updates, or deletes the document for the given Library Container in the search index.
@@ -820,12 +857,12 @@ def upsert_content_object_tags_index_doc(key: OpaqueKey):
820857 _update_index_docs ([doc ])
821858
822859
823- def upsert_block_collections_index_docs ( usage_key : UsageKey ):
860+ def upsert_item_collections_index_docs ( opaque_key : OpaqueKey ):
824861 """
825- Updates the collections data in documents for the given Course/Library block
862+ Updates the collections data in documents for the given Course/Library block, or Container
826863 """
827- doc = {Fields .id : meili_id_from_opaque_key (usage_key )}
828- doc .update (searchable_doc_collections (usage_key ))
864+ doc = {Fields .id : meili_id_from_opaque_key (opaque_key )}
865+ doc .update (searchable_doc_collections (opaque_key ))
829866 _update_index_docs ([doc ])
830867
831868
0 commit comments