python编写es脚本_python中的Elasticsearch操作汇总

这篇文章主要介绍了python中的Elasticsearch操作汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

导入包

from elasticsearch import Elasticsearch

本地连接

es = Elasticsearch(['127.0.0.1:9200'])

创建索引

es.indices.create(index="python_es01",ignore=400)

ingore=400 ingore是忽略的意思,400是未找到

删除索引

es.indices.delete(index="python_es01")

检查索引是否存在

es.indices.exists(index="python_es01")

插入数据

es.index(index="python_es01",doc_type="doc",id=1,body={"name":"kitty","age":50})

同时也可以不加id,即

es.index(index="python_es01",doc_type="doc",body={"name":"kitty","age":10})

查询操作

按id查询

result = es.get(index="python_es01",doc_type="doc",id=1)

会有一个返回值

全查

body= {

"query":{

"match_all":{}

}

}

result = es.search(index="python_es01",body=body)

使用id的用GET,其他search

删除操作

result = es.delete(index="goods",doc_type="type1",id=2)

按查询结果删除

result = es.delete_by_query(index="goods",body=body)

建立mapping

body = {

"mappings": {

"properties": {

"name": {

"type": "text"

},

"price": {

"type": "long"

}

}

}

}

result = es.indices.create(index="shang",body=body)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值