Python教程

python 操作 ElasticSearch ES

本文主要是介绍python 操作 ElasticSearch ES,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
安装环境
pip3 install elasticsearch
#pip3 install urllib3-1.26.4-py2.py3-none-any.whl --no-deps
#pip3 install certifi-2020.12.5-py2.py3-none-any.whl --no-deps
#pip3 install elasticsearch-7.12.1-py2.py3-none-any.whl


import time
from elasticsearch import Elasticsearch
host="192.168.1.1"
port=9200
def init():
    es = Elasticsearch(hosts=host, port=9200, timeout=200)
    # es = Elasticsearch(hosts=host, port=9200, timeout=200 , http_auth=())
    return es
def search(index,type,q):
    es = init()
    if not q:
        q={"query":{"match_all":{}}}
    query = es.search(index=index, doc_type=type, body=q)
    # es查询出的结果第一页
    results = query['hits']['hits']
    # es查询出的结果总量
    total = query['hits']['total']
    # 游标用于输出es查询出的所有结果
    # scroll_id = results['_scroll_id']
    data={}
    for hit in results:
        print(hit['_source'])
        for k in hit['_source'].keys():
            data[k]=hit['_source'][k]
    return data

def create_index(index):
    es = init()
    if es.indices.exists(index=index) is not True:
        res = es.indices.create(index=index)
        return res
def write_list(index,type,id=None,list=[]):
    es = init()
    for item in list:
        if not id:
            res = es.index(index=index, doc_type=type, body=item)
        else:
            res = es.index(index=index, doc_type=type,id=id, body=item)
def write_row(index,type,id=None,dic={}):
    es = init()
    dic["date"]=time.strftime("%Y-%m-%dT%H:%M:%S+0800",time.localtime())
    if not id:
        res = es.index(index=index, doc_type=type, body=dic)
    else:
        res = es.index(index=index, doc_type=type, id=id, body=dic)
    return res

def delete(index,type,id):
    es = init()
    res = es.delete(index=index, doc_type=type, id=id)
    return res
def delete_index(index):
    es = init()
    res = es.indices.delete(index=index)
    return res
def delete_query(index,type,q):
    es = init()
    if not q:
        q = {"query": {"match_all": {}}}
    res = es.delete_by_query(index=index, doc_type=type,body=q)
    return res
if __name__=="__main__":
    # res=delete("test123123","data",'3ZYhiXkBFS00-fnQRMH_')
    delete_index("test")
    # print(res)
    # res=search("test","data",None)
    # print(res)
    # create_index("test","data")
    # create_index("test")
    # list = [
    #     {"cpu": 0.9,
    #      "count": 100,
    #      "date": time.strftime("%Y-%m-%dT%H:%M:%S+0800",time.localtime())
    #      },
    #     {"cpu": 0.5,
    #      "count": 50,
    #      "date": time.strftime("%Y-%m-%dT%H:%M:%S+0800",time.localtime())
    #      },
    # ]
    # write_list("vpn","data",list)
    # row={"cpu": 0.5,
    #      "count": 200,
    #      "date":time.strftime("%Y-%m-%dT%H:%M:%S+0800",time.localtime())
    #      }
    # write_row(index="test",type="data",id=int(time.time()),dic=row)
    # # print(time.strftime("%Y-%m-%dT%H:%M:%SZ",time.localtime()))

 

这篇关于python 操作 ElasticSearch ES的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!