栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Python操作Elasticsearch例子

Python操作Elasticsearch例子

分布式搜索引擎Elasticsearch非常受欢迎,对应Python库为elasticsearch;

from elasticsearch import Elasticsearch
# 连接引擎
es = Elasticsearch(hosts=['127.0.0.1:9200'],http_auth=('elastic','password'))
# 创建索引
es.indices.create(index='test-index', ignore=400)
# 删除索引
es.indices.delete(index='test-index', ignore=[400,404])

下段为对引擎进行增加和删除数据操作部分;

msg = {'author': 'user','text': 'this is a test msg'}
# 对索引添加数据,无需设置ID,会自动添加
res = es.index(index="test-index", document=msg)
# 对索引添加数据,需要设置ID,ID不能重复
res = es.create(index="test-index", id=1314, document=msg)

# 删除索引中对应ID数据
res = es.delete(index="test-index", id=1314)
# 删除索引中对应author为user的数据
query = {"query": {"match": {'author': 'user'}}}
res = es.delete_by_query(index="test-index", body=query)

 下段为使用引擎进行简单搜索的操作部分;

# 查询索引中对应author为user的数据
query = {"match": {'author': 'user'}}
res = es.search(index="test-index", query=query)
# 查询索引中所有数据
query = {"match_all":{}}
res = es.search(index="test-index", query=query)

# 查询索引的数据量
res = es.count(index="test-index")
# 查询索引中对应author为user的数据量
query = {"query": {"match": {'author': 'user'}}}
res = es.count(index="test-index", body=query)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号