from datetime import datetime
from elasticsearch import Elasticsearch
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['created'])
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"])
True {'author': 'kimchy', 'text': 'Elasticsearch: cool. bonsai cool.', 'timestamp': '2017-08-04T16:13:53.099249'} Got 1 Hits: 2017-08-04T16:13:53.099249 kimchy: Elasticsearch: cool. bonsai cool.
# by default we connect to localhost:9200
es = Elasticsearch()
es = Elasticsearch(['http://elastic:changeme@localhost:9200'])
es.count()
{'_shards': {'failed': 0, 'successful': 331, 'total': 331}, 'count': 27392262}
es.cluster.health()
{'active_primary_shards': 331, 'active_shards': 331, 'active_shards_percent_as_number': 50.07564296520424, 'cluster_name': 'docker-cluster', 'delayed_unassigned_shards': 0, 'initializing_shards': 0, 'number_of_data_nodes': 1, 'number_of_in_flight_fetch': 0, 'number_of_nodes': 1, 'number_of_pending_tasks': 0, 'relocating_shards': 0, 'status': 'yellow', 'task_max_waiting_in_queue_millis': 0, 'timed_out': False, 'unassigned_shards': 330}
es.indices.create(index='my-index', ignore=400)
es.indices.create(index='my-index-2', ignore=400)
{'acknowledged': True, 'shards_acknowledged': True}
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
{'_id': '42', '_index': 'my-index', '_shards': {'failed': 0, 'successful': 1, 'total': 2}, '_type': 'test-type', '_version': 1, 'created': True, 'result': 'created'}
es.index(index="my-index-2",doc_type='testtype', body={"c1": "data_2", "timestamp": datetime.now()})
{'_id': 'AV2uJeVTnRhWZ6-3akci', '_index': 'my-index-2', '_shards': {'failed': 0, 'successful': 1, 'total': 2}, '_type': 'testtype', '_version': 1, 'created': True, 'result': 'created'}
indexes = es.indices.get('my-*')
for index in indexes:
print(index)
my-index-2 my-index
indexes
{'my-index': {'aliases': {}, 'mappings': {'test-type': {'properties': {'any': {'fields': {'keyword': {'ignore_above': 256, 'type': 'keyword'}}, 'type': 'text'}, 'timestamp': {'type': 'date'}}}}, 'settings': {'index': {'creation_date': '1501864707000', 'number_of_replicas': '1', 'number_of_shards': '5', 'provided_name': 'my-index', 'uuid': 'Xvc4JU1ST_SRU1G0f5S0YQ', 'version': {'created': '5040299'}}}}, 'my-index-2': {'aliases': {}, 'mappings': {}, 'settings': {'index': {'creation_date': '1501864898718', 'number_of_replicas': '1', 'number_of_shards': '5', 'provided_name': 'my-index-2', 'uuid': '_l61I9tFSNClH2TpYmZ7zA', 'version': {'created': '5040299'}}}}}
es.get(index="my-index", doc_type="test-type", id=42)['_source']
{'any': 'data', 'timestamp': '2017-08-04T16:38:48.785593'}
es.indices.delete("my-index-2")
{'acknowledged': True}