In [8]:
import os

from elasticsearch import Elasticsearch
import requests

In [9]:
response = requests.get('http://star-api.herokuapp.com/api/v1/stars/Sun')

In [10]:
response.json()


Out[10]:
{u'absmag': 4.85,
 u'appmag': -26.72,
 u'colorb_v': 0.65,
 u'created_at': u'2014-11-08T14:49:09.334Z',
 u'dcalc': 0.0,
 u'distly': 0.0,
 u'hipnum': 0.0,
 u'id': 53794,
 u'label': u'Sun',
 u'lum': 0.8913,
 u'plx': 0.0,
 u'plxerr': 0.0,
 u'speed': 0.0,
 u'texnum': 1.0,
 u'updated_at': u'2014-11-08T14:49:09.334Z',
 u'vx': 0.0,
 u'vy': 0.0,
 u'vz': 0.0,
 u'x': 0.0,
 u'y': 0.0,
 u'z': 0.0}

In [11]:
es = Elasticsearch('192.168.59.103:32770')

In [12]:
es.info()


Out[12]:
{u'cluster_name': u'elasticsearch',
 u'name': u'Worm',
 u'status': 200,
 u'tagline': u'You Know, for Search',
 u'version': {u'build_hash': u'2aaf797f2a571dcb779a3b61180afe8390ab61f9',
  u'build_snapshot': False,
  u'build_timestamp': u'2015-04-27T08:06:06Z',
  u'lucene_version': u'4.10.4',
  u'number': u'1.4.5'}}

In [13]:
es.indices.create(index='stars')


Out[13]:
{u'acknowledged': True}

In [14]:
sun = response.json()

In [15]:
es.index(index='stars', doc_type='star', body=sun)


Out[15]:
{u'_id': u'AU2iEZFLZqJfgSIvzVRu',
 u'_index': u'stars',
 u'_type': u'star',
 u'_version': 1,
 u'created': True}

In [16]:
es.search()


Out[16]:
{u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
 u'hits': {u'hits': [{u'_id': u'AU2iEZFLZqJfgSIvzVRu',
    u'_index': u'stars',
    u'_score': 1.0,
    u'_source': {u'absmag': 4.85,
     u'appmag': -26.72,
     u'colorb_v': 0.65,
     u'created_at': u'2014-11-08T14:49:09.334Z',
     u'dcalc': 0.0,
     u'distly': 0.0,
     u'hipnum': 0.0,
     u'id': 53794,
     u'label': u'Sun',
     u'lum': 0.8913,
     u'plx': 0.0,
     u'plxerr': 0.0,
     u'speed': 0.0,
     u'texnum': 1.0,
     u'updated_at': u'2014-11-08T14:49:09.334Z',
     u'vx': 0.0,
     u'vy': 0.0,
     u'vz': 0.0,
     u'x': 0.0,
     u'y': 0.0,
     u'z': 0.0},
    u'_type': u'star'}],
  u'max_score': 1.0,
  u'total': 1},
 u'timed_out': False,
 u'took': 56}

In [17]:
response = requests.get('http://star-api.herokuapp.com/api/v1/stars')

In [18]:
for star in response.json():
   es.index(index='stars', doc_type='star', body=star)

In [19]:
search_results = es.search()

In [20]:
search_results['hits']['total']


Out[20]:
422

In [21]:
es.search(index='stars',
          doc_type='star',
          body={'query': {'term': {'label': 'sun'}}})


Out[21]:
{u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
 u'hits': {u'hits': [{u'_id': u'AU2iEZFLZqJfgSIvzVRu',
    u'_index': u'stars',
    u'_score': 4.8815637,
    u'_source': {u'absmag': 4.85,
     u'appmag': -26.72,
     u'colorb_v': 0.65,
     u'created_at': u'2014-11-08T14:49:09.334Z',
     u'dcalc': 0.0,
     u'distly': 0.0,
     u'hipnum': 0.0,
     u'id': 53794,
     u'label': u'Sun',
     u'lum': 0.8913,
     u'plx': 0.0,
     u'plxerr': 0.0,
     u'speed': 0.0,
     u'texnum': 1.0,
     u'updated_at': u'2014-11-08T14:49:09.334Z',
     u'vx': 0.0,
     u'vy': 0.0,
     u'vz': 0.0,
     u'x': 0.0,
     u'y': 0.0,
     u'z': 0.0},
    u'_type': u'star'},
   {u'_id': u'AU2iEeQoZqJfgSIvzVRv',
    u'_index': u'stars',
    u'_score': 4.8286414,
    u'_source': {u'absmag': 4.85,
     u'appmag': -26.72,
     u'colorb_v': 0.65,
     u'created_at': u'2014-11-08T14:49:09.334Z',
     u'dcalc': 0.0,
     u'distly': 0.0,
     u'hipnum': 0.0,
     u'id': 53794,
     u'label': u'Sun',
     u'lum': 0.8913,
     u'plx': 0.0,
     u'plxerr': 0.0,
     u'speed': 0.0,
     u'texnum': 1.0,
     u'updated_at': u'2014-11-08T14:49:09.334Z',
     u'vx': 0.0,
     u'vy': 0.0,
     u'vz': 0.0,
     u'x': 0.0,
     u'y': 0.0,
     u'z': 0.0},
    u'_type': u'star'}],
  u'max_score': 4.8815637,
  u'total': 2},
 u'timed_out': False,
 u'took': 64}

In [22]:
stats = es.indices.stats()

In [23]:
stats.keys()


Out[23]:
[u'_all', u'indices', u'_shards']

In [24]:
stats['indices'].keys()


Out[24]:
[u'stars']