In [1]:
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.
In [16]:
# by default we connect to localhost:9200
es = Elasticsearch()
In [11]:
es = Elasticsearch(['http://elastic:[email protected]:9200'])
In [18]:
es.count()
Out[18]:
{'_shards': {'failed': 0, 'successful': 331, 'total': 331}, 'count': 27392262}
In [17]:
es.cluster.health()
Out[17]:
{'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}
In [33]:
es.indices.create(index='my-index', ignore=400)
es.indices.create(index='my-index-2', ignore=400)
Out[33]:
{'acknowledged': True, 'shards_acknowledged': True}
In [20]:
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
Out[20]:
{'_id': '42',
 '_index': 'my-index',
 '_shards': {'failed': 0, 'successful': 1, 'total': 2},
 '_type': 'test-type',
 '_version': 1,
 'created': True,
 'result': 'created'}
In [37]:
es.index(index="my-index-2",doc_type='testtype', body={"c1": "data_2", "timestamp": datetime.now()})
Out[37]:
{'_id': 'AV2uJeVTnRhWZ6-3akci',
 '_index': 'my-index-2',
 '_shards': {'failed': 0, 'successful': 1, 'total': 2},
 '_type': 'testtype',
 '_version': 1,
 'created': True,
 'result': 'created'}
In [39]:
indexes = es.indices.get('my-*')
for index in indexes:
  print(index)
my-index-2
my-index
In [31]:
indexes
Out[31]:
{'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'}}}}}
In [21]:
es.get(index="my-index", doc_type="test-type", id=42)['_source']
Out[21]:
{'any': 'data', 'timestamp': '2017-08-04T16:38:48.785593'}
In [40]:
es.indices.delete("my-index-2")
Out[40]:
{'acknowledged': True}