当前位置:首页 » python教程 » 正文

基于python操作ES实例详解

看: 846次  时间:2021-02-19  分类 : python教程

这篇文章主要介绍了基于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博客。

标签:flask  requests  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!