python |elasticsearchs操作es的例子

复制代码
from elasticsearch import Elasticsearch
import json
import time


es = Elasticsearch(['es地址:9200'],ignore=[400, 405, 502]) 




def create_user(plat,gender,b_year,b_month,uni_ids = []):
    """增数据"""
    for uni_id in uni_ids:
        body = {
        "uni_id":uni_id,
        "customer_from_plat":plat,
        "gender":gender,
        "constellation":12,
        "birth_year":b_year,
        "birth_month":b_month,
        "state":"41",
        "city":"410300000000",
        "district":"410302000000",
        "email_suffix":"163.com",
        "customer_child":{
        "name":"customer"
        }
        }
        print(body)
        es.index(index="index_customer_yangbo",doc_type="customer",id= uni_id,body=body)
        time.sleep(1)



def del_date(ids=[]):
    """删数据"""
    for i in ids:
        es.delete(index="index_customer_yangbo", doc_type="customer", id=i)
        print(f"删除了_id为{i}的值")

def sc_date(ids=[]):
    body = {
    "query":{
    "terms":{
    "_id":ids
    }
    }
    }
    res = es.search(index="index_customer_yangbo",body=body)           #查询
    print(json.dumps(res))





create_user("JOS","F","2019","11",["user20","user21","user22"])
#sc_date(["user20","user21","user22"])
#del_date(["user20","user21","user22"])
复制代码

 

posted @   AAA五金批发王建军  阅读(577)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2018-11-07 chromeDriver 地址
2017-11-07 网络知识===wireshark抓包数据分析(一)
2017-11-07 网络知识===wireshark抓包出现“TCP segment of a reassembled PDU”的解释(载)
2017-11-07 网络知识===wireshark抓包,三次握手分析
点击右上角即可分享
微信分享提示