Skip to content

Commit

Permalink
records: configure sorting behavior
Browse files Browse the repository at this point in the history
* Configures sorting behavior for all records types.
* Indexes the fields to sort on as keyword.
* Avoids to click two times on dropdown to show it.
* Closes #402.

Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze committed Aug 30, 2021
1 parent 33bce7e commit ca52e5f
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 25 deletions.
102 changes: 85 additions & 17 deletions sonar/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ def _(x):
SECURITY_RESET_PASSWORD_TEMPLATE = 'sonar/accounts/reset_password.html'
SECURITY_REGISTER_USER_TEMPLATE = 'sonar/accounts/signup.html'
SECURITY_EMAIL_SUBJECT_PASSWORD_RESET = _('SONAR password reset')
SECURITY_EMAIL_SUBJECT_PASSWORD_NOTICE = _('Your SONAR password has been reset')
SECURITY_EMAIL_SUBJECT_PASSWORD_NOTICE = _(
'Your SONAR password has been reset')

RECORDS_UI_ENDPOINTS = {
'doc': {
Expand Down Expand Up @@ -621,31 +622,98 @@ def _(x):
}
"""REST search facets."""

INDEXES = ['documents', 'organisations', 'users', 'deposits']

RECORDS_REST_SORT_OPTIONS = {}
for index in INDEXES:
RECORDS_REST_SORT_OPTIONS[index] = {
'mostrecent': {
'title': _('Most recent'),
'fields': ['-_created'],
'default_order': 'desc',
'order': 1,
RECORDS_REST_SORT_OPTIONS = {
'documents': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-provisionActivity.startDate']
},
'oldest': {
'fields': ['provisionActivity.startDate']
},
'title': {
'fields': ['title.mainTitle.value.raw']
}
},
'users': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
},
'name': {
'fields': ['full_name.raw']
}
},
'organisations': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
},
'bestmatch': {
'title': _('Best match'),
'name': {
'fields': ['name.raw']
}
},
'collections': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
},
'name': {
'fields': ['name.value.raw']
}
},
'deposits': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
}
},
'subdivisions': {
'relevance': {
'fields': ['-_score'],
'default_order': 'asc',
'order': 2,
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
},
'name': {
'fields': ['name.value.raw']
}
}
}
"""Setup sorting options."""

INDEXES = ['documents', 'organisations', 'users', 'deposits', 'subdivisions']

RECORDS_REST_DEFAULT_SORT = {}
for index in INDEXES:
RECORDS_REST_DEFAULT_SORT[index] = {
'query': 'bestmatch',
'noquery': 'mostrecent'
'query': 'relevance',
'noquery': 'newest'
}
"""Set default sorting options."""

Expand Down
9 changes: 9 additions & 0 deletions sonar/es_templates/v7/record.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
]
}
},
"normalizer": {
"sort_normalizer": {
"type": "custom",
"filter": [
"lowercase",
"asciifolding"
]
}
},
"analyzer": {
"default": {
"type": "custom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@
"type": "object",
"properties": {
"value": {
"type": "text"
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
},
"language": {
"type": "keyword"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"description": {
"type": "text"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
},
"code": {
"type": "keyword"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions sonar/modules/users/mappings/v7/users/user-v1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"suggest": {
"type": "completion",
"analyzer": "default"
},
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"raw": {
"type": "keyword",
"normalizer": "sort_normalizer"
}
}
},
Expand Down
16 changes: 16 additions & 0 deletions sonar/resources/projects/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class RecordServiceConfig(BaseRecordServiceConfig):
permission_policy_cls = RecordPermissionPolicy
record_cls = Record
result_list_cls = RecordList
search_sort_default = 'relevance'
search_sort_default_no_query = 'newest'
search_sort_options = {
'relevance': {
'fields': ['_score'],
},
'name': {
'fields': ['metadata.name.raw']
},
'newest': {
'fields': ['-metadata.startDate']
},
'oldest': {
'fields': ['metadata.startDate']
}
}
search_facets_options = {
'aggs': {
'user': {
Expand Down
4 changes: 2 additions & 2 deletions sonar/theme/templates/sonar/macros/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{% else -%}
<li class="nav-item dropdown">
{%- endif %}
<a href="#" class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<a href="#" class="nav-link dropdown-toggle dropdown-toggle-custom" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{% if item.class_name %}<i class="{{ item.class_name }}"></i> {% endif %}
<span>{{ item.name }}</span>
</a>
Expand All @@ -66,4 +66,4 @@
{%- endif -%}
{% endif -%}
{% endfor %}
{% endmacro %}
{% endmacro %}
4 changes: 2 additions & 2 deletions sonar/theme/templates/sonar/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@
document.addEventListener('click', function (event) {
let link = event.target;

// Don't do anything if .toggle-display, the process is just above.
if (link.matches('.toggle-display')) {
// Don't do anything if element don't have custom class.
if (!link.matches('.dropdown-toggle-custom')) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions sonar/theme/templates/sonar/partial/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
{% endif %}
<li class="nav-item dropdown pl-3">
{% with dropdownId='userLinkDropdown' %}
<a class="nav-link dropdown-toggle" href="#" id="{{ dropdownId }}" role="button" data-toggle="dropdown"
<a class="nav-link dropdown-toggle dropdown-toggle-custom" href="#" id="{{ dropdownId }}" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<i class="fa fa-user mr-2"></i>
{% if current_user_record %}
Expand All @@ -96,7 +96,7 @@
{% endif %}
{%- if config.I18N_LANGUAGES %}
<li class="nav-item dropdown pl-3">
<a class="nav-link dropdown-toggle" href="#" id="languageDropdown" role="button" data-toggle="dropdown"
<a class="nav-link dropdown-toggle dropdown-toggle-custom" href="#" id="languageDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
{{ current_i18n.language|upper }}
</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="dropdown">
<button class="btn btn-outline-primary btn-block dropdown-toggle" type="button" id="dropdownMenuButton"
<button class="btn btn-outline-primary btn-block dropdown-toggle dropdown-toggle-custom" type="button" id="dropdownMenuButton"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ label | default(_('Sign-up with SWITCHaai')) }}
</button>
Expand Down

0 comments on commit ca52e5f

Please sign in to comment.