python中的Elasticsearch操作汇总

脚本专栏 发布日期:2024/10/24 浏览次数:1

正在浏览: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)

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

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。