跟益达学Solr5之索引文件夹下所有文件

简介:

   上篇我们学习了如何从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.                    
  24.                 <entity processor="PlainTextEntityProcessor" name="txtfile" url="${files.fileAbsolutePath}" dataSource="fileDataSource">  
  25.                     <field column="plainText" name="text"/>  
  26.                 </entity>  
  27.         </entity>  
  28.     </document>   
  29. </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.   
  6.    <field name="text" type="text_ik" indexed="true" stored="true" omitNorms="true" multiValued="false"/>  
  7.    <field name="author" type="string" indexed="true" stored="true" />  
  8.    <field name="title" type="string" indexed="true" stored="true" />  
  9.   
  10.    <field name="fileName" type="string" indexed="true" stored="true" />  
  11.    <field name="filePath" type="string" indexed="true" stored="true" required="true" multiValued="false" />  
  12.    <field name="size" type="long" indexed="true" stored="true" />  
  13.    <field name="lastModified" type="cndate" indexed="true" stored="true" />  
  14.    <!-- Only remove the "id" field if you have a very good reason to. While not strictly  
  15.      required, it is highly recommended. A <uniqueKey> is present in almost all Solr   
  16.      installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".  
  17.      Do NOT change the type and apply index-time analysis to the <uniqueKey> as it will likely   
  18.      make routing in SolrCloud and document replacement in general fail. Limited _query_ time  
  19.      analysis is possible as long as the indexing process is guaranteed to index the term  
  20.      in a compatible way. Any analysis applied to the <uniqueKey> should _not_ produce multiple  
  21.      tokens  
  22.    -->     
  23.    <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

目录
相关文章
minio查询文件夹下面内容只返回了当前文件夹
minio查询文件夹下面内容的时候,路径最后一定要带个/,不然就是查询的当前文件夹自己
|
13天前
|
索引
filebeat如何索引csv文件
Filebeat通常用于收集、解析和发送日志和事件数据,但是如果你想索引CSV文件,你可能需要额外的配置和插件。在Elastic Stack中,Logstash更适合处理CSV文件的解析和索引。以下是一个基本的步骤: 1. **安装Filebeat和Logstash**:首先确保你已经安装了Filebeat和Logstash,并且它们正在运行。 2. **配置Filebeat**:在Filebeat配置文件中添加一个新的输入,指向你的CSV文件。例如: ```yaml filebeat.inputs: - type: log paths: - /
|
4月前
|
分布式计算 小程序 Java
solr(四)solr目录结构
solr start –p 端口号 #单机版启动solr服务 solr restart –p 端口号 #重启solr服务 solr stop –p 端口号 #关闭solr服务
43 0
|
5月前
|
缓存 Java 索引
idea 使用神坑之索引篇(自定义不创建索引文件夹)
idea 使用神坑之索引篇(自定义不创建索引文件夹)
114 0
|
6月前
|
索引
18Solr管理索引库
18Solr管理索引库
14 0
|
6月前
|
索引
07Lucene索引库的修改
07Lucene索引库的修改
15 0
|
10月前
|
Java
查询文件路径
查询文件路径
52 0
|
11月前
|
JSON 数据格式
批量遍历指定类型的文件及文件夹,并将其复制到新的文件夹
批量遍历指定类型的文件及文件夹,并将其复制到新的文件夹
84 0
|
Java 索引