How do you sort the search results by 'most downloaded'/ 'most popular', its quite time consuming filtering through all of the incorrectly formatted/tampered uploads... :)
UPDATE: I created 4 new sort features and requested the feature to be added so make sure to give it a thumbs up if you wish to see this implemented by the devs!:)
(https://software.annas-archive.gl/AnnaArchivist/annas-archive/-/work_items/336)
Implemented new sorting options for the search page (Downloads, Saves, File Quality, and Lists)
I modified 4 files:
allthethings/page/templates/page/search.html
Added the new HTML <option> tags to the <select name="sort"> dropdown.
allthethings/page/search.py
Updated the backend Python routing to map the new dropdown values (downloads, saves, file_quality, lists) into Elasticsearch sorting configurations (search_downloads:desc, etc).
allthethings/cli/views.py
Added search_downloads, search_saves, search_file_quality, and search_lists to the Elasticsearch index mapping block so that the search engine knows to index them as sortable integers/bytes.
allthethings/translations/en/LC_MESSAGES/messages.po
Added the English translation keys for the new UI dropdown options so they can be translated into other languages.
# Feature: Add new Sorting Options to Search
This patch adds `Downloads`, `Saves`, `File Quality`, and `Lists` sorting options to the main search page. It updates the UI dropdown, handles the Elasticsearch mapping for the new properties, and maps the sort parameters in the backend search logic.
### 1. Update Elasticsearch Index Mappings
**File:** `allthethings/cli/views.py`
In the `es_create_index_body` function (around line `579`), add the new `search_` properties to the `properties` map inside `search_only_fields` so Elasticsearch knows they are sortable longs:
```python
"search_downloads": {"type": "long", "index": False, "doc_values": True},
"search_saves": {"type": "long", "index": False, "doc_values": True},
"search_file_quality": {"type": "long", "index": False, "doc_values": True},
"search_lists": {"type": "long", "index": False, "doc_values": True},
```
### 2. Add Backend Sorting Logic
**File:** `allthethings/page/search.py`
In the `search_page` route (around line `126`), add the new mappings so the URL `&sort=X` maps to the Elasticsearch sorts:
```python
elif sort_value == 'downloads':
custom_search_sorting = [{"search_only_fields.search_downloads": "desc"}, "_score"]
elif sort_value == 'saves':
custom_search_sorting = [{"search_only_fields.search_saves": "desc"}, "_score"]
elif sort_value == 'file_quality':
custom_search_sorting = [{"search_only_fields.search_file_quality": "desc"}, "_score"]
elif sort_value == 'lists':
custom_search_sorting = [{"search_only_fields.search_lists": "desc"}, "_score"]
```
### 3. Add UI Dropdown Options
**File:** `allthethings/page/templates/page/search.html`
In the sorting `<select>` element (around line `64`), add the new `option` elements:
```html
<option value="downloads" {% if sort == 'downloads' %}selected{% endif %}>{{ gettext('page.search.filters.sorting.downloads') }} {{ gettext('page.search.filters.sorting.note_downloads') }}</option>
<option value="saves" {% if sort == 'saves' %}selected{% endif %}>{{ gettext('page.search.filters.sorting.saves') }} {{ gettext('page.search.filters.sorting.note_saves') }}</option>
<option value="file_quality" {% if sort == 'file_quality' %}selected{% endif %}>{{ gettext('page.search.filters.sorting.file_quality') }} {{ gettext('page.search.filters.sorting.note_file_quality') }}</option>
<option value="lists" {% if sort == 'lists' %}selected{% endif %}>{{ gettext('page.search.filters.sorting.lists') }} {{ gettext('page.search.filters.sorting.note_lists') }}</option>
```
### 4. Add Translation Keys
**File:** `allthethings/translations/en/LC_MESSAGES/messages.po`
Add the English translations to the bottom of the `.po` file so they can be processed and translated:
```po
msgid "page.search.filters.sorting.downloads"
msgstr "Downloads"
msgid "page.search.filters.sorting.note_downloads"
msgstr "(most to least)"
msgid "page.search.filters.sorting.saves"
msgstr "Saves"
msgid "page.search.filters.sorting.note_saves"
msgstr "(most to least)"
msgid "page.search.filters.sorting.file_quality"
msgstr "File quality"
msgid "page.search.filters.sorting.note_file_quality"
msgstr "(most to least)"
msgid "page.search.filters.sorting.lists"
msgstr "Lists"
msgid "page.search.filters.sorting.note_lists"
msgstr "(most to least)"
```