In [1]:
from datetime import datetime
In [2]:
from elasticsearch import Elasticsearch
In [3]:
# by default we connect to localhost:9200
es = Elasticsearch()
In [4]:
# datetimes will be serialized
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
Out[4]:
In [5]:
# but not deserialized
es.get(index="my-index", doc_type="test-type", id=42)['_source']
Out[5]:
In [6]:
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime(2010, 10, 10, 10, 10, 10)
}
In [7]:
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
In [8]:
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])
In [9]:
es.indices.refresh(index="test-index")
Out[9]:
In [10]:
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
In [11]:
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
In [23]:
# Notice increased version
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
Out[23]:
In [12]:
es.get(index="my-index", doc_type="test-type", id=42)['_source']
Out[12]:
In [ ]: