diff --git a/ingestion/src/metadata/ingestion/ometa/mixins/user_mixin.py b/ingestion/src/metadata/ingestion/ometa/mixins/user_mixin.py index 2ee604973f1..c389fbc2842 100644 --- a/ingestion/src/metadata/ingestion/ometa/mixins/user_mixin.py +++ b/ingestion/src/metadata/ingestion/ometa/mixins/user_mixin.py @@ -13,6 +13,7 @@ Mixin class containing User specific methods To be used by OpenMetadata class """ +import json from functools import lru_cache from typing import Optional, Type @@ -46,16 +47,29 @@ class OMetaUserMixin: ) @staticmethod - def name_search_query_es(entity: Type[T]) -> str: + def name_search_query_es(entity: Type[T], name: str, from_: int, size: int) -> str: """ Allow for more flexible lookup following what the UI is doing when searching users. We don't want to stick to `q=name:{name}` since in case a user is named `random.user` but looked as `Random User`, we want to find this match. + + Search should only look in name and displayName fields and should not return bots. """ + query_filter = { + "query": { + "query_string": { + "query": f"{name} AND isBot:false", + "fields": ["name", "displayName"], + "default_operator": "AND", + "fuzziness": "AUTO", + } + } + } + return ( - "/search/query?q={name} AND isBot:false&from={from_}&size={size}&index=" - + ES_INDEX_MAP[entity.__name__] + f"""/search/query?query_filter={json.dumps(query_filter)}""" + f"&from={from_}&size={size}&index=" + ES_INDEX_MAP[entity.__name__] ) def _search_by_email( @@ -103,8 +117,8 @@ class OMetaUserMixin: fields: Optional field list to pass to ES request """ if name: - query_string = self.name_search_query_es(entity=entity).format( - name=name, from_=from_count, size=size + query_string = self.name_search_query_es( + entity=entity, name=name, from_=from_count, size=size ) return self.get_entity_from_es( entity=entity, query_string=query_string, fields=fields diff --git a/ingestion/tests/integration/ometa/test_ometa_user_api.py b/ingestion/tests/integration/ometa/test_ometa_user_api.py index fa716b9efbb..e746a7d65e1 100644 --- a/ingestion/tests/integration/ometa/test_ometa_user_api.py +++ b/ingestion/tests/integration/ometa/test_ometa_user_api.py @@ -62,7 +62,9 @@ class OMetaUserTest(TestCase): cls.user_1: User = cls.metadata.create_or_update( data=CreateUserRequest( - name="random.user.es", email="random.user.es@getcollate.io" + name="random.user.es", + email="random.user.es@getcollate.io", + description="test", ), ) @@ -198,3 +200,8 @@ class OMetaUserTest(TestCase): self.assertIsNone( self.metadata.get_reference_by_name(name="Organization", is_owner=True) ) + + # description should not affect in search + self.assertIsNone( + self.metadata.get_reference_by_name(name="test", is_owner=True) + )