skip to Main Content

I have a REST API built using Flask. It has GET and POST methods. The API returns a JSON response. I want SOLR to use the REST API’s URL to perform a search on this response based on the query and return relevant search results.

How can I achieve this? Does SOLR only take a JSON file as input, in which case I would need to write endpoint’s response to a JSON file, place it in the example folder and pass it into SOLR?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Amit! In the link you shared, it had curl commands. But I was trying to do this with Python. I found out a solution for this! Works like a charm. I have also embedded comments for further explanation.

    @app.route('/solrInput', methods = ['POST'])
    def solr_input():
        """Get response data from MongoDB RESTAPI and feed into SOLR for indexing"""
    
        #get mongoDB data from rest api
        input_data = requests.get('http://127.0.0.1:5000/getAllRecords')
        json_data = input_data.json()
    
        #loop through response to fetch the values
        for key, val in json_data.items():
            payload = str(val)
            response = requests.post("http://localhost:8983/solr/techs/update/json/docs?commit=true", data=payload)
    
    if __name__ == "__main__":
        app.run(debug=True)
    

  2. I want SOLR to use the REST API’s URL to perform search on the data(JSON response) based on the query and return relevant search results

    You will have to invoke the REST API, get back the JSON response and add it to Solr Index. Then you can use Solr to search that data.

    Could you please let me know how to achieve this? Is it possible?

    Please take a look at this documentation. It will help you indexing JSON documents.
    https://lucene.apache.org/solr/guide/7_1/uploading-data-with-index-handlers.html#solr-style-json

    Or does SOLR only take JSON file as input placed in the example folder, in which case, I would need to write the response received from my RESTAPI to a json file, place it in the example folder and pass it into SOLR

    This is partially correct understanding, however you do not have to place it in example folder. You need to post that JSON (or format JSON in the form required by Solr) as shown in the reference provided above.

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search