diff --git a/django/core/jinja_config.py b/django/core/jinja_config.py index 9c87d20bd..3af072f37 100644 --- a/django/core/jinja_config.py +++ b/django/core/jinja_config.py @@ -131,10 +131,7 @@ def generate_search_form_inputs(query_params): Returns: list: A list of tuples representing the hidden input fields. """ - # set default ordering to relevance, if it is not specified - search_parameters = { - "ordering": "relevance", - } + search_parameters = {} if query_params: # parse_qsl handles splitting and unquoting key-value pairs and generates a list of tuples # do not include the actual query in the query parameters diff --git a/django/core/view_helpers.py b/django/core/view_helpers.py index 1673d7d3e..e3371cf78 100644 --- a/django/core/view_helpers.py +++ b/django/core/view_helpers.py @@ -87,7 +87,13 @@ def build_search_query(input_text: str) -> SearchQuery: def get_search_queryset( - query, queryset, operator="or", fields=None, tags=None, criteria=None + query, + queryset, + operator="or", + fields=None, + tags=None, + criteria=None, + order_by_relevance=False, ): search_backend = get_search_backend() @@ -132,11 +138,6 @@ def get_search_queryset( Set up order by relevance override for search other sort orders are handled by SmallResultSetPagination filtering and SORT_BY_FILTERS """ - order_by_relevance = False - if "ordering" in criteria: - sort_order = criteria.pop("ordering") - if sort_order == "relevance": - order_by_relevance = True """ Filter queryset """ diff --git a/django/library/views.py b/django/library/views.py index 6b84c4477..2defb65c9 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -399,14 +399,16 @@ def filter_queryset(self, request, queryset, view): criteria.update(id__in=codebases.values_list("id", flat=True)) # or we could include the PL in the query # qs += " ".join(programming_languages) - if ordering: - criteria.update(ordering=ordering) - else: - if qs: - # set default ordering for search when ordering is not specified - criteria.update(ordering="relevance") - return get_search_queryset(qs, queryset, tags=tags, criteria=criteria) + # set order by relevance if there's a query string and no explicit ordering requested + order_by_relevance = qs and (not ordering or ordering == "relevance") + return get_search_queryset( + qs, + queryset, + tags=tags, + criteria=criteria, + order_by_relevance=order_by_relevance, + ) class CodebaseViewSet(SpamCatcherViewSetMixin, CommonViewSetMixin, HtmlNoDeleteViewSet): diff --git a/frontend/src/apps/codebase_list.ts b/frontend/src/apps/codebase_list.ts index 4d295df91..bae8a47ea 100644 --- a/frontend/src/apps/codebase_list.ts +++ b/frontend/src/apps/codebase_list.ts @@ -1,5 +1,6 @@ import "vite/modulepreload-polyfill"; // Ensure that this is needed based on your project setup +import { isEmpty } from "lodash"; import { createApp } from "vue"; import CodebaseListSidebar from "@/components/CodebaseListSidebar.vue"; import SortBy from "@/components/ListSortBy.vue"; @@ -8,11 +9,20 @@ import { extractDataParams } from "@/util"; const props = extractDataParams("sidebar", ["languageFacets"]); createApp(CodebaseListSidebar, props).mount("#sidebar"); -createApp(SortBy, { - sortOptions: [ - { value: "relevance", label: "Relevance" }, - { value: "-first_published_at", label: "Publish date: newest" }, - { value: "first_published_at", label: "Publish date: oldest" }, - { value: "-last_modified", label: "Recently Modified" }, - ], -}).mount("#sortby"); +// Function to check if 'query' exists and is not empty +function hasQueryParam(param: string) { + const params = new URLSearchParams(window.location.search); + const value = params.get(param); + return !isEmpty(value); +} + +const relevanceOption = hasQueryParam("query") ? [{ value: "relevance", label: "Relevance" }] : []; + +const sortOptions = [ + ...relevanceOption, + { value: "-first_published_at", label: "Most recently published" }, + { value: "first_published_at", label: "Earliest published" }, + { value: "-last_modified", label: "Recently modified" }, +]; + +createApp(SortBy, { sortOptions }).mount("#sortby"); diff --git a/frontend/src/components/CodebaseListSidebar.vue b/frontend/src/components/CodebaseListSidebar.vue index 4e8a82e9f..5050f8f89 100644 --- a/frontend/src/components/CodebaseListSidebar.vue +++ b/frontend/src/components/CodebaseListSidebar.vue @@ -57,6 +57,7 @@ label="Tags" type="Codebase" placeholder="Language, framework, etc." + :taggable="false" /> @@ -120,7 +121,7 @@ const initialFilterValues = { onMounted(() => { if (props.languageFacets) { const localLanguageFacets = { ...props.languageFacets }; - console.log(localLanguageFacets); + // console.log(localLanguageFacets); parsedLanguageFacets = Object.entries(localLanguageFacets) .sort(([, valueA], [, valueB]) => valueB - valueA) // Sort by value in descending order .map(([name, value]) => ({ value: name, label: `${name} (${value})` })); diff --git a/frontend/src/components/EventListSidebar.vue b/frontend/src/components/EventListSidebar.vue index 68de0b0ab..e8d587d0a 100644 --- a/frontend/src/components/EventListSidebar.vue +++ b/frontend/src/components/EventListSidebar.vue @@ -7,7 +7,7 @@ >