Skip to content
Merged
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
26 changes: 16 additions & 10 deletions frontends/api/src/generated/api.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions learning_resources_search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DEPARTMENT_QUERY_FIELDS,
LEARNING_RESOURCE_QUERY_FIELDS,
LEARNING_RESOURCE_SEARCH_FILTERS,
LEARNING_RESOURCE_SORTBY_OPTIONS,
LEARNING_RESOURCE_TYPES,
RESOURCEFILE_QUERY_FIELDS,
RUN_INSTRUCTORS_QUERY_FIELDS,
Expand All @@ -22,6 +23,7 @@
)

LEARN_SUGGEST_FIELDS = ["title.trigram", "description.trigram"]
COURSENUM_SORT_FIELD = "course.course_numbers.sort_coursenum"


def gen_content_file_id(content_file_id):
Expand Down Expand Up @@ -62,16 +64,21 @@ def relevant_indexes(resource_types, aggregations):
return map(get_default_alias_name, set(resource_types_copy))


def generate_sort_clause(sort):
def generate_sort_clause(search_params):
"""
Return sort clause for the query

Args:
sort (string): the sort parameter
sort (dict): the search params
Returns:
dict or String: either a dictionary with the sort clause for
nested sort params or just sort parameter
"""
sort = LEARNING_RESOURCE_SORTBY_OPTIONS.get(search_params.get("sortby"), {}).get(
"sort"
)

departments = search_params.get("department")

if "." in sort:
if sort.startswith("-"):
Expand All @@ -83,7 +90,26 @@ def generate_sort_clause(sort):

path = ".".join(field.split(".")[:-1])

return {field: {"order": direction, "nested": {"path": path}}}
sort_filter = {}
if field == COURSENUM_SORT_FIELD:
if departments:
sort_filter = {
"filter": {
"bool": {
"should": [
{
"term": {
f"{path}.department.department_id": department
}
}
for department in departments
]
}
}
}
else:
sort_filter = {"filter": {"term": {f"{path}.primary": True}}}
return {field: {"order": direction, "nested": {"path": path, **sort_filter}}}

else:
return sort
Expand Down Expand Up @@ -439,7 +465,7 @@ def execute_learn_search(search_params):
search = search.extra(size=search_params.get("limit"))

if search_params.get("sortby"):
sort = generate_sort_clause(search_params.get("sortby"))
sort = generate_sort_clause(search_params)

search = search.sort(sort)

Expand Down
94 changes: 75 additions & 19 deletions learning_resources_search/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,70 @@ def test_relevant_indexes(resourse_types, aggregations, result):


@pytest.mark.parametrize(
("sort_param", "result"),
("sort_param", "departments", "result"),
[
("prices", "prices"),
("-prices", "-prices"),
("id", None, "id"),
("-id", ["7"], "-id"),
(
"runs.start_date",
"start_date",
["5"],
{"runs.start_date": {"order": "asc", "nested": {"path": "runs"}}},
),
(
"-runs.start_date",
"-start_date",
None,
{"runs.start_date": {"order": "desc", "nested": {"path": "runs"}}},
),
(
"mitcoursenumber",
None,
{
"course.course_numbers.sort_coursenum": {
"order": "asc",
"nested": {
"path": "course.course_numbers",
"filter": {"term": {"course.course_numbers.primary": True}},
},
}
},
),
(
"mitcoursenumber",
["7", "5"],
{
"course.course_numbers.sort_coursenum": {
"order": "asc",
"nested": {
"path": "course.course_numbers",
"filter": {
"bool": {
"should": [
{
"term": {
"course.course_numbers.department.department_id": (
"7"
)
}
},
{
"term": {
"course.course_numbers.department.department_id": (
"5"
)
}
},
]
}
},
},
}
},
),
],
)
def test_generate_sort_clause(sort_param, result):
assert generate_sort_clause(sort_param) == result
def test_generate_sort_clause(sort_param, departments, result):
params = {"sortby": sort_param, "department": departments}
assert generate_sort_clause(params) == result


def test_generate_learning_resources_text_clause():
Expand Down Expand Up @@ -93,7 +141,9 @@ def test_generate_learning_resources_text_clause():
"query": {
"multi_match": {
"query": "math",
"fields": ["departments.name"],
"fields": [
"departments.department_id"
],
}
},
}
Expand Down Expand Up @@ -208,7 +258,7 @@ def test_generate_learning_resources_text_clause():
"query": {
"multi_match": {
"query": "math",
"fields": ["departments.name"],
"fields": ["departments.department_id"],
}
},
}
Expand Down Expand Up @@ -323,7 +373,9 @@ def test_generate_learning_resources_text_clause():
"query": {
"query_string": {
"query": '"math"',
"fields": ["departments.name"],
"fields": [
"departments.department_id"
],
}
},
}
Expand Down Expand Up @@ -441,7 +493,7 @@ def test_generate_learning_resources_text_clause():
"query": {
"query_string": {
"query": '"math"',
"fields": ["departments.name"],
"fields": ["departments.department_id"],
}
},
}
Expand Down Expand Up @@ -545,7 +597,9 @@ def test_generate_content_file_text_clause():
"query": {
"multi_match": {
"query": "math",
"fields": ["departments.name"],
"fields": [
"departments.department_id"
],
}
},
}
Expand Down Expand Up @@ -574,7 +628,7 @@ def test_generate_content_file_text_clause():
"query": {
"multi_match": {
"query": "math",
"fields": ["departments.name"],
"fields": ["departments.department_id"],
}
},
}
Expand Down Expand Up @@ -607,7 +661,9 @@ def test_generate_content_file_text_clause():
"query": {
"query_string": {
"query": '"math"',
"fields": ["departments.name"],
"fields": [
"departments.department_id"
],
}
},
}
Expand Down Expand Up @@ -636,7 +692,7 @@ def test_generate_content_file_text_clause():
"query": {
"query_string": {
"query": '"math"',
"fields": ["departments.name"],
"fields": ["departments.department_id"],
}
},
}
Expand Down Expand Up @@ -804,7 +860,7 @@ def test_execute_learn_search(opensearch):
"resource_type": ["course"],
"limit": 1,
"offset": 1,
"sortby": "prices",
"sortby": "-readable_id",
}

query = {
Expand Down Expand Up @@ -857,7 +913,7 @@ def test_execute_learn_search(opensearch):
"multi_match": {
"query": "math",
"fields": [
"departments.name"
"departments.department_id"
],
}
},
Expand Down Expand Up @@ -979,7 +1035,7 @@ def test_execute_learn_search(opensearch):
"query": {
"multi_match": {
"query": "math",
"fields": ["departments.name"],
"fields": ["departments.department_id"],
}
},
}
Expand Down Expand Up @@ -1083,7 +1139,7 @@ def test_execute_learn_search(opensearch):
]
}
},
"sort": ["prices"],
"sort": [{"readable_id": {"order": "desc"}}],
"from": 1,
"size": 1,
"suggest": {
Expand Down
Loading