Commit 187a55b6 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 663fbfb2
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# @Email : StudentCWZ@outlook.com # @Email : StudentCWZ@outlook.com
# @Date : 2023/10/28 12:19 # @Date : 2023/10/28 12:19
# @File : elasticsearch.py # @File : elasticsearch.py
# @Description : # @Description : A utility class to interact with Elasticsearch.
""" """
import datetime import datetime
...@@ -15,16 +15,24 @@ import datetime ...@@ -15,16 +15,24 @@ import datetime
from application.extensions.init_elasticsearch import es from application.extensions.init_elasticsearch import es
import datetime
from application.extensions.init_elasticsearch import es
class ElasticsearchUtil: class ElasticsearchUtil:
@classmethod @classmethod
def dsl(cls, _start: str, _end: str, size=5000) -> dict: 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 Returns:
:param _end: end time dict: The DSL query.
:param size: data number
:return: dsl
""" """
_dsl = { _dsl = {
"size": size, "size": size,
...@@ -61,27 +69,43 @@ class ElasticsearchUtil: ...@@ -61,27 +69,43 @@ class ElasticsearchUtil:
@classmethod @classmethod
def search(cls, _index: str, _dsl: dict, _scroll="5m") -> dict: def search(cls, _index: str, _dsl: dict, _scroll="5m") -> dict:
""" """
Search data Searches data in Elasticsearch.
:param _index: index name Args:
:param _dsl: dsl _index (str): Index name.
:param _scroll: scroll time _dsl (dict): DSL query.
:return: data after search _scroll (str, optional): Scroll time. Defaults to "5m".
Returns:
dict: Data after the search.
""" """
return es.search(index=_index, scroll=_scroll, body=_dsl) return es.search(index=_index, scroll=_scroll, body=_dsl)
@classmethod @classmethod
def scroll_search(cls, _id, _scroll="5m") -> dict: 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 Returns:
:param _scroll: scroll time dict: Data after the search by scroll.
:return: data after search by scroll
""" """
return es.scroll(scroll_id=_id, scroll=_scroll, request_timeout=30) return es.scroll(scroll_id=_id, scroll=_scroll, request_timeout=30)
@classmethod @classmethod
def insert_data(cls, index: str, doc_type: str, data: dict) -> None: 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) es.index(index=index, doc_type=doc_type, body=data)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment