python使用elasticsearch_Python操作elasticsearch接口

#!/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值