Fix search by email index keyword (#10698)

* Fix search by email index keyword

* Fix search by email index keyword
This commit is contained in:
Pere Miquel Brull 2023-03-22 04:50:47 +01:00 committed by GitHub
parent 9f99296ea0
commit e2a2bcc8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 4 deletions

View File

@ -35,7 +35,7 @@ class OMetaUserMixin:
client: REST
email_search = (
"/search/query?q=email:{email}&from={from_}&size={size}&index="
"/search/query?q=email.keyword:{email}&from={from_}&size={size}&index="
+ ES_INDEX_MAP[User.__name__]
)
@ -44,7 +44,7 @@ class OMetaUserMixin:
self,
email: Optional[str],
from_count: int = 0,
size: int = 10,
size: int = 1,
fields: Optional[list] = None,
) -> Optional[User]:
"""

View File

@ -56,7 +56,13 @@ USER_ELASTICSEARCH_INDEX_MAPPING = textwrap.dedent(
"type": "text"
},
"email": {
"type": "text"
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isAdmin": {
"type": "text"

View File

@ -78,6 +78,10 @@ class OMetaUserTest(TestCase):
data=CreateUserRequest(name="Levy", email="user2.1234@getcollate.io"),
)
cls.user_3: User = cls.metadata.create_or_update(
data=CreateUserRequest(name="Lima", email="random.lima@getcollate.io"),
)
# Leave some time for indexes to get updated, otherwise this happens too fast
cls.check_es_index()
@ -112,6 +116,13 @@ class OMetaUserTest(TestCase):
self.metadata.get_user_by_email(email="idonotexist@random.com")
)
# Non existing email returns, even if they have the same domain
# To get this fixed, we had to update the `email` field in the
# index as a `keyword` and search by `email.keyword` in ES.
self.assertIsNone(
self.metadata.get_user_by_email(email="idonotexist@getcollate.io")
)
# I can get User 1, who has the name equal to its email
self.assertEqual(
self.user_1.id,

View File

@ -39,7 +39,13 @@
"type": "text"
},
"email": {
"type": "text"
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isAdmin": {
"type": "boolean"