前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python3操作Elasticsearch进行增删改查

Python3操作Elasticsearch进行增删改查

作者头像
双面人
发布2019-04-10 11:09:03
2.1K0
发布2019-04-10 11:09:03
举报
文章被收录于专栏:热爱IT

# -*- coding: utf-8 -*- from elasticsearch import Elasticsearch # 默认host为localhost,port为9200.但也可以指定host与port es = Elasticsearch() # 插入数据,index,doc_type名称可以自定义,id可以根据需求赋值,body为内容 es.index(index="my_index",doc_type="test_type",id=0,body={"name":"python","addr":"深圳"}) es.index(index="my_index",doc_type="test_type",id=1,body={"name":"python","addr":"深圳"}) #同样是插入数据,create() 方法需要我们指定 id 字段来唯一标识该条数据,而 index() 方法则不需要,如果不指定 id,会自动生成一个 id es.create(index="my_index",doc_type="test_type",id=1,body={"name":"python","addr":"深圳"}) #删除指定的index、type、id的文档 es.delete(index='indexName', doc_type='typeName', id=1) #删除index es.indices.delete(index='news', ignore=[400, 404]) query = {'query': {'match_all': {}}}# 查找所有文档 query1 = {'query': {'match': {'sex': 'famale'}}}# 删除性别为女性的所有文档 query2 = {'query': {'range': {'age': {'lt': 11}}}}# 删除年龄小于11的所有文档 query3 = {'query': {'term': {'name': 'jack'}}}# 查找名字叫做jack的所有文档 #删除所有文档 es.delete_by_query(index="my_index",doc_type="test_type",body=query) #get:获取指定index、type、id所对应的文档 es.get(index="my_index",doc_type="test_type",id=1) #search:查询满足条件的所有文档,没有id属性,且index,type和body均可为None result = es.search(index="my_index",doc_type="test_type",body=query) print(result['hits']['hits'][0])# 返回第一个文档的内容 #update:更新指定index、type、id所对应的文档 #更新的主要点: #1. 需要指定 id #2. body={"doc": <xxxx>} , 这个doc是必须的 es.update(index="my_index",doc_type="test_type",id=1,body={"doc":{"name":"python1","addr":"深圳1"}})

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档