上一篇我们用了python的原生库requests来访问elasticsearch,其实还有包装好的elasticsearch-py库可以用。

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://172.16.11.2:9200'])  
#es = Elasticsearch(['https://user:secret@localhost:443'])
res = es.search(index="products", doc_type="product", body={"query": {"match": {"description": "Whi"}}})  
print("%d documents found" % res['hits']['total'])  
for doc in res['hits']['hits']:  
    print("%s) %s" % (doc['_id'], doc['_source']['description']))

新建文档的方法:

es.create(index="products", doc_type="", body={  
    "title":"The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972
})
comments powered by Disqus