.. include:: ../services/es.rst 如何使用 ============ 首先,执行 `pip install -t vendor elasticsearch` 安装 elasticsearch 到应用的 vendor 目录下。将 vendor 目录加入到代码仓库里提交,然后就可以在应用里使用 Elasticsearch 服务了。 .. code-block:: python from datetime import datetime from elasticsearch import ElasticSearch es = Elasticsearch('https://用户名:密码@es.sinacloud.com') doc = { 'author': 'kimchy', 'text': 'Elasticsearch: cool. bonsai cool.', 'timestamp': datetime.now(), } print es.index(index="索引名", doc_type='tweet', id=1, body=doc) print es.get(index="索引名", doc_type='tweet', id=1) res = es.search(index="索引名", body={"query": {"match_all": {}}}) print("Got %d Hits:" % res['hits']['total']) for hit in res['hits']['hits']: print("%(timestamp)s %(author)s: %(text)s" % hit["_source"]) API 文档:https://elasticsearch-py.readthedocs.io/en/master/