python

How to get data from elasticsearch use python

Posted by

Using Python to get ElasticSearch data, we may use some open source Python libraries, such as elastic search-py, elastic search-dsl-py etc. But sometimes you just want to get some query data. These libraries are too complex.

We can get the query data by sending a request directly to Elastic Search. Let’s get started.

We need two python libs:

requests: Python HTTP for Humans.

requests_aws4auth: AWS signature version 4 signing process for the python requests module

Here is sample code:

import requests
import json
from requests_aws4auth import AWS4Auth

aws_key = 'xxxxx'
aws_secret = 'xxxx'
aws_reg = 'us-east-1'
awsAuth = False

url = 'https://sxxxxonaws.com.cn'

def getAwsAuth():
    if aws_key and aws_secret and aws_reg and awsAuth:
        print 'Creating AWS authorization'
        auth = AWS4Auth(aws_key, aws_secret, aws_reg, 'es')
        return auth
    return None

def getSearchByQuery(index, query):
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    auth = getAwsAuth()
    r = requests.get(url + index, auth=auth, data=json.dumps(query), headers = headers)
    if r.status_code != 200:
        raise Exception(TAG + r.text)
    return r.text

Test for the code:

getSearchByQuery(‘indice’, ‘{}’)