From 72f1d998b4ee23fa52911df88fbd3ab7a676df3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20De=CC=81le=CC=80ze?= Date: Fri, 27 Aug 2021 08:27:19 +0200 Subject: [PATCH] records: configure sorting behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- sonar/config.py | 102 +++++++++++++++--- sonar/es_templates/v7/record.json | 9 ++ .../v7/collections/collection-v1.0.0.json | 4 + .../v7/documents/document-v1.0.0.json | 8 +- .../v7/organisations/organisation-v1.0.0.json | 9 ++ .../v7/subdivisions/subdivision-v1.0.0.json | 4 + .../users/mappings/v7/users/user-v1.0.0.json | 4 + .../mappings/v7/projects/project-v1.0.0.json | 4 + sonar/resources/projects/service.py | 16 +++ sonar/theme/templates/sonar/macros/menu.html | 4 +- sonar/theme/templates/sonar/page.html | 4 +- .../theme/templates/sonar/partial/navbar.html | 4 +- .../sonar/partial/switch_aai_dropdown.html | 2 +- 13 files changed, 149 insertions(+), 25 deletions(-) diff --git a/sonar/config.py b/sonar/config.py index 941fd59b..af23fdee 100644 --- a/sonar/config.py +++ b/sonar/config.py @@ -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': { @@ -625,31 +626,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.""" diff --git a/sonar/es_templates/v7/record.json b/sonar/es_templates/v7/record.json index ef0a797d..71f27b5b 100644 --- a/sonar/es_templates/v7/record.json +++ b/sonar/es_templates/v7/record.json @@ -18,6 +18,15 @@ ] } }, + "normalizer": { + "sort_normalizer": { + "type": "custom", + "filter": [ + "lowercase", + "asciifolding" + ] + } + }, "analyzer": { "default": { "type": "custom", diff --git a/sonar/modules/collections/mappings/v7/collections/collection-v1.0.0.json b/sonar/modules/collections/mappings/v7/collections/collection-v1.0.0.json index b55961db..c07c3717 100644 --- a/sonar/modules/collections/mappings/v7/collections/collection-v1.0.0.json +++ b/sonar/modules/collections/mappings/v7/collections/collection-v1.0.0.json @@ -30,6 +30,10 @@ "type": "text", "analyzer": "autocomplete", "search_analyzer": "standard" + }, + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" } } } diff --git a/sonar/modules/documents/mappings/v7/documents/document-v1.0.0.json b/sonar/modules/documents/mappings/v7/documents/document-v1.0.0.json index 738637b6..1fe0e0eb 100644 --- a/sonar/modules/documents/mappings/v7/documents/document-v1.0.0.json +++ b/sonar/modules/documents/mappings/v7/documents/document-v1.0.0.json @@ -123,7 +123,13 @@ "type": "object", "properties": { "value": { - "type": "text" + "type": "text", + "fields": { + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" + } + } }, "language": { "type": "keyword" diff --git a/sonar/modules/organisations/mappings/v7/organisations/organisation-v1.0.0.json b/sonar/modules/organisations/mappings/v7/organisations/organisation-v1.0.0.json index 109c12df..badb8608 100644 --- a/sonar/modules/organisations/mappings/v7/organisations/organisation-v1.0.0.json +++ b/sonar/modules/organisations/mappings/v7/organisations/organisation-v1.0.0.json @@ -17,6 +17,15 @@ "description": { "type": "text" }, + "name": { + "type": "text", + "fields": { + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" + } + } + }, "code": { "type": "keyword" }, diff --git a/sonar/modules/subdivisions/mappings/v7/subdivisions/subdivision-v1.0.0.json b/sonar/modules/subdivisions/mappings/v7/subdivisions/subdivision-v1.0.0.json index 31f9aacd..9781113e 100644 --- a/sonar/modules/subdivisions/mappings/v7/subdivisions/subdivision-v1.0.0.json +++ b/sonar/modules/subdivisions/mappings/v7/subdivisions/subdivision-v1.0.0.json @@ -30,6 +30,10 @@ "type": "text", "analyzer": "autocomplete", "search_analyzer": "standard" + }, + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" } } } diff --git a/sonar/modules/users/mappings/v7/users/user-v1.0.0.json b/sonar/modules/users/mappings/v7/users/user-v1.0.0.json index 255e6bf4..f07475ab 100644 --- a/sonar/modules/users/mappings/v7/users/user-v1.0.0.json +++ b/sonar/modules/users/mappings/v7/users/user-v1.0.0.json @@ -26,6 +26,10 @@ "suggest": { "type": "completion", "analyzer": "default" + }, + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" } } }, diff --git a/sonar/resources/projects/mappings/v7/projects/project-v1.0.0.json b/sonar/resources/projects/mappings/v7/projects/project-v1.0.0.json index 640fc804..27804254 100644 --- a/sonar/resources/projects/mappings/v7/projects/project-v1.0.0.json +++ b/sonar/resources/projects/mappings/v7/projects/project-v1.0.0.json @@ -21,6 +21,10 @@ "type": "text", "analyzer": "autocomplete", "search_analyzer": "standard" + }, + "raw": { + "type": "keyword", + "normalizer": "sort_normalizer" } } }, diff --git a/sonar/resources/projects/service.py b/sonar/resources/projects/service.py index 9c2e7760..e6f12a27 100644 --- a/sonar/resources/projects/service.py +++ b/sonar/resources/projects/service.py @@ -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': { diff --git a/sonar/theme/templates/sonar/macros/menu.html b/sonar/theme/templates/sonar/macros/menu.html index 086e6c62..2597b063 100644 --- a/sonar/theme/templates/sonar/macros/menu.html +++ b/sonar/theme/templates/sonar/macros/menu.html @@ -42,7 +42,7 @@ {% else -%}