Skip to content

Commit

Permalink
remove relevance in sort order when no query string is present (comse…
Browse files Browse the repository at this point in the history
…s#770)

if `query` is empty, `Relevance` ordering option in UI should not be visible for /codebases/

- simplify order by relevance logic, instead of passing strings around and interpreting them downstream, set order_by_relevance directly in the search_backend ES call
- re-enable "Apply Filters" for Event and Jobs
- remove the option to create non-existent tags in the tag filter on the sidebars: Codebase, Event, Jobs, Profile

Co-authored-by: Anton Suharev <[email protected]>
  • Loading branch information
alee and asuworks authored Oct 25, 2024
1 parent 108230c commit ff5c42d
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 31 deletions.
5 changes: 1 addition & 4 deletions django/core/jinja_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions django/core/view_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
"""
Expand Down
16 changes: 9 additions & 7 deletions django/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 18 additions & 8 deletions frontend/src/apps/codebase_list.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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");
3 changes: 2 additions & 1 deletion frontend/src/components/CodebaseListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
label="Tags"
type="Codebase"
placeholder="Language, framework, etc."
:taggable="false"
/>
</form>
</template>
Expand Down Expand Up @@ -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})` }));
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/EventListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
>
<template #form>
<form @submit="handleSubmit">
<TaggerField class="mb-3" name="tags" label="Keywords" type="Event" />
<TaggerField class="mb-3" name="tags" label="Keywords" type="Event" :taggable="false" />
<DatepickerField
class="mb-3"
name="submissionDeadlineAfter"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/JobListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<template #form>
<form @submit="handleSubmit">
<TaggerField class="mb-3" name="tags" label="Keywords" type="Job" />
<TaggerField class="mb-3" name="tags" label="Keywords" type="Job" :taggable="false" />
<DatepickerField class="mb-3" name="initialPostingAfter" label="Posted after" />
<DatepickerField name="applicationDeadlineAfter" label="Application deadline after" />
</form>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/ListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export interface SearchProps {
clearAllFilters?: () => void;
}
const props = defineProps<SearchProps>();
const props = withDefaults(defineProps<SearchProps>(), {
isFilterChanged: true,
});
function handleApplyFilters() {
isApplyingFiltersLoading.value = true;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ProfileListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<template #form>
<form @submit="handleSubmit">
<TaggerField name="tags" label="Keywords" type="Profile" />
<TaggerField name="tags" label="Keywords" type="Profile" :taggable="false" />
<!-- consider adding full member/peer reviewer filter -->
</form>
</template>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/form/TaggerField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
:clear-on-select="false"
:close-on-select="false"
:options-limit="50"
:taggable="true"
:taggable="taggable"
:limit="20"
@tag="addTag"
@search-change="fetchMatchingTags"
Expand Down Expand Up @@ -66,11 +66,13 @@ export interface TaggerFieldProps {
help?: string;
placeholder?: string;
required?: boolean;
taggable?: boolean;
type?: TagType;
}
const props = withDefaults(defineProps<TaggerFieldProps>(), {
type: "",
taggable: true,
placeholder: "Type to add tags",
});
Expand Down

0 comments on commit ff5c42d

Please sign in to comment.