Skip to content

Commit

Permalink
feat: add usage_key to breadcrumbs in Studio search index (openedx#34535
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rpenido authored Apr 25, 2024
1 parent f51c161 commit 7fc6cd6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
11 changes: 10 additions & 1 deletion openedx/core/djangoapps/content/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ class implementation returns only:
# this would be very inefficient. Better to recurse the tree top-down with the parent blocks loaded.
log.warning(f"Updating Studio search index for XBlock {block.usage_key} but ancestors weren't cached.")
cur_block = cur_block.get_parent()
block_data[Fields.breadcrumbs].insert(0, {"display_name": xblock_api.get_block_display_name(cur_block)})
parent_data = {
"display_name": xblock_api.get_block_display_name(cur_block),
}
if cur_block.scope_ids.block_type != "course":
parent_data["usage_key"] = str(cur_block.usage_key)
block_data[Fields.breadcrumbs].insert(
0,
parent_data,
)
try:
content_data = block.index_dictionary()
# Will be something like:
Expand Down Expand Up @@ -220,6 +228,7 @@ def searchable_doc_for_library_block(xblock_metadata: lib_api.LibraryXBlockMetad
doc = {
Fields.id: meili_id_from_opaque_key(xblock_metadata.usage_key),
Fields.type: DocType.library_block,
Fields.breadcrumbs: []
}

doc.update(_fields_from_block(block))
Expand Down
57 changes: 33 additions & 24 deletions openedx/core/djangoapps/content/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,43 @@ def setUp(self):
# Create XBlocks
self.sequential = self.store.create_child(self.user_id, self.course.location, "sequential", "test_sequential")
self.doc_sequential = {
'id': 'block-v1org1test_coursetest_runtypesequentialblocktest_sequential-f702c144',
'type': 'course_block',
'usage_key': 'block-v1:org1+test_course+test_run+type@sequential+block@test_sequential',
'block_id': 'test_sequential',
'display_name': 'sequential',
'block_type': 'sequential',
'context_key': 'course-v1:org1+test_course+test_run',
'org': 'org1',
'breadcrumbs': [{'display_name': 'Test Course'}],
'content': {},
'access_id': course_access.id,
"id": "block-v1org1test_coursetest_runtypesequentialblocktest_sequential-f702c144",
"type": "course_block",
"usage_key": "block-v1:org1+test_course+test_run+type@sequential+block@test_sequential",
"block_id": "test_sequential",
"display_name": "sequential",
"block_type": "sequential",
"context_key": "course-v1:org1+test_course+test_run",
"org": "org1",
"breadcrumbs": [
{
"display_name": "Test Course",
},
],
"content": {},
"access_id": course_access.id,
}
self.store.create_child(self.user_id, self.sequential.location, "vertical", "test_vertical")
self.doc_vertical = {
'id': 'block-v1org1test_coursetest_runtypeverticalblocktest_vertical-e76a10a4',
'type': 'course_block',
'usage_key': 'block-v1:org1+test_course+test_run+type@vertical+block@test_vertical',
'block_id': 'test_vertical',
'display_name': 'vertical',
'block_type': 'vertical',
'context_key': 'course-v1:org1+test_course+test_run',
'org': 'org1',
'breadcrumbs': [
{'display_name': 'Test Course'},
{'display_name': 'sequential'}
"id": "block-v1org1test_coursetest_runtypeverticalblocktest_vertical-e76a10a4",
"type": "course_block",
"usage_key": "block-v1:org1+test_course+test_run+type@vertical+block@test_vertical",
"block_id": "test_vertical",
"display_name": "vertical",
"block_type": "vertical",
"context_key": "course-v1:org1+test_course+test_run",
"org": "org1",
"breadcrumbs": [
{
"display_name": "Test Course",
},
{
"display_name": "sequential",
"usage_key": "block-v1:org1+test_course+test_run+type@sequential+block@test_sequential",
},
],
'content': {},
'access_id': course_access.id,
"content": {},
"access_id": course_access.id,
}

# Create a content library:
Expand Down
42 changes: 33 additions & 9 deletions openedx/core/djangoapps/content/search/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,21 @@ def test_problem_block(self):
"access_id": self.toy_course_access_id,
"display_name": "Test Problem",
"breadcrumbs": [
{"display_name": "Toy Course"},
{"display_name": "chapter"},
{"display_name": "sequential"},
{"display_name": "vertical"},
{
'display_name': 'Toy Course',
},
{
'display_name': 'chapter',
'usage_key': 'block-v1:edX+toy+2012_Fall+type@chapter+block@vertical_container',
},
{
'display_name': 'sequential',
'usage_key': 'block-v1:edX+toy+2012_Fall+type@sequential+block@vertical_sequential',
},
{
'display_name': 'vertical',
'usage_key': 'block-v1:edX+toy+2012_Fall+type@vertical+block@vertical_test',
},
],
"content": {
"capa_content": "What is a test?",
Expand Down Expand Up @@ -133,9 +144,17 @@ def test_html_block(self):
"access_id": self.toy_course_access_id,
"display_name": "Text",
"breadcrumbs": [
{"display_name": "Toy Course"},
{"display_name": "Overview"},
{"display_name": "Toy Videos"},
{
'display_name': 'Toy Course',
},
{
'display_name': 'Overview',
'usage_key': 'block-v1:edX+toy+2012_Fall+type@chapter+block@Overview',
},
{
"display_name": "Toy Videos",
"usage_key": "block-v1:edX+toy+2012_Fall+type@sequential+block@Toy_Videos",
},
],
"content": {
"html_content": (
Expand Down Expand Up @@ -168,8 +187,13 @@ def test_video_block_untagged(self):
"access_id": self.toy_course_access_id,
"display_name": "Welcome",
"breadcrumbs": [
{"display_name": "Toy Course"},
{"display_name": "Overview"},
{
'display_name': 'Toy Course',
},
{
'display_name': 'Overview',
'usage_key': 'block-v1:edX+toy+2012_Fall+type@chapter+block@Overview',
},
],
"content": {},
# This video has no tags.
Expand Down
17 changes: 15 additions & 2 deletions openedx/core/djangoapps/content/search/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def test_create_delete_xblock(self, meilisearch_client):
"block_type": "sequential",
"context_key": "course-v1:orgA+test_course+test_run",
"org": "orgA",
"breadcrumbs": [{"display_name": "Test Course"}], "content": {},
"breadcrumbs": [
{
"display_name": "Test Course",
},
],
"content": {},
"access_id": course_access.id,

}
Expand All @@ -87,7 +92,15 @@ def test_create_delete_xblock(self, meilisearch_client):
"block_type": "vertical",
"context_key": "course-v1:orgA+test_course+test_run",
"org": "orgA",
"breadcrumbs": [{"display_name": "Test Course"}, {"display_name": "sequential"}],
"breadcrumbs": [
{
"display_name": "Test Course",
},
{
"display_name": "sequential",
"usage_key": "block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential",
},
],
"content": {},
"access_id": course_access.id,
}
Expand Down

0 comments on commit 7fc6cd6

Please sign in to comment.