solr4.7.2 索引远程文件内容

来源:互联网 发布:电脑网络枪战游戏大全 编辑:程序博客网 时间:2024/05/01 11:10

我的项目需求如下,一张数据库表是存储我们上传的文档附件的,其中有一个字段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配置就不说了,挺简单的。
 
 <dataConfig>   <!--从本地索引的,可以使用<dataSource name="fileDataSource" type="FileDataSource" /><dataSource name="urlDataSource" type="BinURLDataSource" />    <document>            <entity name="files" dataSource="null" rootEntity="false"            processor="FileListEntityProcessor"            baseDir="c:/docs" fileName=".*\.(doc)|(pdf)|(docx)|(txt)"            onError="skip"            recursive="true">                <field column="fileAbsolutePath" name="fileurl" />                <entity processor="PlainTextEntityProcessor" name="txtfile" url="${files.fileAbsolutePath}" dataSource="fileDataSource">                    <field column="plainText" name="text"/>                </entity>        </entity>    </document>--><!--索引网络文章,可以使用<dataSource name="urlDataSource" type="URLDataSource" />    <document>        <entity processor="PlainTextEntityProcessor" name="onlineTxtFile" url="http://demoumc.yonyou.com/ump/QueryPatchDetailedServlet?PK=0001AA1000000002UW21" dataSource="urlDataSource">            <field column="plainText" name="text"/>        </entity>    </document>--><!--从本地索引pdf文件,可以使用<dataSource name="binFileDataSource" type="BinFileDataSource" /><document>        <entity name="tika-test" processor="TikaEntityProcessor"                url="C:/docs/solr-word.pdf" format="text">                <field column="Author" name="filename" meta="true"/>                <field column="title" name="fileurl" meta="true"/>                <field column="text" name="text"/>        </entity>    </document>--><!--使用URLDataSource可以网络上的页面文章等索引下来,但是对于我们的应用中url是下载的连接,这样索引下来的是流,而不是内容--><!--<dataSource name="urlDataSource" type="URLDataSource"/>--><!--BinFileDataSource需要指定的是文件的路径,但是我们的应用中给的是下载的连接,这两者不太一样,使用这个数据源的时候会报file not found--><!--<dataSource name="binFileDataSource" type="BinFileDataSource" />--><!--符合我们项目需求的dih导入附件内容进行索引,可以使用--><dataSource name="binURLDataSource" type="BinURLDataSource" /><dataSource name="oracle" type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@20.10.130.160:1521:orcl" user="caiwu63" password="1"/>    <document name="filedocument">          <entity pk="FILEURL" dataSource="oracle" name="files"     query="select uft.filename as filename,uft.fileurl as FILEURL from ump_fastdfs_test uft "> <field column="filename"   name="filename" />  <field column="FILEURL"   name="fileurl" /><!--这里子entity引用父entity的变量fileurl,这个变量可以不是父entity的外键,因为父子entity不是做的left join查询--><entity processor="TikaEntityProcessor" name="onlineTxtFile" url="${files.FILEURL}" dataSource="binURLDataSource" format="text">                <field column="Author" name="author" meta="true"/>                <field column="title"  name="title" meta="true"/><field column="text"  name="text"/>            </entity><!--这里子entity引用父entity的变量fileurl,这个变量必须是父entity的外键,因为父子entity做的是left join查询--><!--<entity name="comment"         query="select uft.fileurl as fileurl2 from ump_fastdfs_test uft where uft.fileurl = '${files.FILEURL}' "><field column="fileurl2"    name="fileurl2" /></entity>-->        </entity>    </document></dataConfig>  


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

0 0
原创粉丝点击