上篇我们学习了如何从PDF文件中提取文本进行索引,今天我们来学习如何对一个文件夹下所有文本文件进行索引。废话不多说,我直接贴相关配置:

首先依然是在solrconfig.xml配置文件中配置dataimport请求处理器,并指定data-config.xml配置文件加载路径:

Xml代码  
  1. <requestHandler name="/dataimport" class="solr.DataImportHandler">
  2. <lst name="defaults">
  3. <str name="config">data-config.xml</str>
  4. </lst>
  5. </requestHandler>

指定依赖的jar包加载路径:

Xml代码  
  1. <dataDir>C:\solr_home\core1\data</dataDir>
  2. <lib dir="./lib" regex=".*\.jar"/>

依赖的jar包如图:

 然后重点是配置我们的data-config.xml了,配置内容如下:

Xml代码  
  1. <dataConfig>
  2. <dataSource name="fileDataSource" type="FileDataSource" />
  3. <!--
  4. <document>
  5. <entity name="tika-test" processor="TikaEntityProcessor"
  6. url="C:/docs/solr-word.pdf" format="text">
  7. <field column="Author" name="author" meta="true"/>
  8. <field column="title" name="title" meta="true"/>
  9. <field column="text" name="text"/>
  10. </entity>
  11. </document>
  12. -->
  13. <dataSource name="urlDataSource" type="BinURLDataSource" />
  14. <document>
  15. <entity name="files" dataSource="null" rootEntity="false"
  16. processor="FileListEntityProcessor"
  17. baseDir="c:/docs" fileName=".*\.(doc)|(pdf)|(docx)|(txt)"
  18. onError="skip"
  19. recursive="true">
  20. <field column="fileAbsolutePath" name="filePath" />
  21. <field column="fileSize" name="size" />
  22. <field column="fileLastModified" name="lastModified" />
  23. <entity processor="PlainTextEntityProcessor" name="txtfile" url="${files.fileAbsolutePath}" dataSource="fileDataSource">
  24. <field column="plainText" name="text"/>
  25. </entity>
  26. </entity>
  27. </document>
  28. </dataConfig>

baseDir表示获取这个文件夹下的文件,fileName支持使用正则表达式来过滤一些baseDir文件夹下你不想被索引的文件,processor是用来生成Entity的处理器,而不同Entity默认会生成不同的Field域。FileListEntityProcessor处理器会根据指定的文件夹生成多个Entity,且生成的Entity会包含fileAbsolutePathfileSizefileLastModifiedfileName这几个域,recursive表示是否递归查找子目录下的文件,onError表示当出现异常时是否跳过这个条件不处理。

然后我们需要在schema.xml中定义域,

Xml代码  
  1. <field name="userName" type="string" indexed="true" stored="true" omitNorms="true"/>
  2. <field name="sex" type="boolean" indexed="true" stored="true" omitNorms="true"/>
  3. <field name="birth" type="cndate" indexed="true" stored="true" omitNorms="true"/>
  4. <field name="salary" type="int" indexed="true" stored="true" omitNorms="true"/>
  5. <field name="text" type="text_ik" indexed="true" stored="true" omitNorms="true" multiValued="false"/>
  6. <field name="author" type="string" indexed="true" stored="true" />
  7. <field name="title" type="string" indexed="true" stored="true" />
  8. <field name="fileName" type="string" indexed="true" stored="true" />
  9. <field name="filePath" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  10. <field name="size" type="long" indexed="true" stored="true" />
  11. <field name="lastModified" type="cndate" indexed="true" stored="true" />
  12. <!-- Only remove the "id" field if you have a very good reason to. While not strictly
  13. required, it is highly recommended. A <uniqueKey> is present in almost all Solr
  14. installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".
  15. Do NOT change the type and apply index-time analysis to the <uniqueKey> as it will likely
  16. make routing in SolrCloud and document replacement in general fail. Limited _query_ time
  17. analysis is possible as long as the indexing process is guaranteed to index the term
  18. in a compatible way. Any analysis applied to the <uniqueKey> should _not_ produce multiple
  19. tokens
  20. -->
  21. <field name="id" type="string" indexed="true" stored="true" required="false" multiValued="false" />

到此,配置工作就完毕了,在C:/docs目录下准备几个txt文件用于测试,注意,txt文件编码请保证是UTF-8编码,默认txt文件的编码是GBK,这是很多小白容易犯的错误,特此提醒!!!!!!

 然后重启你的tomcat,执行索引导入,如图:

 

 照例,切换到Query菜单进行查询测试,如图:

 OK,大功告成!本篇博客示例配置文件以及测试用的txt文件我待会儿会上传到底下附件(由于jar包体积太大,附件里不会包含jar包,包含完整jar的,我会上传到我的百度网盘)。

百度网盘下载地址:solr_home.rar

如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,

或者加裙
一起交流学习!

转载:http://iamyida.iteye.com/blog/2214905

跟益达学Solr5之索引文件夹下所有文件相关推荐

  1. 跟益达学Solr5之索引网络上远程文件

    我们的文件有时候不在本地,可能是在网络上,这时候我们该如何对其进行索引呢?当然你可以先通过爬虫把它抓取下来保存到本地硬盘,然后通过上篇博客介绍的方式对其进行索引,其实Solr内置了URLDataSou ...

  2. 跟益达学Solr5之从MySQL数据库导入数据并索引

    最近有小伙伴跟我抱怨说:益达,最近博客更新的有点慢呐.其实不是我变懒了,我是不想因为数量而降低了博客的质量,我需要抱着对你们负责的态度来写每一篇博客,绝不能含糊啊,所以,还望大家多多包涵呐. 今天群里 ...

  3. 跟益达学Solr5之批量索引JSON数据

    假定你有这样一堆JSON数据, Json代码   [ {"id":"1", "name":"Red Lobster",  ...

  4. 跟益达学Solr5之使用Tomcat部署Solr

    最近忙着面试以及生活琐事把时间都霸占了,博客拖了4天没更新了,让各位久等了,望多多包涵!不过还好,工作已经敲定了,终于可以安心的学习Solr并分享我学习的点点滴滴啦! 上回我们在Jetty下部署了,不 ...

  5. python怎么读文件夹下的文件夹-python2.7读取文件夹下所有文件名称及内容的方法...

    最近稍稍有点空闲时间,于是重新温习了一下之前学习过的python基础.废话不多说,记录一下自己的所得. 首先,安装什么的不在本人的温习范围,另,本人使用的是windows下的python2.7版本,且 ...

  6. bat递归查找指定文件_批处理脚本遍历指定文件夹下的文件

    批处理脚本 1. 遍历指定文件夹下的文件 1.1 命令解释 命令: for [参数] %%变量名 in (匹配符) do (执行的命令) 切记:每个指令之间必须以空格隔开,in 与 ( 之间有空格,d ...

  7. python读取文件夹下所有文件的名称_python2.7 学习之读取文件夹下所有文件名称及内容...

    python2.7 学习之读取文件夹下所有文件名称及内容 最近稍稍有点空闲时间, 于是重新温习了一下之前学习过的 python 基础废话不多说, 记录一下自己的所得 首先, 安装什么的不在本人的温习范 ...

  8. Windows 下 C++ 利用 OpenCV glob 函数获取文件夹下所有文件绝对路径

    绪论 本文记录 Windows 下 C++ 利用 OpenCv glob 函数得到 文件夹下所有文件的绝对路径(含文件名).本文还含有 std::string::find()等函数的记录.如果是 Py ...

  9. Linux C 读取文件夹下所有文件(包括子文件夹)

    Linux C 读取文件夹下所有文件(包括子文件夹) 递归读取某文件夹及其子文件夹下所有文件名 深入探讨:linux中遍历文件夹下的所有文件 Linux C :遍历输出指定目录下的所有文件 Linux ...

最新文章

  1. linux ubuntu 虚拟机中配置samba的实现文件共享的方法
  2. 在Oracle中实现自增加ID的功能
  3. java文件异步上传_[Java教程]原生javascript实现文件异步上传
  4. 【渝粤题库】陕西师范大学500001 理论力学 作业(专升本)
  5. 【数据库】SQL查询强化篇
  6. 计算机是如何进行时间同步的
  7. B程序员:讲述三年计算机学习辛酸史
  8. 死锁避免算法--银行家算法
  9. 一文看尽Facebook 3D视觉技术研究进展
  10. CUDA核函数share memory
  11. CATIA怎么约束快捷键_在catia软件上怎么设置快捷键_catia快捷键设置使用要注意哪些问题 - 驱动管家...
  12. android手机安装win10,安卓手机成功安装运行桌面版win10系统
  13. 【GA MTSP】基于matlab遗传算法求解多旅行商问题(同起点不同终点)【含Matlab源码 1325期】
  14. 手机安装 卸载CA证书
  15. 两个路由器无线连接教程
  16. antv g2字体阴影_antv-g2学习手册-中
  17. LintCode_新手必编程50题(1-3阶段)解答与分析
  18. 把用户证书安装成系统证书
  19. c语音大小写字母转换
  20. 与() 、或(|)、异或(^) 、取反(~) 运算

热门文章

  1. 总结一下Qt内存泄漏检测与处理策略
  2. MSSQL On Linux AlwaysOn
  3. erp和mysql_ERP是什么?跟数据库有什么关系?
  4. jQuery fadeIn() 、fadeOut()
  5. 【白兔兔】用TiKZ画2017高考全国3卷理科数学流程图
  6. HTTP和HTTPS的区别以及含义
  7. mysql创建学生信息表学号_mysql创建学生信息表
  8. 最新绝地求生服务器中文版,绝地求生正式版本服务器翻译最新 | 手游网游页游攻略大全...
  9. 日本小学生入校时校长说的话
  10. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.my.mapper.UserM