mirror of https://github.com/jumpserver/jumpserver
perf: ES command log supports fuzzy search
parent
1e5a995917
commit
d933e296bc
|
@ -35,6 +35,7 @@ class OperateLogStore(ES, metaclass=Singleton):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exact_fields = {}
|
exact_fields = {}
|
||||||
|
fuzzy_fields = {}
|
||||||
match_fields = {
|
match_fields = {
|
||||||
'id', 'user', 'action', 'resource_type',
|
'id', 'user', 'action', 'resource_type',
|
||||||
'resource', 'remote_addr', 'org_id'
|
'resource', 'remote_addr', 'org_id'
|
||||||
|
@ -44,7 +45,7 @@ class OperateLogStore(ES, metaclass=Singleton):
|
||||||
}
|
}
|
||||||
if not config.get('INDEX'):
|
if not config.get('INDEX'):
|
||||||
config['INDEX'] = 'jumpserver_operate_log'
|
config['INDEX'] = 'jumpserver_operate_log'
|
||||||
super().__init__(config, properties, keyword_fields, exact_fields, match_fields)
|
super().__init__(config, properties, keyword_fields, exact_fields, fuzzy_fields, match_fields)
|
||||||
self.pre_use_check()
|
self.pre_use_check()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -123,7 +123,7 @@ def get_es_client_version(**kwargs):
|
||||||
|
|
||||||
class ES(object):
|
class ES(object):
|
||||||
|
|
||||||
def __init__(self, config, properties, keyword_fields, exact_fields=None, match_fields=None):
|
def __init__(self, config, properties, keyword_fields, exact_fields=None, fuzzy_fields=None, match_fields=None, **kwargs):
|
||||||
self.version = 7
|
self.version = 7
|
||||||
self.config = config
|
self.config = config
|
||||||
hosts = self.config.get('HOSTS')
|
hosts = self.config.get('HOSTS')
|
||||||
|
@ -140,7 +140,7 @@ class ES(object):
|
||||||
self.index = None
|
self.index = None
|
||||||
self.query_index = None
|
self.query_index = None
|
||||||
self.properties = properties
|
self.properties = properties
|
||||||
self.exact_fields, self.match_fields, self.keyword_fields = set(), set(), set()
|
self.exact_fields, self.match_fields, self.keyword_fields, self.fuzzy_fields = set(), set(), set(), set()
|
||||||
|
|
||||||
if isinstance(keyword_fields, Iterable):
|
if isinstance(keyword_fields, Iterable):
|
||||||
self.keyword_fields.update(keyword_fields)
|
self.keyword_fields.update(keyword_fields)
|
||||||
|
@ -148,6 +148,8 @@ class ES(object):
|
||||||
self.exact_fields.update(exact_fields)
|
self.exact_fields.update(exact_fields)
|
||||||
if isinstance(match_fields, Iterable):
|
if isinstance(match_fields, Iterable):
|
||||||
self.match_fields.update(match_fields)
|
self.match_fields.update(match_fields)
|
||||||
|
if isinstance(fuzzy_fields, Iterable):
|
||||||
|
self.fuzzy_fields.update(fuzzy_fields)
|
||||||
|
|
||||||
self.init_index()
|
self.init_index()
|
||||||
self.doc_type = self.config.get("DOC_TYPE") or '_doc'
|
self.doc_type = self.config.get("DOC_TYPE") or '_doc'
|
||||||
|
@ -314,6 +316,13 @@ class ES(object):
|
||||||
query: {k: v}
|
query: {k: v}
|
||||||
})
|
})
|
||||||
return _filter
|
return _filter
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def handle_fuzzy_fields(exact):
|
||||||
|
_filter = []
|
||||||
|
for k, v in exact.items():
|
||||||
|
_filter.append({ 'wildcard': { k: f'*{v}*' } })
|
||||||
|
return _filter
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_keyword(props: dict, field: str) -> bool:
|
def is_keyword(props: dict, field: str) -> bool:
|
||||||
|
@ -335,10 +344,12 @@ class ES(object):
|
||||||
keyword_fields = self.keyword_fields
|
keyword_fields = self.keyword_fields
|
||||||
exact_fields = self.exact_fields
|
exact_fields = self.exact_fields
|
||||||
match_fields = self.match_fields
|
match_fields = self.match_fields
|
||||||
|
fuzzy_fields = self.fuzzy_fields
|
||||||
|
|
||||||
match = {}
|
match = {}
|
||||||
search = []
|
search = []
|
||||||
exact = {}
|
exact = {}
|
||||||
|
fuzzy = {}
|
||||||
index = {}
|
index = {}
|
||||||
|
|
||||||
if index_in_field in kwargs:
|
if index_in_field in kwargs:
|
||||||
|
@ -360,6 +371,9 @@ class ES(object):
|
||||||
|
|
||||||
elif k in common_keyword_able:
|
elif k in common_keyword_able:
|
||||||
exact[f"{k}.keyword"] = v
|
exact[f"{k}.keyword"] = v
|
||||||
|
|
||||||
|
elif k in fuzzy_fields:
|
||||||
|
fuzzy[f"{k}.keyword"] = v
|
||||||
|
|
||||||
elif k in match_fields:
|
elif k in match_fields:
|
||||||
match[k] = v
|
match[k] = v
|
||||||
|
@ -405,6 +419,7 @@ class ES(object):
|
||||||
{'match': item} for item in search
|
{'match': item} for item in search
|
||||||
],
|
],
|
||||||
'filter': self.handle_exact_fields(exact) +
|
'filter': self.handle_exact_fields(exact) +
|
||||||
|
self.handle_fuzzy_fields(fuzzy) +
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
'range': {
|
'range': {
|
||||||
|
|
|
@ -27,11 +27,12 @@ class CommandStore(ES):
|
||||||
"type": "long"
|
"type": "long"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exact_fields = {'input', 'risk_level', 'user', 'asset', 'account'}
|
exact_fields = {}
|
||||||
|
fuzzy_fields = {'input', 'risk_level', 'user', 'asset', 'account'}
|
||||||
match_fields = {'input'}
|
match_fields = {'input'}
|
||||||
keyword_fields = {'session', 'org_id'}
|
keyword_fields = {'session', 'org_id'}
|
||||||
|
|
||||||
super().__init__(config, properties, keyword_fields, exact_fields, match_fields)
|
super().__init__(config, properties, keyword_fields, exact_fields, fuzzy_fields, match_fields)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def make_data(command):
|
def make_data(command):
|
||||||
|
|
Loading…
Reference in New Issue