Skip to content

Commit

Permalink
Extract build_query to clean up query_media
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Nov 13, 2023
1 parent cf7aa4b commit 5728670
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions api/api/controllers/search_controller.py
Expand Up @@ -352,6 +352,16 @@ def build_collection_query(
return Q("bool", **search_query)


def build_query(
strategy: SearchStrategy,
search_params: MediaListRequestSerializer,
collection_params: dict[str, str] | None,
) -> Q:
if strategy == "collection":
return build_collection_query(search_params, collection_params)
return build_search_query(search_params)


def query_media(
strategy: SearchStrategy,
search_params: MediaListRequestSerializer,
Expand Down Expand Up @@ -388,10 +398,7 @@ def query_media(
"""
index = get_index(exact_index, origin_index, search_params)

if strategy == "collection":
query = build_collection_query(search_params, collection_params)
else:
query = build_search_query(search_params)
query = build_query(strategy, search_params, collection_params)

s = Search(index=index).query(query)

Expand All @@ -409,8 +416,8 @@ def query_media(

# Sort by `created_on` if the parameter is set or if `strategy` is `collection`.
sort_by = search_params.validated_data.get("sort_by")
sort_dir = search_params.validated_data.get("sort_dir", "desc")
if strategy == "collection" or sort_by == INDEXED_ON:
sort_dir = search_params.validated_data.get("sort_dir", "desc")
s = s.sort({"created_on": {"order": sort_dir}})

# Execute paginated search and tally results
Expand Down

0 comments on commit 5728670

Please sign in to comment.