如何使用webservice的内容索引solr服务器。
我的webservice输出如下所示
现在我想使用xml下的内容索引solr服务器,如上所示
如何将这个索引编入apache solr。
答案 0 :(得分:1)
您需要松散地按照以下步骤索引数据。
答案 1 :(得分:1)
使用您喜欢的脚本语言编写脚本(Python代表我)。我做了类似数据库的事情,希望类似的解决方案能顺利完成。
使用Python:
并像cron-job一样定期运行此脚本。
您将需要两段代码:一条用于查询您的RESTful服务并获取响应的主体;另一个将格式化文档上传到Solr。
这段代码将Python对象 request_obj 上传到给定的 request_url ,并且solr的响应作为Python对象返回。本机Python对象(由字典(关联数组),列表,字符串,数字组成)可轻松转换为JSON(具有1-2个警告)。
仅用作参考。我保证不适合你的目的。
不要忘记使用从Solr 3.3开始提供的 / update / json?wt = python 。您需要MultipartPostHandler库。
def solr_interface(self,request_url,request_obj):
request=json.dumps(request_obj,indent=4,encoding="cp1252")
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
req = urllib2.Request(request_url, request)
req.add_header("Content-Type", "application/json")
text_response = urllib2.urlopen(req).read().strip()
return ast.literal_eval(text_response)
至于在Python中解析(和编写)XML,请使用这些优秀的教程http://www.learningpython.com/2008/05/07/elegant-xml-parsing-using-the-elementtree-module/和http://effbot.org/zone/element.htm
这是一个命令行示例。
from xml.etree import ElementTree as ET
elem =ET.fromstring("<doc><p>This is a block</p><p>This is another block</p></doc>")
for subelement in elem:
... print subelement.text
...
This is a block
This is another block