Search code examples
elasticsearchelasticsearch-pyelasticsearch-sql

Use SQL Access in Elasticsearch python client


I am trying to use the python client for elastic search to query an index using SQL access for elastic search. I want to query an index using sql syntax. How do i specify to elasticsearch that it has to read SQLsyntax?

 def searchText(text):
    t1 = time.time()
    es=Elasticsearch([{'host':'localhost','port':9200}])
    res =  es.search(index='global_res_todos_acco', size=10000, request_timeout=60,
                     body={'query':{
                                        "select * from global_res_todos_acco limit 100 where EntityList = " + text
                                    }
                          }
                     )
    GHList = []
    for hit in res['hits']['hits']:
            GHList.append(hit['_source']['Geohash7'])
    return(GHList)

i receive the following error in console

TypeError: Unable to serialize {'select * from global_res_todos_acco where EntityList = phuket indian food'}


Solution

  • If you're using the Python client, you need to use the es.sql.query() function:

    es=Elasticsearch([{'host':'localhost','port':9200}])
    es.sql.query(body={'query': 'select * from global_res_todos_acco...'})