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