python操作elasticsearch (一)

一、连接

from elasticsearch import Elasticsearch
# elasticsearch集群服务器的地址,IP可以是localhost,也可为指定的远程IP地址
ES = ['IP:9200']

# 创建es客户端
# sniff_on_start:启动前嗅探es集群服务器  sniff_on_connection_fail:es集群服务器结点连接异常时是否刷新es节点信息 sniffer_timeout:每多少秒刷新节点信息
es = Elasticsearch(ES, sniff_on_start=True, sniff_on_connection_fail=True, sniffer_timeout=60)
print(es)

二、操作

  • 创建索引
    result = es.indices.create(index='news',ignore=400)
    print(result)   # acknowledged 字段表示创建操作执行是否成功
    
  • 删除索引
     result = es.indices.delete(index='new', ignore=[400, 404])
     print(result)
    
  • 插入数据
    data_list = [
    {  "title": "标题1",
        "datetime": "2021-09-27",
    },{   
        "title": "标题2",
        "datetime": "2021-09-27",
    }
    ]
    
    n_id=0
    for i in data_list:
        n_id +=1
        result= es.create(index="news",body=i,id=n_id)
        print(result)
    
    
  • 更新数据
    result = es.update(index='news',doc_type="article",id=1,body=data)
    print(result)
    
  • 删除数据
    result = es.delete(index='news',id=1)
    print(result)
    
  • 查询数据
    q3 = {"query":{
    	"bool":{
    	 	"must":[
    	 	{"match":{"datetime":"2021-09-27"}},
    	 	{"match_phrase_prefix":{"title":"标题*"}},
    	 	]
    	 }
       }
    }
    
    result = es.search(index='table_name', body=q)
    print(result)
    if result['hits']['hits']:
        for i in result['hits']['hits']:
             print(i['_source'])
    
    未完待续~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
利用Python操作Elasticsearch可以使用elasticsearchelasticsearch-dsl这两个库,也可以使用更为简单的es-pandas库。其中,elasticsearch库提供了与Elasticsearch交互的低级接口,而elasticsearch-dsl库则提供了更高级别的查询构建器和对象映射器。es-pandas库则提供了一种将Elasticsearch数据转换为pandas DataFrame的简单方法。 以下是一个使用elasticsearch库进行查询的例子: ```python from elasticsearch import Elasticsearch # 连接到Elasticsearch es = Elasticsearch() # 查询所有文档 res = es.search(index="my_index", body={"query": {"match_all": {}}}) # 输出结果 for hit in res['hits']['hits']: print(hit['_source']) ``` 以下是一个使用elasticsearch-dsl库进行查询的例子: ```python from elasticsearch import Elasticsearch from elasticsearch_dsl import Search # 连接到Elasticsearch es = Elasticsearch() # 创建查询对象 s = Search(using=es, index="my_index").query("match", title="python") # 执行查询并输出结果 response = s.execute() for hit in response: print(hit.title) ``` 以下是一个使用es-pandas库将Elasticsearch数据转换为pandas DataFrame的例子: ```python from es_pandas import es_pandas # 将Elasticsearch数据转换为pandas DataFrame df = es_pandas.DataFrame({ "host": {"field": "host.keyword"}, "response": {"field": "response_time_ms"}, "timestamp": {"field": "@timestamp", "dtype": "datetime64[ns]"} }, es_url="http://localhost:9200", es_index_pattern="my_index-*") # 输出DataFrame print(df.head()) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值