python-2.7 – 无法使用python和请求发布到solr服务器
发布时间:2020-12-20 13:37:24 所属栏目:Python 来源:网络整理
导读:这是我试图实现的代码: – import requestsimport tornado.ioloopimport tornado.webimport tornado.autoreloadimport jsonclass MainHandler(tornado.web.RequestHandler): def get(self): payload = [{"id" : "978-0641723445","cat" : ["book","hardcove
这是我试图实现的代码: –
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() 我能够在localhost:8888 / query上查询solr服务器 { responseHeader: { status: 0,QTime: 46 } } 数据未发布到solr服务器. 有什么建议 ?? 解决方法
代码在标头中不包含commitWithin信息.参数以毫秒为单位.只有在提交后,才能从Solr搜索数据.以下可以作为将数据POST到solr的示例.将JSON标头与commitWithin时间以及数据作为JSON字符串添加到数据参数
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“:”来自Kimy的调试事务“},”boost“:1.0,”overwrite“:true,”commitWithin“:1000} }“) 回应: { “responseHeader”:{ “状态”:0 “QTIME”:128}} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |