1# to install
2python -m pip install elasticsearch
3
4from datetime import datetime
5from elasticsearch import Elasticsearch
6es = Elasticsearch()
7
8doc = {
9 'author': 'kimchy',
10 'text': 'Elasticsearch: cool. bonsai cool.',
11 'timestamp': datetime.now(),
12}
13res = es.index(index="test-index", id=1, body=doc)
14print(res['result'])
15
16res = es.get(index="test-index", id=1)
17print(res['_source'])
18
19es.indices.refresh(index="test-index")
20
21res = es.search(index="test-index", body={"query": {"match_all": {}}})
22print("Got %d Hits:" % res['hits']['total']['value'])
23for hit in res['hits']['hits']:
24 print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
1from datetime import datetime
2from elasticsearch import Elasticsearch
3es = Elasticsearch()
4
5doc = {
6 'author': 'kimchy',
7 'text': 'Elasticsearch: cool. bonsai cool.',
8 'timestamp': datetime.now(),
9}
10res = es.index(index="test-index", id=1, body=doc)
11print(res['result'])
12
13res = es.get(index="test-index", id=1)
14print(res['_source'])
15
16es.indices.refresh(index="test-index")
17
18res = es.search(index="test-index", body={"query": {"match_all": {}}})
19print("Got %d Hits:" % res['hits']['total']['value'])
20for hit in res['hits']['hits']:
21 print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
22