使用python连接elasticsearch

官方文档地址: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"])
posted @ 2020-11-16 16:49  哈喽哈喽111111  阅读(1392)  评论(0编辑  收藏  举报