Manish Shegokar
Manish Shegokar

Reputation: 131

How to successfully write & run a SQL query in Python script which is built in Elasticsearch Kibana?

I have been trying to implement the SQL query in a python script that is working in ES Kibana but when I run it, I'm facing errors.

Following query working in Kibana

POST _opendistro/_sql?format=json
{
  "query": "select * from test_index where ['title_name.S'] like 'iron%' limit 10"
}

Following is the snippet of the function that I've been using in the Python script.

def sql(index):
    response = es.search(
        body ={
                "query":  "select * from " + index + " limit 10"
            }
        )
    return response

Following is the error that I get when I try to run the script.

enter image description here

Is there an alternative way to resolve or fix this issue? Any help would be much appreciated!

Upvotes: 0

Views: 893

Answers (1)

glenacota
glenacota

Reputation: 2547

assuming you're using the elasticsearch-py Python client, it seems you're using the search API, which issues GET requests to the _search endpoint of an index. What you want to send instead is a POST request to the _opendistro/_sql endpoint. You won't find an API for that in the Python client, but you can use e.g. the Python requests library to issue any HTTP request.

Upvotes: 1

Related Questions