官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/overview.html

安装的时候注意版本,要与使用的elasticsearch兼容

The library is compatible with all Elasticsearch versions since 0.90.x but you have to use a matching major version:

For Elasticsearch 7.0 and later, use the major version 7 (7.x.y) of the library.

For Elasticsearch 6.0 and later, use the major version 6 (``6.x.y`) of the library.

For Elasticsearch 5.0 and later, use the major version 5 (5.x.y) of the library.

For Elasticsearch 2.0 and later, use the major version 2 (2.x.y) of the library, and so on.

The recommended way to set your requirements in your setup.py or requirements.txt is::

# Elasticsearch 7.x
elasticsearch>=7,<8 # Elasticsearch 6.x
elasticsearch>=6,<7 # Elasticsearch 5.x
elasticsearch>=5,<6 # Elasticsearch 2.x
elasticsearch>=2,<3

安装步骤地址:https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/installation.html

$ python -m pip install elasticsearch
$ python -m pip install elasticsearch[async] # If your application uses async/await in Python you can install with the async extra

简单使用步骤:

>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch # By default we connect to localhost:9200
>>> es = Elasticsearch() # Datetimes will be serialized...
>>> es.index(index="my-index-000001", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
{'_id': '42', '_index': 'my-index-000001', '_type': 'test-type', '_version': 1, 'ok': True} # ...but not deserialized
>>> es.get(index="my-index-000001", doc_type="test-type", id=42)['_source']
{'any': 'data', 'timestamp': '2013-05-12T19:45:31.804229'}

api地址:https://elasticsearch-py.readthedocs.io/en/master/api.html

api文档下载地址:https://readthedocs.org/projects/elasticsearch-py/downloads/

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", id=1, body=doc)
print(res['result'])
res = es.get(index="test-index", id=1)
print(res['_source'])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

使用python连接elasticsearch的更多相关文章

  1. python连接 elasticsearch 查询数据,支持分页

    使用python连接es并执行最基本的查询 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:92 ...

  2. Python操作ElasticSearch

    Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...

  3. python操作Elasticsearch (一、例子)

    E lasticsearch是一款分布式搜索引擎,支持在大数据环境中进行实时数据分析.它基于Apache Lucene文本搜索引擎,内部功能通过ReST API暴露给外部.除了通过HTTP直接访问El ...

  4. python实现elasticsearch操作-CRUD API

    python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...

  5. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  6. python连接mysql的驱动

    对于py2.7的朋友,直接可以用MySQLdb去连接,但是MySQLdb不支持python3.x.这是需要注意的~ 那应该用什么python连接mysql的驱动呢,在stackoverflow上有人解 ...

  7. paip. 解决php 以及 python 连接access无效的参数量。参数不足,期待是 1”的错误

    paip. 解决php 以及 python 连接access无效的参数量.参数不足,期待是 1"的错误 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源 ...

  8. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  9. paip.python连接mysql最佳实践o4

    paip.python连接mysql最佳实践o4 python连接mysql 还使用了不少时间...,相比php困难多了..麻烦的.. 而php,就容易的多兰.. python标准库没mysql库,只 ...

随机推荐

  1. LDAP+Gitea统一认证Git服务器账户管理openLdap和微软的ad

    很多时候我们需要管理多个内容管理系统,比如Jira.Jenkins.GitEA/Gitlab等等各种管理系统,我们需要每一套管理系统每个人都管理一套密码,每套系统每套密码简直是一种灾难,于是LDAP可 ...

  2. Chrome安装Vue.js devtool F12无效

    要安装 vue-devtools-4.1.4_0 链接: https://pan.baidu.com/s/1aeUxKJEUDW0U_i6uuAZFvQ 提取码: 4btc vue-devtools- ...

  3. vivo官网APP全机型UI适配方案

    vivo 互联网客户端团队- Xu Jie 日益新增的机型,给开发人员带来了很多的适配工作.代码能不能统一.apk能不能统一.物料如何选取.样式怎么展示等等都是困扰开发人员的问题,本方案就是介绍不同机 ...

  4. HTTP协议之Expect爬坑

    前言 今天,在对接一个第三方平台开放接口时遇到一个很棘手的问题,根据接口文档组装好报文,使用HttpClient发起POST请求时一直超时,对方服务器一直不给任何响应. 发起请求的代码如下: usin ...

  5. SPFA算法(SLF优化)2022.7.8更新

    SPFA可能会被卡掉,能用dijkstra就别用SPFA,代码较长,但我已尽力做到解释,请耐心看下去,存储为邻接表存储. #include<bits/stdc++.h> #define i ...

  6. MySQL基本操作笔记

    一.数值类型 1.常量(1)字符串常量 ASCII字符串常量占一个字节 例如:'Hello Word' Unicode字符串常量占两个字节 例如:N'Hello Word' mysql> sel ...

  7. 常用的函数式接口_Prodicate接口_默认方法or&negate和常用的函数式接口_Predicate接口练习_集合信息筛选

    常用的函数式接口_Prodicate接口_默认方法or&negate OR package com.yang.Test.PredicateStudy; import java.util.fun ...

  8. 字符编码和字符集和编码引出的问题_FileReader读取GBK格式的文件

    字符编码 计算机中鵆的信息都是用二进制数表示的,而我们在屏幕上看到的数字.英文.标点符号.汉子等字符都是二进制数转换之后的结果.按照某种规则,将字符存储到计算机中,称为编码.反之,将存储在计算机中的二 ...

  9. 20220722-Java可变参数

    Java可变参数总结 来源:B站韩顺平老师的Java入门教学 代码示例如下: public void f(String str,int... nums){ 方法体; } 代码示例如下: public ...

  10. 关于标准IO缓冲区的问题

    关于标准IO缓冲区的问题 按照标准IO缓冲区可以分为三类: 不缓存类型: 一旦有数据,直接将数据写入到文件 行缓冲类型: 同全缓冲类型 遇到\n时,将数据写入文件 全缓冲类型: 当程序结束,将数据冲洗 ...