官方文档地址: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"])

最新文章

  1. async &amp; await 的前世今生(Updated)
  2. eclipse的maven项目,如何使用java run main函数
  3. Websites for more Android development information
  4. T4:T4 笔记 + Trait 示例
  5. javascript生成n至m的随机整数
  6. 安装percona-toolkit提示的报错
  7. [Javascript] Object.assign()
  8. HIVE快速入门
  9. Hibernate(五)——面向对象查询语言和锁
  10. linux----命令替换
  11. SQLI LABS Challenges Part(54-65) WriteUp
  12. Office 365平台及其价值主张
  13. 让bind函数支持IE8浏览器的方法
  14. Vue(二)基础
  15. react事件绑定,事件传参,input单向数据绑定
  16. BZOJ2150部落战争——最小路径覆盖
  17. testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件
  18. 不能够连接到主机(名称为localhost)上的MySQL服务”
  19. Spring框架介绍及使用
  20. 小强学渲染之OpenGL的CPU管线

热门文章

  1. java通过注解指定顺序导入excel
  2. 基于ABP实现DDD--仓储实践
  3. 常用的函数式接口_Consumer接口和常用的函数式接口_Consumer接口的默认方法andThen
  4. 转:windows下定时执行备份数据库
  5. DBPack SQL Tracing 功能及数据加密功能详解
  6. 羽夏看Linux内核——段相关入门知识
  7. 点击>>>解锁Apache Hadoop Meetup 2021!
  8. Ubuntu添加非root用户到Docker用户组
  9. Java 数字转汉字
  10. Excel 查找函数(二):VLOOKUP