#!/usr/bin/env python # coding: utf-8 # 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"]) # In[16]: # by default we connect to localhost:9200 es = Elasticsearch() # In[11]: es = Elasticsearch(['http://elastic:changeme@localhost:9200']) # In[18]: es.count() # In[17]: es.cluster.health() # In[33]: es.indices.create(index='my-index', ignore=400) es.indices.create(index='my-index-2', ignore=400) # In[20]: es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()}) # In[37]: es.index(index="my-index-2",doc_type='testtype', body={"c1": "data_2", "timestamp": datetime.now()}) # In[39]: indexes = es.indices.get('my-*') for index in indexes: print(index) # In[31]: indexes # In[21]: es.get(index="my-index", doc_type="test-type", id=42)['_source'] # In[40]: es.indices.delete("my-index-2")