基于python操作ES实例详解

yipeiwu_com5年前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设计】。

相关文章

wtfPython—Python中一组有趣微妙的代码【收藏】

wtfPython是github上的一个项目,作者收集了一些奇妙的Python代码片段,这些代码的输出结果会和我们想象中的不太一样; 通过探寻产生这种结果的内部原因,可以让我们对Pyth...

Python数据结构与算法之列表(链表,linked list)简单实现

Python 中的 list 并不是我们传统(计算机科学)意义上的列表,这也是其 append 操作会比 insert 操作效率高的原因。传统列表——通常也叫作链表(linked lis...

Python with用法实例

python中with可以明显改进代码友好度,比如: 复制代码 代码如下: with open('a.txt') as f:      prin...

深入解析Python编程中JSON模块的使用

JSON编码支持的基本数据类型为 None , bool , int , float 和 str , 以及包含这些类型数据的lists,tuples和dictionaries。 对于di...

Python面向对象程序设计之私有属性及私有方法示例

本文实例讲述了Python面向对象程序设计之私有属性及私有方法。分享给大家供大家参考,具体如下: 如果有一个对象,当需要对其进行修改属性时,有2种方法: (1)对象名.属性名=数据---...