Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to post to solr server using python and requests

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 ??

like image 934
Divyanshu Das Avatar asked Sep 25 '13 12:09

Divyanshu Das


People also ask

How does Python connect to Solr?

Simple PythonFirst, tell Python you will need to make HTTP connections. Now open a connection to the server and get a response. The wt query parameter tells Solr to return results in a format that Python can understand. Now interpreting the response is just a matter of pulling out the information that you need.

How upload data to Solr?

Importing the DataGo to browser and open http://localhost:8983/solr to access Solr admin. Choose your Core as shown below. You should now see a new menu. Choose Data Import from the menu and you should see a view as shown below.

How many requests can Solr handle?

Depending on a multitude of factors, a single machine can easily host a Lucene/Solr index of 5 – 80+ million documents, while a distributed solution can provide subsecond search response times across billions of documents.


1 Answers

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}}

like image 178
xtreak Avatar answered Nov 15 '22 08:11

xtreak