solr直接对pdf、word等建索引

1.准备solr6.6环境,这里我搭建了3台solr服务的环境
2.配置managed-schema,添加几个测试字段
  <field name="literal.path" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="literal.pathuploaddate" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="literal.pathftype" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="fmap.content" type="string" multiValued="false" indexed="true" stored="true"/>
 
3.添加依赖包
在/opt/solr-6.6.2/server/solr-webapp/webapp/WEB-INF/lib下上传如下两个包:
mmseg4j-core-1.10.0.jar
mmseg4j-solr-2.3.1.jar
 
  79eaf844b26c7ea02ed62693ce0a08a9dc8.jpg
4.测试接口如下:
package solrpdf;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
 
public class SolrPdf {
  public static void main(String[] args)
     {
         String fileName = "E://2019-08-09//bj_fzc_hd_fzk_qh__liyanpo__5796104_Doc5501.pdf";
         String solrId = "bj_fzc_hd_fzk_qh__liyanpo__5796104_Doc5501.pdf";
// String path = "";
         try
         {
// indexFilesSolrCell(solrId, solrId,fileName);
    indexFilesSolrCell(fileName, solrId, fileName);
         }
         catch (IOException e)
         {
             e.printStackTrace();
         }
         catch (SolrServerException e)
         {
             e.printStackTrace();
         }
         }
         private static String GetCurrentDate(){
             Date dt = new Date();
             //最后的aa表示“上午”或“下午” HH表示24小时制 如果换成hh表示12小时制
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             String day =sdf.format(dt);
             return day;
         }
         public static void indexFilesSolrCell(String fileName, String solrId, String path)
                 throws IOException, SolrServerException
         {
             String urlString = "http://192.168.0.231:8983/solr/TESTPDF";
             SolrClient solr = new HttpSolrClient.Builder(urlString).build();
             ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
             String contentType = getFileContentType(fileName);
             up.addFile(new File(path), contentType);
             String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
             up.setParam("literal.id", fileName);
             up.setParam("literal.path", path);//文件路径
             up.setParam("literal.pathuploaddate", GetCurrentDate());//文件上传时间
             up.setParam("literal.pathftype", fileType);//文件类型,doc,pdf
             up.setParam("fmap.content", "attr_content");//文件内容
             up.setAction(ACTION.COMMIT, true, true);
             solr.request(up);
         }
 
         /**
         * @Author:sks
         * @Description:根据文件名获取文件的ContentType类型
         * @Date: 
         */
         public static String getFileContentType(String filename) {
             String contentType = "";
             String prefix = filename.substring(filename.lastIndexOf(".") + 1);
             if (prefix.equals("xlsx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
             } else if (prefix.equals("pdf")) {
                 contentType = "application/pdf";
             } else if (prefix.equals("doc")) {
                 contentType = "application/msword";
             } else if (prefix.equals("txt")) {
                 contentType = "text/plain";
             } else if (prefix.equals("xls")) {
                 contentType = "application/vnd.ms-excel";
             } else if (prefix.equals("docx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
             } else if (prefix.equals("ppt")) {
                 contentType = "application/vnd.ms-powerpoint";
             } else if (prefix.equals("pptx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
             }
             else {
                 contentType = "othertype";
             }
             return contentType;
         }
}
测试结果:
ffbac48d9f35bff9d66196600ada3850cf1.jpg
索引后的全文内容:"attr_content":" \n \n date 2018-12-26T01:02:31Z  \n pdf:PDFVersion 1.4  \n stream_content_type application/pdf  \n access_permission:modify_annotations true  \n access_permission:can_print_degraded true  \n dcterms:created 2018-12-26T01:02:31Z  \n Last-Modified 2018-12-26T01:02:31Z  \n dcterms:modified 2018-12-26T01:02:31Z  \n dc:format application/pdf; version=1.4  \n Last-Save-Date 2018-12-26T01:02:31Z  \n access_permission:fill_in_form true  \n pdf:docinfo:modified 2018-12-26T01:02:31Z  \n meta:save-date 2018-12-26T01:02:31Z  \n pdf:encrypted false  \n modified 2018-12-26T01:02:31Z  \n Content-Type application/pdf  \n stream_size 4354  \n X-Parsed-By org.apache.tika.parser.DefaultParser  \n X-Parsed-By org.apache.tika.parser.pdf.PDFParser  \n meta:creation-date 2018-12-26T01:02:31Z  \n created Wed Dec 26 09:02:31 GMT+08:00 2018  \n access_permission:extract_for_accessibility true  \n access_permission:assemble_document true  \n xmpTPg:NPages 2  \n Creation-Date 2018-12-26T01:02:31Z  \n access_permission:extract_content true  \n access_permission:can_print true  \n producer iText1.3.1 by lowagie.com (based on itext-paulo-154)  \n access_permission:can_modify true  \n pdf:docinfo:producer iText1.3.1 by lowagie.com (based on itext-paulo-154)  \n pdf:docinfo:created 2018-12-26T01:02:31Z  \n  \n \n  page   \n 北京市工商行政管理局海淀分局\n \n 行 政 处 罚 决 定 书\n京工商海处字〔2018〕第2331号\n \n     当事人:小米科技有限责任公司\n \n     住所:北京市海淀区清河中街68号华润五彩城购物中心二期13层\n \n     统一社会信用代码:91110108551385082Q\n \n     法定代表人:雷军\n \n     经查:米家有品app为当事人所有,当事人于2017年12月19日至12\n \n 月26日在米家有品自营店铺所进行的“有品感恩季活动”中,对其\n \n 销售的小米MIX2手机宣传:“领券减100 128GB版送小米AI音箱”,\n \n 但消费者购买小米MIX2 128GB版手机领券后没有减100元,也没有送\n \n 小米AI音箱。当事人称,消费者购买时如果选择的限时购,就不会\n \n 享受领券减100元及送小米AI音箱,因为限时购与其它优惠政策不叠\n \n 加,但当事人未在产品页面中标明。上述违法事实有当事人的营业\n \n 执照复印件、情况说明、经当事人确认的相关网页截图打印件、举\n \n 报登记单、询问(调查)笔录等材料在案佐证。\n \n       我局于2018年11月27日向当事人送达了《行政处罚听证告知书》\n \n ,告知当事人我局决定作出行政处罚决定的事实、理由、依据、内\n \n 容以及当事人依法享有的陈述权、申辩权。当事人在法定期限内未\n \n 提出陈述、申辩意见,也未提出听证。\n \n      当事人的上述行为,违反了《中华人民共和国广告法》第八条第\n \n 一款,构成广告中对商品的性能、功能、产地、用途、质量、成分\n \n 、价格、生产者、有效期限、允诺等有表示的,未准确、清楚、明\n \n 白表示的违法行为。依据《中华人民共和国广告法》第五十九条第\n \n 一款(一)项的规定,责令当事人停止发布上述违法广告,并决\n \n 第 1 页 \n  \n \n page   \n 定处罚如下:罚款30000元。\n \n     当事人应当自收到本处罚决定书之日起15日内到就近银行(非税收\n \n 入代理收缴银行:北京银行、建设银行、交通银行、光大银行、中\n \n 信银行)缴纳罚款。逾期不缴纳,每日按罚款数额的3%加处罚款。\n \n     如不服本处罚决定,可于接到本处罚决定书之日起60日内向北京市\n \n 工商行政管理局或海淀区人民政府申请复议。也可于6个月内直接向\n \n 海淀区人民法院起诉。\n \n                          北京市工商行政管理局海淀分局\n \n                            二〇一八年十二月二十日\n \n 第 2 页 \n  \n \n  ",
        "_version_":1641378670909063168}]
  }}
参考链接:https://www.cnblogs.com/shaosks/p/8033362.html
 

转载于:https://my.oschina.net/u/3197158/blog/3089076

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值