网络编程
位置:首页>> 网络编程>> Python编程>> 基于python操作ES实例详解

基于python操作ES实例详解

作者:太虚真人  发布时间:2021-01-06 05:08:28 

标签:python,操作,ES

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

安装


pip install elasticsearch5 # 安装对应版本的模块

创建ES对象


from elasticsearch5 import Elasticsearch

# elasticsearch集群服务器的地址
ES = [
 '127.0.0.1:9200'
]

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

搜索数据


query = {
 'query': {
   'bool': {
     'must': [
       {'match': {'_all': 'python web'}}
     ],
     'filter': [
       {'term': {'status': 2}}
     ]
   }
 }
}
ret = es.search(index='articles', doc_type='article', body=query)

添加数据


doc = {
    'article_id': article.id,
    'user_id': article.user_id,
    'title': article.title
  }
es.index(index='articles', doc_type='article', body=doc, id=article.id)

基于python操作ES实例详解

来源:https://www.cnblogs.com/oklizz/p/11448389.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com