python es 多条件查询

from elasticsearch import Elasticsearch
from elasticsearch import helpers
from requests.auth import HTTPBasicAuth
from sqlalchemy import create_engine
import datetime
import time


t = datetime.datetime.today().date()
y = t - datetime.timedelta(days=1)
t=str(t)
y = str(y)
ytimearray = time.strptime(y, "%Y-%m-%d")
ytime = int(time.mktime(ytimearray))
ttimearray = time.strptime(t, "%Y-%m-%d")
ttime = int(time.mktime(ttimearray))

SEARCHCONFIG = {
    'es': {
        'address': ***,
        'username': ***,
        'password': '***',
        'indexlesson': '***',
        'indexlessontype': '***',
    }
}


class SearchConfig():

    def __init__(self, c):
        self.address = c['es']['address']
        self.auth = [c['es']['username'], c['es']['password']]
        self.indexlesson = c['es']['indexlesson']
        self.indexlessontype = c['es']['indexlessontype']


class Search:
    """
    搜索核心类
    """
    config = None
    es = None

    def __init__(self, config=None):
        if config is not None:
            self.config = config
            self.es = Elasticsearch(self.config.address, http_auth=config.auth)
        
    def get_all_lesson(self, is_live=1):

        try:
            indexType = self.config.indexlessontype
            res = self.es.get(index=self.config.indexlesson, doc_type=indexType, is_live=is_live)
            # print(res)
        except Exception as e:
            print(e)
            print('false')
            return None
    def get_yeasterday_all(self):

        body={
            "query":{
                "bool":{
                    "must":{
                        "range":{
                            "learning_time":{
                                "gte": ytime,
                                "lte": ttime

                            }
                        }
                    }
                }
            }
        }

        res = self.es.search(index=self.config.indexlesson, doc_type=self.config.indexlessontype, body=body)
        print(res['hits']['total'])

    def get_yestarday_live(self):
        '''
        得到昨天的直播数
        :return:
        '''


        body = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "is_live": 1
                            }
                        },
                        {
                            "term": {
                                "is_online": 1
                            }
                        },
                        {
                            "range":{
                                "learning_time":{
                                    "gte": ytime,
                                    "lte": ttime

                                }
                            }
                        }

                    ]

                },

            }
        }

        res = self.es.search(index=self.config.indexlesson, doc_type=self.config.indexlessontype, body=body)
        print(res['hits']['total'])

    def get_yesterday_no_live(self):

        body={
            "query":{
                "bool":{
                    "must":[ {
                        "range":{
                            "learning_time":{
                                    "gte": ytime,
                                    "lte": ttime

                                }
                        }
                    },
                        {
                            "bool":{
                                "should":[{
                                    "term":{
                                        "is_live":0
                                    }


                                },
                                    {
                                        "term": {
                                            "is_live": 0
                                        }
                                    }

                                ],

                            }
                        }

                    ]
                }
            }
        }

        res = self.es.search(index=self.config.indexlesson, doc_type=self.config.indexlessontype, body=body)
        print(res['hits']['total'])


#
config = SearchConfig(SEARCHCONFIG)
search = Search(config)
search.get_yeasterday_all()
search.get_yestarday_live()
search.get_yesterday_no_live()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值