Not able to post to solr server using python and requests
Asked Answered
B

1

8

This is the code I am trying to implement:-

import requests
import tornado.ioloop
import tornado.web
import tornado.autoreload
import json

class MainHandler(tornado.web.RequestHandler):
        def get(self):
            payload = [{"id" : "978-0641723445", "cat" : ["book","hardcover"], "name" : "The Lightning Thief", "author" : "Rick Riordan","series_t" : "Percy Jackson  Olympians", "sequence_i" : 1, "genre_s" : "fantasy", "inStock" : True, "price" : 12.50, "pages_i" : 384}]
            url = 'http://localhost:8983/solr/update/json'
            headers = {'content-type' : 'application/json'}
            # files = {'file': ('books.json', open('books.json', 'rb'))}
            timeline = requests.post(url, data = json.dumps(payload), headers = headers)
            self.write(timeline.text)
class QueryHandler(tornado.web.RequestHandler):
        def get(self):
            # timeline = requests.get('http://localhost:8983/solr/collection1/select?q=a&wt=json&indent=true')
            payload = {'q' : 'a', 'wt' : 'json', 'indent' : True}
            timeline = requests.get('http://localhost:8983/solr/collection1/select', params = payload)
            self.write(timeline.json())
application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/query", QueryHandler)
])

if __name__ == "__main__":
    application.listen(8888)
    io_loop = tornado.ioloop.IOLoop.instance()
    tornado.autoreload.start(io_loop)
    io_loop.start()

I am able to query the solr server on localhost:8888/query but on localhost:8888 where I am trying to post the data, I get this response from solr:-

{
responseHeader: {
status: 0,
QTime: 46
}
}

Data is not getting posted to the solr server.

Any suggestions ??

Bravo answered 25/9, 2013 at 12:24 Comment(2)
Did you ever solve this? Facing similar problem.Mediaeval
I did manage to solve it. But I can't seem to remember and I also dont have the code to post. I'll add as an answer if I can find the codeBravo
P
4

The code doesn't contain commitWithin info in the header. The parameter is in milliseconds. Its only after a commit the data is available for search from Solr. The following may serve as an example to POST data to solr. Add the JSON header along with the commitWithin time and the data as a JSON string to the data param

requests.post("http://localhost:8983/solr/collection1/update?wt=json", headers={"Content-Type":"application/json"}, data='{"add":{ "doc":{"id" : 14, "log_type" : "debug", "log_text" : "A transaction of debug from Kimy"},"boost":1.0,"overwrite":true, "commitWithin": 1000}}')

Response :

{"responseHeader":{"status":0,"QTime":128}}

Peirce answered 23/6, 2015 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.