Skip to content

Commit

Permalink
fix: sort by relevance on initial search
Browse files Browse the repository at this point in the history
1. On initial page load, codebases are sorted by date: newest.
2. On first search, results are sorted by relevance
3. Disable buttons in the sidebar when loading
4. Disable "Apply filters" button if filters are unchanged
  • Loading branch information
asuworks authored and alee committed Aug 30, 2024
1 parent 67b108d commit 096bce2
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 180 deletions.
27 changes: 9 additions & 18 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
"profile": "https://github.com/cpritcha",
"contributions": [
"code",
"doc",
"bug",
"maintenance",
"test",
"review"
"doc"
]
},
{
Expand All @@ -30,12 +26,8 @@
"avatar_url": "https://avatars0.githubusercontent.com/u/8737685?v=4",
"profile": "https://www.linkedin.com/in/chrstngyn/",
"contributions": [
"bug",
"code",
"doc",
"design",
"test",
"maintenance"
"doc"
]
},
{
Expand All @@ -53,17 +45,13 @@
"avatar_url": "https://avatars0.githubusercontent.com/u/22534?v=4",
"profile": "https://complexity.asu.edu",
"contributions": [
"bug",
"code",
"design",
"doc",
"fundingFinding",
"ideas",
"infra",
"maintenance",
"mentoring",
"projectManagement",
"review",
"security",
"test"
"security"
]
},
{
Expand All @@ -74,6 +62,9 @@
"contributions": [
"code",
"design",
"doc",
"mentoring",
"review",
"test"
]
},
Expand Down Expand Up @@ -112,7 +103,7 @@
"contributions": [
"code"
]
},
}
],
"contributorsPerLine": 7,
"skipCi": true
Expand Down
13 changes: 6 additions & 7 deletions django/core/jinja2/common.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
{% for term in list_state.filter_display_terms %}
<span class="badge bg-gray ms-1">{{ term }}</span>
{% endfor %}
<a class="text-danger ms-1" href="{{ url(url_name) }}">
<i class="fas fa-times"></i> clear
<a class="text-warn ms-1" href="{{ url(url_name) }}">
<i class="fas fa-times"></i> clear filters
</a>
{% endif %}
</p>
Expand All @@ -189,11 +189,10 @@
<div class="input-group">
<input id="search-query" class="form-control" name="query" type="search" value="{{ current_query }}"
placeholder="{{ placeholder }}">
{% if query_params %}
{% for key, value in generate_hidden_inputs(query_params) %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}
{% endif %}
{% for key, value in generate_hidden_inputs(query_params) %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}

<button type="submit" class="btn btn-primary">
<i class="fas fa-search px-1"></i>
</button>
Expand Down
9 changes: 9 additions & 0 deletions django/core/jinja_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,18 @@ def generate_hidden_inputs(query_params):
if query_params:
# parse_qsl handles splitting and unquoting key-value pairs
parsed_params = parse_qsl(query_params)

# set default ordering, if it is not specified
if not any(key == "ordering" for key, value in parsed_params):
hidden_inputs.append(("ordering", "relevance"))

for key, value in convert_keys_to_camel_case(parsed_params):
if key != "query":
hidden_inputs.append((key, value))
else:
# initial ordering of the codebase search results
hidden_inputs.append(("ordering", "relevance"))

return hidden_inputs


Expand Down
4 changes: 2 additions & 2 deletions django/core/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def create_paginated_context_data(
Args:
query: request query
data: results from Elasticsearch
page: requested page
page: requested page (typically from http query params)
count: total number of results
query_params: query dictionary
size (optional): number of results per page (default=cls.page_size)
size (optional): number of results per page, defaults to cls.page_size (currently: 10)
Returns:
dict: paginated context data
Expand Down
4 changes: 4 additions & 0 deletions django/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ def filter_queryset(self, request, queryset, view):
criteria.update(id__in=filtered_codebase_ids)
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)

Expand Down
Loading

0 comments on commit 096bce2

Please sign in to comment.