elasticsearch.py 2.35 KB
Newer Older
崔为之's avatar
崔为之 committed
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/28 12:19
# @File        : elasticsearch.py
# @Description :
"""

import datetime

from application.extensions.init_elasticsearch import es


class ElasticsearchUtils:
    @classmethod
    def dsl(cls, _start: str, _end: str, size=10000) -> dict:
        """
        Setting dsl

        :param _start: start time
        :param _end: end time
        :param size: data number
        :return: dsl
        """
        _dsl = {
            "size": size,
            "query": {
                "bool": {
                    "must": [{
                        "match_phrase": {
                            "tag": {
                                "query": "global"
                            }
                        }
                    },
                        {
                        "range": {
                            "@timestamp": {
                                "gte": (datetime.datetime.strptime(_start, "%Y-%m-%d %H:%M:%S") -
                                        datetime.timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%SZ"),
                                "lte": (datetime.datetime.strptime(_end, "%Y-%m-%d %H:%M:%S") -
                                        datetime.timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%SZ")
                            }
                        }
                    }
                    ],
                    "filter": [{
                        "match_all": {}
                    }],
                    "should": [],
                    "must_not": []
                }
            }
        }
        return _dsl

    @classmethod
    def search(cls, _index: str, _dsl: dict, _scroll="5m") -> dict:
        """
        Search data

        :param _index: index name
        :param _dsl: dsl
        :param _scroll: scroll time
        :return: data after search
        """
        return es.search(index=_index, scroll=_scroll, body=_dsl)

    @classmethod
    def scroll_search(cls, _id,  _scroll="5m") -> dict:
        """
        Search data

        :param _id: scroll id
        :param _scroll: scroll time
        :return: data after search by scroll
        """
        return es.scroll(scroll_id=_id, scroll=_scroll, request_timeout=30)