Skip to content

Commit

Permalink
search: improve with CompositeSuggestQueryParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ptamarit committed Dec 9, 2024
1 parent 11fe024 commit eb1fcfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 9 additions & 5 deletions invenio_users_resources/services/users/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 TU Wien.
# Copyright (C) 2022 CERN.
# Copyright (C) 2022-2024 CERN.
# Copyright (C) 2024 KTH Royal Institute of Technology.
#
# Invenio-Users-Resources is free software; you can redistribute it and/or
Expand All @@ -26,10 +26,10 @@
SortParam,
)
from invenio_records_resources.services.records.queryparser import (
CompositeSuggestQueryParser,
FieldValueMapper,
QueryParser,
SearchFieldTransformer,
SuggestQueryParser,
)
from luqum.tree import Word

Expand Down Expand Up @@ -68,9 +68,14 @@ class UserSearchOptions(SearchOptions, SearchOptionsMixin):
# The user search needs to be highly restricted to avoid leaking
# account information, hence do not edit here unless you are
# absolutely sure what you're doing.
suggest_parser_cls = SuggestQueryParser.factory(
suggest_parser_cls = CompositeSuggestQueryParser.factory(
tree_transformer_cls=SearchFieldTransformer,
fields=["username^2", "email^2", "profile.full_name^3", "profile.affiliations"],
fields=[
"username^2",
"email.keyword^2",
"profile.full_name^3",
"profile.affiliations",
],
# Only public emails because hidden emails are stored in email_hidden field.
allow_list=["username", "email"],
mapping={
Expand All @@ -81,7 +86,6 @@ class UserSearchOptions(SearchOptions, SearchOptionsMixin):
"name": "profile.full_name",
},
type="most_fields", # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#multi-match-types
fuzziness="AUTO", # https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
)

params_interpreters_cls = [
Expand Down
5 changes: 2 additions & 3 deletions tests/services/users/test_service_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ def test_user_search_field_not_searchable(user_service, user_pub, query):
("CERN", USERNAME_BOTH),
("Jose", USERNAME_JOSE),
("Jos", USERNAME_JOSE),
("Jose CERN", USERNAME_BOTH),
("Jose AND CERN", USERNAME_BOTH),
("Jose CERN", USERNAME_JOSE),
("Tim", USERNAME_TIM),
("Tim CERN", USERNAME_BOTH),
("Tim CERN", USERNAME_TIM),
("[email protected]", USERNAME_JOSE),
("[email protected]", USERNAME_JOSE),
("pub@inveniosoft", USERNAME_JOSE),
Expand Down

0 comments on commit eb1fcfa

Please sign in to comment.