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.
* Closes #402.

Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze committed Aug 23, 2021
1 parent 0d45c81 commit fd68b4b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 18 deletions.
88 changes: 71 additions & 17 deletions sonar/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,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 @@ -610,31 +611,84 @@ 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': ['-_created']
},
'oldest': {
'fields': ['_created']
},
'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']
},
'name': {
'fields': ['name.raw']
}
},
'collections': {
'relevance': {
'fields': ['-_score'],
},
'newest': {
'fields': ['-_created']
},
'bestmatch': {
'title': _('Best match'),
'oldest': {
'fields': ['_created']
},
'name': {
'fields': ['name.value.raw']
}
},
'deposits': {
'relevance': {
'fields': ['-_score'],
'default_order': 'asc',
'order': 2,
},
'newest': {
'fields': ['-_created']
},
'oldest': {
'fields': ['_created']
}
}
}
"""Setup sorting options."""

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"raw": {
"type": "keyword"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@
"type": "object",
"properties": {
"value": {
"type": "text"
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"language": {
"type": "keyword"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"description": {
"type": "text"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"code": {
"type": "keyword"
},
Expand Down
3 changes: 3 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,9 @@
"suggest": {
"type": "completion",
"analyzer": "default"
},
"raw": {
"type": "keyword"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"raw": {
"type": "keyword"
}
}
},
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': ['-created']
},
'oldest': {
'fields': ['created']
}
}
search_facets_options = {
'aggs': {
'user': {
Expand Down

0 comments on commit fd68b4b

Please sign in to comment.