solr4.7.2 索引远程文件内容

来源:互联网 发布:网络零售交易额指什么 编辑:程序博客网 时间:2024/05/01 12:40

我的项目需求如下,一张数据库表是存储我们上传的文档附件的,其中有一个字段fileurl是该附件下载的连接url,要求通过dih的方式将该连接对应的文档内容索引下来。

首先在网上看了下面的三个博客文章挺有用的。

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

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

http://iamyida.iteye.com/blog/2214600

跟益达学Solr5之使用Tika从PDF中提取数据导入索引

http://iamyida.iteye.com/blog/2214920 

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

虽然我的需求他描述的很接近,但是还是有不一样的地方的,我的url是下载的连接,而不像他在索引远程文件中那样是一个html文件,因此在选择datasource时是需要注意的。

因为在网上这样的资料挺少的,就自己根据知道的一个一个测试,最后成功的索引的文档内容。

在这里总结几点

1.索引远程文档所需要的jar请参考我提供的那三篇博客,是一样的,虽然不太了解其中一些jar包的意思,希望以后可以研究下。

不过你需要根据你的solr版本去找对应的jar包,在下载的solr中都可以找到,如solr-4.7.2\contrib\extraction\lib中就是。

在solr-4.7.2\example中有很多例子可以帮助我们学习,如solr-4.7.2\example\example-DIH\solr就有solr对不同形式的数据进行索引的例子,如网络文件,mail,db等。

2.益达在博客中写到指定依赖的jar包,而不用都将jar包放到tomcat服务器的solr项目的web-inf的lib下。

指定依赖的jar包加载路径:<lib dir="./lib" regex=".*\.jar"/>  

但是我在做的过程中使用这样的方式,虽然在启动solr的时候将这些jar都加在到classloader中了,但是索引文档的时候还是报class not found ,放到solr的服务器目录中,/webapps/solr/WEB-INF/lib/就可以正常的索引文档内容,在网上一直没有找到该问题出现的原因和解决方法,还希望知道原因的人帮忙指点下。

在http://blog.csdn.net/cywosp/article/details/38965981博客中作者也遇到了这样的问题,最后也是将jar放到tomcat的服务器目录中解决的。

3.我使用的是entity嵌套的方式

   3.1子entity也是sql查询的话,如果引用父entity的变量,这个变量必须是父entity的外键,因为父子entity做的是主外键left join的查询

   3.2子entity使用其他的processor的话,引用父entity的值,则这个值不必是父entity的外键。

http://blog.csdn.net/ystyaoshengting/article/details/47445543 博客中写到

  • left join:涉及到多个表之间有外键连接时,solr源码里面给我们展示的,是<entity></entity>里面,去引用上一层<entity></entity>对应表的外键:orgid='${member.mem_orgid}'。那么,我们何不直接将配置文件中的Query改为:select  distinct * from member left join organization o on orgid = mem_orgid。这样就可以不用分层,把所有<field></field>定义到一块了。这样是可行的,只是效率还是个未知数。不过推荐分层的<entity></entity>,逻辑清楚。
最后上传下我自己写的data-config.xml的配置,scheme.xml和solrconfig.xml配置就不说了,挺简单的。
 
[html] view plaincopy
  1.  <dataConfig>     
  2.     <!--从本地索引的,可以使用  
  3.     <dataSource name="fileDataSource" type="FileDataSource" />  
  4.     <dataSource name="urlDataSource" type="BinURLDataSource" />  
  5.     <document>  
  6.             <entity name="files" dataSource="null" rootEntity="false"  
  7.             processor="FileListEntityProcessor"  
  8.             baseDir="c:/docs" fileName=".*\.(doc)|(pdf)|(docx)|(txt)"  
  9.             onError="skip"  
  10.             recursive="true">  
  11.                 <field column="fileAbsolutePath" name="fileurl" />  
  12.                 <entity processor="PlainTextEntityProcessor" name="txtfile" url="${files.fileAbsolutePath}" dataSource="fileDataSource">  
  13.                     <field column="plainText" name="text"/>  
  14.                 </entity>  
  15.         </entity>  
  16.     </document>  
  17.     -->  
  18.     <!--索引网络文章,可以使用  
  19.     <dataSource name="urlDataSource" type="URLDataSource" />  
  20.     <document>  
  21.         <entity processor="PlainTextEntityProcessor" name="onlineTxtFile" url="http://demoumc.yonyou.com/ump/QueryPatchDetailedServlet?PK=0001AA1000000002UW21" dataSource="urlDataSource">  
  22.             <field column="plainText" name="text"/>  
  23.         </entity>  
  24.     </document>  
  25.     -->  
  26.     <!--从本地索引pdf文件,可以使用  
  27.     <dataSource name="binFileDataSource" type="BinFileDataSource" />  
  28.     <document>  
  29.         <entity name="tika-test" processor="TikaEntityProcessor"  
  30.                 url="C:/docs/solr-word.pdf" format="text">  
  31.                 <field column="Author" name="filename" meta="true"/>  
  32.                 <field column="title" name="fileurl" meta="true"/>  
  33.                 <field column="text" name="text"/>  
  34.         </entity>  
  35.     </document>  
  36.     -->  
  37.     <!--使用URLDataSource可以网络上的页面文章等索引下来,但是对于我们的应用中url是下载的连接,这样索引下来的是流,而不是内容-->  
  38.     <!-- 
  39.     <dataSource name="urlDataSource" type="URLDataSource"/> 
  40.     -->  
  41.     <!--BinFileDataSource需要指定的是文件的路径,但是我们的应用中给的是下载的连接,这两者不太一样,使用这个数据源的时候会报file not found-->  
  42.     <!-- 
  43.     <dataSource name="binFileDataSource" type="BinFileDataSource" /> 
  44.     -->  
  45.       
  46.     <!--符合我们项目需求的dih导入附件内容进行索引,可以使用-->  
  47.     <dataSource name="binURLDataSource" type="BinURLDataSource" />  
  48.     <dataSource name="oracle" type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver"   
  49.                 url="jdbc:oracle:thin:@20.10.130.160:1521:orcl" user="caiwu63" password="1"/>  
  50.     <document name="filedocument">  
  51.           <entity pk="FILEURL" dataSource="oracle" name="files"   
  52.             query="select uft.filename as filename,uft.fileurl as FILEURL from ump_fastdfs_test uft ">     
  53.             <field column="filename"   name="filename" />    
  54.             <field column="FILEURL"     name="fileurl" />   
  55.             <!--这里子entity引用父entity的变量fileurl,这个变量可以不是父entity的外键,因为父子entity不是做的left join查询-->  
  56.             <entity processor="TikaEntityProcessor" name="onlineTxtFile" url="${files.FILEURL}" dataSource="binURLDataSource" format="text">  
  57.                 <field column="Author" name="author" meta="true"/>  
  58.                 <field column="title"  name="title" meta="true"/>  
  59.                 <field column="text"  name="text"/>  
  60.             </entity>  
  61.             <!--这里子entity引用父entity的变量fileurl,这个变量必须是父entity的外键,因为父子entity做的是left join查询-->  
  62.             <!--  
  63.             <entity name="comment"   
  64.                     query="select uft.fileurl as fileurl2 from ump_fastdfs_test uft where uft.fileurl = '${files.FILEURL}' ">  
  65.                 <field column="fileurl2"    name="fileurl2" />  
  66.             </entity>  
  67.             -->  
  68.         </entity>  
  69.     </document>  
  70. </dataConfig>    


大家也可以参考http://wiki.apache.org/solr/官方文档对datasource及entity的processor的讲解,很有帮助。
0 0
原创粉丝点击