#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Eric Winn
# @Email : eng.eric.winn@gmail.com
# @Time : 19-5-22 下午8:40
# @Version : 1.0
# @File : elasticsearch
# @Software : PyCharm
import json
from elasticsearch import Elasticsearch
from common.utils import get_logger, get_signer
from django.conf import settings
signer = get_signer()
logger = get_logger()
class ESAPI(object):
'''
ES类封装
'''
def __init__(self):
'''
从settings表中读取ES认证信息
'''
self.hosts = settings.ELASTICSEARCH_HOST.split(',')
self.port = settings.ELASTICSEARCH_PORT
self.username = settings.ELASTICSEARCH_USERNAME
# 密码是加密的,使用时需要解密
self.password = signer.unsign((settings.ELASTICSEARCH_PASSWORD).encode())
self.client = None
def connect(self):
'''
连接ES
'''
self.client = Elasticsearch(
self.hosts,
http_auth=(self.username, self.password),
# scheme="https",
port=self.port)
def search(self, index=None, doc_type='tweet', body=None, **params):
'''
查询方法,如梦查询异常,根据ES的renturn格式,返回空数据
:param index:
:param doc_type: 默认tweet,在7版本中已经不需要了
:param body:
:param params:
:return:
'''
try:
ret = self.client.search(index, json.dumps(body), **params)
except Exception as e:
logger.error(e)
return {'hits': {'hits': [], 'total': 0}}
return ret
def create_index(self, index):
ret = self.client.indices.create(index=index, ignore=400)
return ret
def index(self, index, doc_type='tweet', id=None, body={}):
'''
增、改方法
:param index:
:param doc_type:
:param id:
:param body:
:return:
'''
class_type = body.get('classname').split(' ')[0]
# classname是用来存放自定义的index
data = self.search(index='classname', body={
"query": {
"bool": {
"should": [
{"match": {"index": index}},
{"match": {"type": class_type}}
],
"minimum_should_match": 2
}
}
})
if data['hits']['total'] is 0:
self.client.index(index='classname', doc_type=doc_type,
body={'index': index, 'classname': body.get('classname'), "type": class_type})
ret = self.client.index(index=index, doc_type=doc_type, id=id, body=json.dumps(body))
return ret
def get(self, index, id=0):
ret = self.client.get(index=index, id=id)
return ret