Querying elastic with Python API

Hi Team

I have been given assignment of developing a Python API for connecting Elasticsearch and get the things that are requested. For this I have used Python flask. I am trying to test it with Postman. But I am failing to get the desired output. I am running Flask on Private IP address and Elasticsearch on Localhost. When I provide index name and JSON query in code I am able to fetch the details. When I pass a value of that index I am able to fetch Index details .But the same details if I provide in Postman I am getting below Error. How can I pass the JSON from Postman to Python-Flask Code as a Variable and what should be the URL route in flask application. Is it should be like <>/index/type/_search...?? Please give me some infer.

File "local/bin/elasticapi/elastic/elastic1.py", line 14, in index
elk = es.search(bodyin)
File "/.local/bin/elasticapi/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 84, in _wrapped
return func(*args, params=params, **kwargs)
File "/.local/bin/elasticapi/lib/python2.7/site-packages/elasticsearch/client/init.py", line 1548, in search
"GET", _make_path(index, doc_type, "_search"), params=params, body=body
File "/.local/bin/elasticapi/lib/python2.7/site-packages/elasticsearch/transport.py", line 318, in perform_request
body = self.serializer.dumps(body)
File "/.local/bin/elasticapi/lib/python2.7/site-packages/elasticsearch/serializer.py", line 54, in dumps
raise SerializationError(data, e)
SerializationError: (<Response 43 bytes [200 OK]>, TypeError("Unable to serialize <Response 43 bytes [200 OK]> (type: <class 'flask.wrappers.Response'>)",))

Code I am using:

from flask import Flask,render_template,request
from elasticsearch import Elasticsearch

elasticapp = Flask(name)
es = Elasticsearch()

def index():

q = request.args.get("q")

if q is not None:
    resp=es.search(index=testindex, body= {'query':{'match':{'Name': q}}})
    return render_template("index.html",q=q,response=resp)

return render_template('index.html')

if name == "main":
elasticapp.run(host= '<Private_IP>',debug=True,port=8000)

For the above code If I pass the value of 'q' i can fetch Index details.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.