Python实现向solrclould提交pdf文件

根据官方文档,可以通过culr向solr提交文件,https://wiki.apache.org/solr/ExtractingRequestHandler
具体原理请阅读官方wiki
那么如何通过Python来实现呢?
1 在solrconfig.xml里配置ExtractingRequestHandler

<requestHandler name="/update/extract" class="solr.handler.extraction.ExtractingRequestHandler" startup="lazy">
    <lst name="defaults">
      <str name="fmap.content">text</str>  #这个就是tika解析pdf文件后获得内容对应的字段
      <str name="lowernames">true</str>
      <str name="uprefix">ignored_</str> # 很重要,配合schema.xml里设置的字段
     </lst>
  </requestHandler>

2 在schema.xml里配置字段

   <fields>
   <field name="_version_" type="long" indexed="true" stored="true"/>  
   <field name="subject" type="text" indexed="true" stored="true"/>

   <field name="text" type="text" indexed="true" stored="false" multiValued="true"/> 
   <field name="id" type="string" indexed="true" stored="true" required="true" /> #主键
   <dynamicField name="ignored_*" type="ignored"/> #对应上面1中的ignored配置
</fields>

3.官方wiki里用culr来提交文件如:
curl “http://localhost:8983/solr/update/extract?literal.id=doc2&commit=true” -F “tutorial=@tutorial.pdf”
那么Python怎么实现呢?
这里用Python的第三方库pycurl来实现,pycurl下载请移步 https://pypi.python.org/pypi/pycurl
上代码

    import pycurl
    import cStringIO
    url = "localhost/solr/qa_file/update/extract?literal.id=filename&commit=true"
    cur = pycurl.Curl()
    fp = cStringIO.StringIO()
    cur.setopt(pycurl.WRITEFUNCTION, fp.write)
    cur.setopt(pycurl.FOLLOWLOCATION, 1)
    cur.setopt(pycurl.MAXREDIRS, 5)
    cur.setopt(pycurl.CONNECTTIMEOUT, 60)
    cur.setopt(pycurl.TIMEOUT, 300)
    cur.setopt(cur.POST, 1)
    cur.setopt(cur.URL, url)
    # cur.setopt(cur.POSTFIELDS, urllib.urlencode(post_data_dic))
    cur.setopt(cur.HTTPPOST, [("file", (cur.FORM_FILE, r"E:\1Python\test.pdf"))])
    cur.perform()
    status = cur.getinfo(cur.HTTP_CODE)
    bbody = fp.getvalue()
    print status, "\n", bbody
    cur.close()

literal.id=filename就是对应刚才配置字段里的id,也就是主键
这样就能在solr里通过pdf的内容检索到pdf了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值