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 ee9e9e7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
9 changes: 4 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 -*-

Check failure on line 1 in invenio_users_resources/services/users/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)

Black format check --- /home/runner/work/invenio-users-resources/invenio-users-resources/invenio_users_resources/services/users/config.py 2024-12-09 08:16:10.817864+00:00 +++ /home/runner/work/invenio-users-resources/invenio-users-resources/invenio_users_resources/services/users/config.py 2024-12-09 08:18:49.324620+00:00 @@ -68,11 +68,16 @@ # 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 = CompositeSuggestQueryParser.factory( tree_transformer_cls=SearchFieldTransformer, - fields=["username^2", "email.keyword^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={ "affiliation": "profile.affiliations", "affiliations": "profile.affiliations",

Check failure on line 1 in invenio_users_resources/services/users/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)

Black format check --- /home/runner/work/invenio-users-resources/invenio-users-resources/invenio_users_resources/services/users/config.py 2024-12-09 08:16:10.424418+00:00 +++ /home/runner/work/invenio-users-resources/invenio-users-resources/invenio_users_resources/services/users/config.py 2024-12-09 08:18:55.126489+00:00 @@ -68,11 +68,16 @@ # 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 = CompositeSuggestQueryParser.factory( tree_transformer_cls=SearchFieldTransformer, - fields=["username^2", "email.keyword^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={ "affiliation": "profile.affiliations", "affiliations": "profile.affiliations",
#
# 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,9 @@ 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 +81,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 ee9e9e7

Please sign in to comment.