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 Feb 21, 2025
1 parent 66b8d13 commit 87d182e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 9 additions & 4 deletions invenio_users_resources/services/users/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,15 @@ 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",
"email^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 +87,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),
("pub@inveniosoftware.org", USERNAME_JOSE),
("pub@inveniosoftware.or", USERNAME_JOSE),
("pub@inveniosoft", USERNAME_JOSE),
Expand Down

0 comments on commit 87d182e

Please sign in to comment.