Data Skeptic — Data Science, Machine Learning & AI Podcast
import requests
from pyelasticsearch.client import ElasticSearch
endpoint = 'https://search-test-vu2fwve5ykzm5tsjkut5q4idum.us-east-1.es.amazonaws.com'
es = ElasticSearch(endpoint, port=443)
es.index('contacts',
          'person',
          {'name': 'Joe Tester', 'age': 25, 'title': 'QA Master'},
           id=1)
Out[20]:
{'_id': '1',
 '_index': 'contacts',
 '_shards': {'failed': 0, 'successful': 1, 'total': 2},
 '_type': 'person',
 '_version': 1,
 'created': True,
 'result': 'created'}
docs = [{'id': 2, 'name': 'Jessica Coder', 'age': 32, 'title': 'Programmer'},
          {'id': 3, 'name': 'Freddy Tester', 'age': 29, 'title': 'Office Assistant'}]
es.bulk((es.index_op(doc, id=doc.pop('id')) for doc in docs),
          index='contacts',
          doc_type='person')
Out[21]:
{'errors': False,
 'items': [{'index': {'_id': '2',
    '_index': 'contacts',
    '_shards': {'failed': 0, 'successful': 1, 'total': 2},
    '_type': 'person',
    '_version': 1,
    'created': True,
    'result': 'created',
    'status': 201}},
  {'index': {'_id': '3',
    '_index': 'contacts',
    '_shards': {'failed': 0, 'successful': 1, 'total': 2},
    '_type': 'person',
    '_version': 1,
    'created': True,
    'result': 'created',
    'status': 201}}],
 'took': 14}
es.refresh('contacts')
Out[23]:
{'_shards': {'failed': 0, 'successful': 5, 'total': 10}}
es.get('contacts', 'person', 3)
Out[27]:
{'_id': '3',
 '_index': 'contacts',
 '_source': {'age': 29, 'name': 'Freddy Tester', 'title': 'Office Assistant'},
 '_type': 'person',
 '_version': 1,
 'found': True}
es.search('name:joe OR name:freddy', index='contacts')
Out[28]:
{'_shards': {'failed': 0, 'successful': 5, 'total': 5},
 'hits': {'hits': [{'_id': '1',
    '_index': 'contacts',
    '_score': 0.25811607,
    '_source': {'age': 25, 'name': 'Joe Tester', 'title': 'QA Master'},
    '_type': 'person'},
   {'_id': '3',
    '_index': 'contacts',
    '_score': 0.25811607,
    '_source': {'age': 29,
     'name': 'Freddy Tester',
     'title': 'Office Assistant'},
    '_type': 'person'}],
  'max_score': 0.25811607,
  'total': 2},
 'timed_out': False,
 'took': 6}
query = {
     'query': {
         'filtered': {
             'query': {
                 'query_string': {'query': 'name:tester'}
             },
             'filter': {
                 'range': {
                     'age': {
                         'from': 27,
                         'to': 37,
                     },
                 },
             },
         },
     },
}
es.search(query, index='contacts')
WARNING:elasticsearch:GET /contacts/_search [status:400 request:0.133s]
---------------------------------------------------------------------------
ElasticHttpError                          Traceback (most recent call last)
<ipython-input-26-42a0731641db> in <module>()
     16      },
     17 }
---> 18 es.search(query, index='contacts')

/Users/kylepolich/anaconda/lib/python2.7/site-packages/pyelasticsearch/client.pyc in decorate(*args, **kwargs)
     91                 elif k in convertible_args:
     92                     query_params[k] = kwargs.pop(k)
---> 93             return func(*args, query_params=query_params, **kwargs)
     94         return decorate
     95     return decorator

/Users/kylepolich/anaconda/lib/python2.7/site-packages/pyelasticsearch/client.pyc in search(self, query, **kwargs)
    857             http://www.elastic.co/guide/en/elasticsearch/reference/current/_the_search_api.html
    858         """
--> 859         return self._search_or_count('_search', query, **kwargs)
    860 
    861     @es_kwargs('df', 'analyzer', 'default_operator', 'source', 'routing')

/Users/kylepolich/anaconda/lib/python2.7/site-packages/pyelasticsearch/client.pyc in _search_or_count(self, kind, query, index, doc_type, query_params)
    834             [self._concat(index), self._concat(doc_type), kind],
    835             body,
--> 836             query_params=query_params)
    837 
    838     @es_kwargs('routing', 'size')

/Users/kylepolich/anaconda/lib/python2.7/site-packages/pyelasticsearch/client.pyc in send_request(self, method, path_components, body, query_params)
    283             status = exc.args[0]
    284             error_message = exc.args[1]
--> 285             self._raise_exception(status, error_message)
    286 
    287         return prepped_response

/Users/kylepolich/anaconda/lib/python2.7/site-packages/pyelasticsearch/client.pyc in _raise_exception(self, status, error_message)
    297             error_class = IndexAlreadyExistsError
    298 
--> 299         raise error_class(status, error_message)
    300 
    301     def _encode_json(self, value):

ElasticHttpError: (400, u'parsing_exception')
es.delete_index('contacts')
Out[27]:
{u'acknowledged': True}
 
 
 
 
import elasticsearch

host = 'YOURHOST.us-east-1.es.amazonaws.com'
awsauth = AWS4Auth(your_access_key, your_secret_key, region, 'es')

es = elasticsearch.Elasticsearch(
    hosts=[{'host': host, 'port': 443}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=elasticsearch.connection.RequestsHttpConnection
)
print(es.cluster.health())