基于python操作ES实例详解

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

相关文章

Python 内置函数globals()和locals()对比详解

这篇文章主要介绍了Python globals()和locals()对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Pytho...

python交互式图形编程实例(二)

本文实例为大家分享了python交互式图形编程的第二部分代码,供大家参考,具体内容如下 #!/usr/bin/env python3 # -*- coding: utf-8 -*-...

python寻找list中最大值、最小值并返回其所在位置的方法

实例如下所示: c = [-10,-5,0,5,3,10,15,-20,25] print c.index(min(c)) # 返回最小值 print c.index(max(c)...

python实现log日志的示例代码

源代码: # coding=utf-8 import logging import os import time LEVELS={'debug':logging.DEBUG,\...

Python线程池模块ThreadPoolExecutor用法分析

本文实例讲述了Python线程池模块ThreadPoolExecutor用法。分享给大家供大家参考,具体如下: python3内置的有Threadingpool和ThreadPoolEx...