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
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;
}
}
测试结果:
索引后的全文内容:"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

solr直接对pdf、word等建索引相关推荐

  1. 第六周 Word目录和索引

    第六周 Word目录和索引 教学时间 2013-4-2 教学课时 2 教案序号 5 教学目标 能正确使用索引.目录等 教学过程: 复习提问 1.脚注和尾注的区别是什么? 2.如何插入脚注和尾注? 3. ...

  2. solr java 全量,Solr实时创建增量或全量索引

    1,为了支持增量建索引,我们需要把上述文中的mysql-data-config.xml内容改为 query="SELECT id, title, content FROM article&q ...

  3. mysql在线上建索引,mysql 5.6在线DDL建索引测试

    基本信息: mysql版本: (product)root@localhost [(none)]> select @@version; +------------+ | @@version  | ...

  4. 不该建索引及不走索引的原因

    1.5 不应该建索引列的特点: 第一,对于那些在查询中很少使用或者参考的列不应该创建索引.这是因为,既然这些列很少使用到,因此有索引或者无索引,并不能提高查询速度.相反,由于增加了索引,反而降低了系统 ...

  5. sqlserver怎么查看索引_Sql Server之旅——第六站 为什么都说状态少的字段不能建索引...

    我们在学sqlserver的时候,大多教科书和前辈们都说状态少的字段不要建索引,由此带来的开销还不如不建索引,但是这句话有多少人真的知道,或者说有多少人真的对此有比较深刻的理解,而不是听别人道听途说. ...

  6. 什么情况下应不建或少建索引

    1.表记录太少 2.经常插入.删除.修改的表 3.数据重复且分布平均的表字段,假如一个表有10万行记录,有一个字段A只有T和F两种值,且每个值的分布概率大约为50%,那么对这种表A字段建索引一般不会提 ...

  7. 为什么重复值高的字段不能建索引(比如性别字段等)

    结论(以innodb为例) a.非聚簇索引存储了对主键的引用,如果select字段不在非聚簇索引内,就需要跳到主键索引(上图中从右边的索引树跳到左边的索引树),再获取select字段值 b.如果非聚簇 ...

  8. oracle建索引默认并发,ORACLE重建索引需要考虑问题

    一:考虑重建索引的场合 1:表上频繁发生update,delete操作 2:表上发生了alter table ..move操作(move操作导致了rowid变化) 二:判断重建索引的标准 索引重建是否 ...

  9. Mongodb在renameCollection之后,记得千万要立即补建索引

    mongodb执行完renameCollection之后,记得千万要立即建索引. 因为mongodb的新insert语句只负责create collections,不负责create index, 如 ...

最新文章

  1. Windows Server 2008 without Hyper-V 版本
  2. 如何让语音助手通过图灵测试
  3. C#写的WebServices可运行于树莓派
  4. python腐蚀膨胀代码_OpenCV+python实现膨胀和腐蚀的示例
  5. [转]双线性插值(Bilinear interpolation)
  6. 转载:面向站长和网站管理员的Web缓存加速指南
  7. 关于button onclick a href 分析
  8. NetScaler的cookieinsert和sourceip联合保持机制
  9. json数据交互——@RequestBody与@ResponseBody
  10. 黑苹果macOS Catalina 10.15.4 安装过程详细记录
  11. MSN聊天记录显示方式
  12. 及c语言实现 pdf,词法分析及其C语言实现.PDF
  13. 蚂蚁链API参考接口
  14. wps文字 用WPS如何演示制作英语有声读物
  15. 协通xt800 anydesk teamview 密码获取
  16. FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-PU
  17. vr虚拟现实技术前景怎么样?虚拟展示技术应用介绍
  18. c语言中余数取整,C 逻辑运算, 移位运算 , 取整 , 取模(取余)
  19. 个人笔记:数据库——数据库如何进行备份?
  20. jupyter的环境问题:在jupyter中添加我们想要的环境

热门文章

  1. 一些国外的免费网站模板网站
  2. 三维几何基础(3D?)
  3. 大学十年(一个程序员的路程
  4. Ubuntu之输入法
  5. 微信支付商户与服务商区别
  6. 在日本生活是什么体验
  7. PC817TL431的配合电路问题
  8. 京东程序员压力太大在网页植入骂人代码?官方辟谣!
  9. Glyph 26426 missing from current font. matplotlib显示中文乱码解决
  10. NOKIA手机共有的缺陷