Python创建ES索引

# pip install elasticsearch
from datetime import datetime
from elasticsearch import Elasticsearch

es_servers = [{
    "host": "10.10.6.225",
    "port": "9200"
}]

es = Elasticsearch(es_servers)

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res)

res = es.get(index="test-index", doc_type='tweet', 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'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
原文地址:https://www.cnblogs.com/littlehb/p/8492462.html