diff --git a/application/utils/elasticsearch/elasticsearch.py b/application/utils/elasticsearch/elasticsearch.py index 43fa4ebfcd7809ef1cab24be953041684f406d58..209103bf77380a7f2686d92745fcd7f50f9c2e92 100644 --- a/application/utils/elasticsearch/elasticsearch.py +++ b/application/utils/elasticsearch/elasticsearch.py @@ -7,7 +7,7 @@ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/28 12:19 # @File : elasticsearch.py -# @Description : +# @Description : A utility class to interact with Elasticsearch. """ import datetime @@ -15,16 +15,24 @@ import datetime from application.extensions.init_elasticsearch import es +import datetime + +from application.extensions.init_elasticsearch import es + + class ElasticsearchUtil: @classmethod def dsl(cls, _start: str, _end: str, size=5000) -> dict: """ - Setting dsl + Constructs a DSL (Domain Specific Language) query for Elasticsearch. + + Args: + _start (str): Start time. + _end (str): End time. + size (int, optional): Number of data results. Defaults to 5000. - :param _start: start time - :param _end: end time - :param size: data number - :return: dsl + Returns: + dict: The DSL query. """ _dsl = { "size": size, @@ -61,27 +69,43 @@ class ElasticsearchUtil: @classmethod def search(cls, _index: str, _dsl: dict, _scroll="5m") -> dict: """ - Search data + Searches data in Elasticsearch. - :param _index: index name - :param _dsl: dsl - :param _scroll: scroll time - :return: data after search + Args: + _index (str): Index name. + _dsl (dict): DSL query. + _scroll (str, optional): Scroll time. Defaults to "5m". + + Returns: + dict: Data after the search. """ return es.search(index=_index, scroll=_scroll, body=_dsl) @classmethod def scroll_search(cls, _id, _scroll="5m") -> dict: """ - Search data + Searches data in Elasticsearch using scroll. + + Args: + _id (str): Scroll ID. + _scroll (str, optional): Scroll time. Defaults to "5m". - :param _id: scroll id - :param _scroll: scroll time - :return: data after search by scroll + Returns: + dict: Data after the search by scroll. """ return es.scroll(scroll_id=_id, scroll=_scroll, request_timeout=30) @classmethod def insert_data(cls, index: str, doc_type: str, data: dict) -> None: - # 插入数据 + """ + Inserts data into Elasticsearch. + + Args: + index (str): Index name. + doc_type (str): Document type. + data (dict): Data to be inserted. + + Returns: + None + """ es.index(index=index, doc_type=doc_type, body=data)