forked from amundsen-io/amundsen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.py
668 lines (580 loc) · 26 KB
/
elasticsearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import itertools
import logging
import uuid
from typing import (
Any, Dict, List, Union,
)
from amundsen_common.models.index_map import TABLE_INDEX_MAP, USER_INDEX_MAP
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import NotFoundError
from elasticsearch_dsl import Search, query
from flask import current_app
from search_service import config
from search_service.api.dashboard import DASHBOARD_INDEX
from search_service.api.table import TABLE_INDEX
from search_service.api.user import USER_INDEX
from search_service.models.dashboard import Dashboard, SearchDashboardResult
from search_service.models.search_result import SearchResult
from search_service.models.table import SearchTableResult, Table
from search_service.models.tag import Tag
from search_service.models.user import SearchUserResult, User
from search_service.proxy.base import BaseProxy
from search_service.proxy.statsd_utilities import timer_with_counter
# Default Elasticsearch index to use, if none specified
DEFAULT_ES_INDEX = 'table_search_index'
LOGGING = logging.getLogger(__name__)
# mapping to translate request for table resources
TABLE_MAPPING = {
'badges': 'badges',
'tag': 'tags',
'schema': 'schema.raw',
'table': 'name.raw',
'column': 'column_names.raw',
'database': 'database.raw',
'cluster': 'cluster.raw'
}
# Maps payload to a class
TAG_MAPPING = {
'badges': Tag,
'tags': Tag
}
# mapping to translate request for dashboard resources
DASHBOARD_MAPPING = {
'group_name': 'group_name.raw',
'name': 'name.raw',
'product': 'product',
'tag': 'tags',
}
class ElasticsearchProxy(BaseProxy):
"""
ElasticSearch connection handler
"""
def __init__(self, *,
host: str = None,
user: str = '',
password: str = '',
client: Elasticsearch = None,
page_size: int = 10
) -> None:
"""
Constructs Elasticsearch client for interactions with the cluster.
Allows caller to pass a fully constructed Elasticsearch client, {elasticsearch_client}
or constructs one from the parameters provided.
:param host: Elasticsearch host we should connect to
:param auth_user: user name to use for authentication
:param auth_pw: user password to use for authentication
:param elasticsearch_client: Elasticsearch client to use, if provided
:param page_size: Number of search results to return per request
"""
if client:
self.elasticsearch = client
else:
http_auth = (user, password) if user else None
self.elasticsearch = Elasticsearch(host, http_auth=http_auth)
self.page_size = page_size
def _get_search_result(self, page_index: int,
client: Search,
model: Any,
search_result_model: Any = SearchResult) -> Any:
"""
Common helper function to get result.
:param page_index:
:param client:
:param model: The model to import result(table, user etc)
:return:
"""
if model is None:
raise Exception('ES Doc model must be provided!')
results = []
# Use {page_index} to calculate index of results to fetch from
if page_index != -1:
start_from = page_index * self.page_size
end_at = start_from + self.page_size
client = client[start_from:end_at]
else:
# if page index is -1, return everything
client = client[0:client.count()]
response = client.execute()
for hit in response:
try:
es_metadata = hit.__dict__.get('meta', {})
"""
ES hit example:
{
'_d_': {
'name': 'name',
'database': 'database',
'schema': 'schema',
'key': 'database://cluster.schema/name',
'cluster': 'cluster',
'column_descriptions': ['description1', 'description2'],
'column_names': ['colname1', 'colname2'],
'description': None,
'display_name': 'display name',
'last_updated_timestamp': 12345678,
'programmatic_descriptions': [],
'schema_description': None,
'tags': ['tag1', 'tag2'],
'badges': [],
'total_usage': 0
},
'mata': {
'index': 'table index',
'id': 'table id',
'type': 'type'
}
}
"""
es_payload = hit.__dict__.get('_d_', {})
if not es_payload:
raise Exception('The ES doc not contain required field')
result = {}
for attr, val in es_payload.items():
if attr in model.get_attrs():
result[attr] = self._get_instance(attr=attr, val=val)
result['id'] = self._get_instance(attr='id', val=es_metadata['id'])
results.append(model(**result))
except Exception:
LOGGING.exception('The record doesnt contain specified field.')
return search_result_model(total_results=response.hits.total,
results=results)
def _get_instance(self, attr: str, val: Any) -> Any:
if attr in TAG_MAPPING:
# maps a given badge or tag to a tag class
return [TAG_MAPPING[attr](tag_name=property_val) for property_val in val] # type: ignore
else:
return val
def _search_helper(self, page_index: int,
client: Search,
query_name: dict,
model: Any,
search_result_model: Any = SearchResult) -> Any:
"""
Constructs Elasticsearch Query DSL to:
1. Use function score to customize scoring of search result. It currently uses "total_usage" field to score.
`Link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html`_
2. Uses multi match query to search term in multiple fields.
`Link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html`_
:param page_index:
:param client:
:param query_name: name of query to query the ES
:return:
"""
if query_name:
q = query.Q(query_name)
client = client.query(q)
return self._get_search_result(page_index=page_index,
client=client,
model=model,
search_result_model=search_result_model)
@timer_with_counter
def fetch_table_search_results(self, *,
query_term: str,
page_index: int = 0,
index: str = '') -> SearchTableResult:
"""
Query Elasticsearch and return results as list of Table objects
:param query_term: search query term
:param page_index: index of search page user is currently on
:param index: current index for search. Provide different index for different resource.
:return: SearchResult Object
"""
current_index = index if index else \
current_app.config.get(config.ELASTICSEARCH_INDEX_KEY, DEFAULT_ES_INDEX)
if not query_term:
# return empty result for blank query term
return SearchTableResult(total_results=0, results=[])
s = Search(using=self.elasticsearch, index=current_index)
query_name = {
"function_score": {
"query": {
"multi_match": {
"query": query_term,
"fields": ["display_name^1000",
"name.raw^75",
"name^5",
"schema^3",
"description^3",
"column_names^2",
"column_descriptions",
"tags",
"badges",
"programmatic_descriptions"],
}
},
"field_value_factor": {
"field": "total_usage",
"modifier": "log2p"
}
}
}
return self._search_helper(page_index=page_index,
client=s,
query_name=query_name,
model=Table,
search_result_model=SearchTableResult)
@staticmethod
def get_model_by_index(index: str) -> Any:
if index == TABLE_INDEX:
return Table
elif index == USER_INDEX:
return User
elif index == DASHBOARD_INDEX:
return Dashboard
raise Exception('Unable to map given index to a valid model')
@staticmethod
def parse_filters(filter_list: Dict,
index: str) -> str:
query_list = [] # type: List[str]
if index == TABLE_INDEX:
mapping = TABLE_MAPPING
elif index == DASHBOARD_INDEX:
mapping = DASHBOARD_MAPPING
else:
raise Exception(f'index {index} doesnt exist nor support search filter')
for category, item_list in filter_list.items():
mapped_category = mapping.get(category)
if mapped_category is None:
LOGGING.warn(f'Unsupported filter category: {category} passed in list of filters')
elif item_list is '' or item_list == ['']:
LOGGING.warn(f'The filter value cannot be empty.In this case the filter {category} is ignored')
else:
query_list.append(mapped_category + ':' + '(' + ' OR '.join(item_list) + ')')
if len(query_list) == 0:
return ''
return ' AND '.join(query_list)
@staticmethod
def validate_filter_values(search_request: dict) -> Any:
if 'filters' in search_request:
filter_values_list = search_request['filters'].values()
# Ensure all values are arrays
filter_values_list = list(
map(lambda x: x if type(x) == list else [x], filter_values_list))
# Flatten the array of arrays
filter_values_list = list(itertools.chain.from_iterable(filter_values_list))
# Check if / or : exist in any of the values
if any(("/" in str(item) or ":" in str(item)) for item in (filter_values_list)):
return False
return True
@staticmethod
def parse_query_term(query_term: str,
index: str) -> str:
# TODO: Might be some issue with using wildcard & underscore
# https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/8
if index == TABLE_INDEX:
query_term = f'(name:(*{query_term}*) OR name:({query_term}) ' \
f'OR schema:(*{query_term}*) OR schema:({query_term}) ' \
f'OR description:(*{query_term}*) OR description:({query_term}) ' \
f'OR column_names:(*{query_term}*) OR column_names:({query_term}) ' \
f'OR column_descriptions:(*{query_term}*) OR column_descriptions:({query_term}))'
elif index == DASHBOARD_INDEX:
query_term = f'(name:(*{query_term}*) OR name:({query_term}) ' \
f'OR group_name:(*{query_term}*) OR group_name:({query_term}) ' \
f'OR query_names:(*{query_term}*) OR query_names:({query_term}) ' \
f'OR description:(*{query_term}*) OR description:({query_term}) ' \
f'OR tags:(*{query_term}*) OR tags:({query_term}) ' \
f'OR badges:(*{query_term}*) OR badges:({query_term}) ' \
f'OR product:(*{query_term}*) OR product:({query_term}))'
else:
raise Exception(f'index {index} doesnt exist nor support search filter')
return query_term
@classmethod
def convert_query_json_to_query_dsl(self, *,
search_request: dict,
query_term: str,
index: str) -> str:
"""
Convert the generic query json to query DSL
e.g
```
{
'type': 'AND'
'filters': {
'database': ['hive', 'bigquery'],
'schema': ['test-schema1', 'test-schema2'],
'table': ['*amundsen*'],
'column': ['*ds*']
'tag': ['test-tag']
}
}
This generic JSON will convert into DSL depending on the backend engines.
E.g in Elasticsearch, it will become
'database':('hive' OR 'bigquery') AND
'schema':('test-schema1' OR 'test-schema2') AND
'table':('*amundsen*') AND
'column':('*ds*') AND
'tag':('test-tag')
```
:param search_request:
:param query_term:
:param index: table_index, dashboard_index
:return: The search engine query DSL
"""
filter_list = search_request.get('filters')
add_query = ''
query_dsl = ''
if filter_list:
valid_filters = self.validate_filter_values(search_request)
if valid_filters is False:
raise Exception(
'The search filters contain invalid characters and thus cannot be handled by ES')
query_dsl = self.parse_filters(filter_list,
index)
if query_term:
add_query = self.parse_query_term(query_term,
index)
if not query_dsl and not add_query:
raise Exception('Unable to convert parameters to valid query dsl')
result = ''
if query_dsl and add_query:
result = query_dsl + ' AND ' + add_query
elif add_query and not query_dsl:
result = add_query
elif query_dsl and not add_query:
result = query_dsl
return result
@timer_with_counter
def fetch_search_results_with_filter(self, *,
query_term: str,
search_request: dict,
page_index: int = 0,
index: str = '') -> Union[SearchDashboardResult,
SearchTableResult]:
"""
Query Elasticsearch and return results as list of Table objects
:param search_request: A json representation of search request
:param page_index: index of search page user is currently on
:param index: current index for search. Provide different index for different resource.
:return: SearchResult Object
"""
current_index = index if index else \
current_app.config.get(config.ELASTICSEARCH_INDEX_KEY, DEFAULT_ES_INDEX) # type: str
if current_index == DASHBOARD_INDEX:
search_model = SearchDashboardResult # type: Any
elif current_index == TABLE_INDEX:
search_model = SearchTableResult
else:
raise RuntimeError(f'the {index} doesnt have search filter support')
if not search_request:
# return empty result for blank query term
return search_model(total_results=0, results=[])
try:
query_string = self.convert_query_json_to_query_dsl(search_request=search_request,
query_term=query_term,
index=current_index) # type: str
except Exception as e:
LOGGING.exception(e)
# return nothing if any exception is thrown under the hood
return search_model(total_results=0, results=[])
s = Search(using=self.elasticsearch, index=current_index)
query_name = {
"function_score": {
"query": {
"query_string": {
"query": query_string
}
},
"field_value_factor": {
"field": "total_usage",
"modifier": "log2p"
}
}
}
model = self.get_model_by_index(current_index)
return self._search_helper(page_index=page_index,
client=s,
query_name=query_name,
model=model,
search_result_model=search_model)
@timer_with_counter
def fetch_user_search_results(self, *,
query_term: str,
page_index: int = 0,
index: str = '') -> SearchUserResult:
if not index:
raise Exception('Index cant be empty for user search')
if not query_term:
# return empty result for blank query term
return SearchUserResult(total_results=0, results=[])
s = Search(using=self.elasticsearch, index=index)
# Don't use any weight(total_follow, total_own, total_use)
query_name = {
"function_score": {
"query": {
"multi_match": {
"query": query_term,
"fields": ["full_name.raw^30",
"full_name^5",
"first_name.raw^5",
"last_name.raw^5",
"first_name^3",
"last_name^3",
"email^3"],
"operator": "and"
}
}
}
}
return self._search_helper(page_index=page_index,
client=s,
query_name=query_name,
model=User,
search_result_model=SearchUserResult)
@timer_with_counter
def fetch_dashboard_search_results(self, *,
query_term: str,
page_index: int = 0,
index: str = '') -> SearchDashboardResult:
"""
Fetch dashboard search result with fuzzy search
:param query_term:
:param page_index:
:param index:
:return:
"""
current_index = index if index else \
current_app.config.get(config.ELASTICSEARCH_INDEX_KEY, DEFAULT_ES_INDEX)
if not query_term:
# return empty result for blank query term
return SearchDashboardResult(total_results=0, results=[])
s = Search(using=self.elasticsearch, index=current_index)
query_name = {
"function_score": {
"query": {
"multi_match": {
"query": query_term,
"fields": ["name.raw^75",
"name^7",
"group_name.raw^15",
"group_name^7",
"description^3",
"query_names^3"]
}
},
"field_value_factor": {
"field": "total_usage",
"modifier": "log2p"
}
}
}
return self._search_helper(page_index=page_index,
client=s,
query_name=query_name,
model=Dashboard,
search_result_model=SearchDashboardResult)
# The following methods are related to document API that needs to update
@timer_with_counter
def create_document(self, *, data: List[Table], index: str) -> str:
"""
Creates new index in elasticsearch, then routes traffic to the new index
instead of the old one
:return: str
"""
if not index:
raise Exception('Index cant be empty for creating document')
if not data:
LOGGING.warn('Received no data to upload to Elasticsearch')
return ''
return self._create_document_helper(data=data, index=index)
@timer_with_counter
def update_document(self, *, data: List[Table], index: str) -> str:
"""
Updates the existing index in elasticsearch
:return: str
"""
if not index:
raise Exception('Index cant be empty for updating document')
if not data:
LOGGING.warn('Received no data to upload to Elasticsearch')
return ''
return self._update_document_helper(data=data, index=index)
@timer_with_counter
def delete_document(self, *, data: List[str], index: str) -> str:
if not index:
raise Exception('Index cant be empty for deleting document')
if not data:
LOGGING.warn('Received no data to upload to Elasticsearch')
return ''
return self._delete_document_helper(data=data, index=index)
def _create_document_helper(self, data: List[Table], index: str) -> str:
# fetch indices that use our chosen alias (should only ever return one in a list)
indices = self._fetch_old_index(index)
for i in indices:
# build a list of elasticsearch actions for bulk upload
actions = self._build_index_actions(data=data, index_key=i)
# bulk create or update data
self._bulk_helper(actions)
return index
def _update_document_helper(self, data: List[Table], index: str) -> str:
# fetch indices that use our chosen alias (should only ever return one in a list)
indices = self._fetch_old_index(index)
for i in indices:
# build a list of elasticsearch actions for bulk update
actions = self._build_update_actions(data=data, index_key=i)
# bulk update existing documents in index
self._bulk_helper(actions)
return index
def _delete_document_helper(self, data: List[str], index: str) -> str:
# fetch indices that use our chosen alias
indices = self._fetch_old_index(index)
# set the document type
type = User.get_type() if index is USER_INDEX else Table.get_type()
for i in indices:
# build a list of elasticsearch actions for bulk deletion
actions = self._build_delete_actions(data=data, index_key=i, type=type)
# bulk delete documents in index
self._bulk_helper(actions)
return index
def _build_index_actions(self, data: List[Table], index_key: str) -> List[Dict[str, Any]]:
actions = list()
for item in data:
index_action = {'index': {'_index': index_key, '_type': item.get_type(), '_id': item.get_id()}}
actions.append(index_action)
actions.append(item.get_attrs_dict())
return actions
def _build_update_actions(self, data: List[Table], index_key: str) -> List[Dict[str, Any]]:
actions = list()
for item in data:
actions.append({'update': {'_index': index_key, '_type': item.get_type(), '_id': item.get_id()}})
actions.append({'doc': item.get_attrs_dict()})
return actions
def _build_delete_actions(self, data: List[str], index_key: str, type: str) -> List[Dict[str, Any]]:
return [{'delete': {'_index': index_key, '_id': id, '_type': type}} for id in data]
def _bulk_helper(self, actions: List[Dict[str, Any]]) -> None:
result = self.elasticsearch.bulk(actions)
if result['errors']:
# ES's error messages are nested within elasticsearch objects and can
# fail silently if you aren't careful
LOGGING.error('Error during Elasticsearch bulk actions')
LOGGING.debug(result['items'])
return
def _fetch_old_index(self, alias: str) -> List[str]:
"""
Retrieve all indices that are currently tied to alias
(Can most often expect only one index to be returned in this list)
:return: list of elasticsearch indices
"""
try:
indices = self.elasticsearch.indices.get_alias(alias).keys()
return indices
except NotFoundError:
LOGGING.warn('Received index not found error from Elasticsearch', exc_info=True)
# create a new index if there isn't already one that is usable
new_index = self._create_index_helper(alias=alias)
return [new_index]
def _create_index_helper(self, alias: str) -> str:
def _get_mapping(alias: str) -> str:
if alias is USER_INDEX:
return USER_INDEX_MAP
elif alias is TABLE_INDEX:
return TABLE_INDEX_MAP
return ''
index_key = str(uuid.uuid4())
mapping: str = _get_mapping(alias=alias)
self.elasticsearch.indices.create(index=index_key, body=mapping)
# alias our new index
index_actions = {'actions': [{'add': {'index': index_key, 'alias': alias}}]}
self.elasticsearch.indices.update_aliases(index_actions)
return index_key