分享

Solr4.7从文件创建索引

问题导读

1.solr4.7中如何从pdf文件创建索引?
2.如何配置文件索引库





索引数据源并不会一定来自于数据库、XML、JSON、CSV这类结构化数据,很多时候也来自于PDF、word、html、word、MP3等这类非结构化数据,从这类非结构化数据创建索引,solr也给我们提供了很好的支持,利用的是apache  tika。

下面我们来看看在solr4.7中如何从pdf文件创建索引。



一、配置文件索引库

1、  新建core

我们新建一个solr的core,用于存储文件型索引,新建core的步骤请参考:

http://blog.csdn.net/clj198606061111/article/details/21288499

2、  准备jar

我们在$solr_home下面新建一个extract文件夹,用于存放solr扩展jar包。

从colr4.7发布包中solr-4.7.0\dist拷贝solr-cell-4.7.0.jar到新建的extract文件夹下。拷贝solr4.7发布包solr-4.7.0\contrib\extraction\lib下所有jar包到extract文件夹下。

3、  配置solrconfig.xml

添加请求解析配置:

  1. <requestHandler name="/update/extract" class="solr.extraction.ExtractingRequestHandler" >
  2.    <lst name="defaults">
  3.     <str name="fmap.content">text</str>
  4.     <str name="lowernames">true</str>
  5.     <str name="uprefix">attr_</str>
  6.     <str name="captureAttr">true</str>
  7.    </lst>
  8.   </requestHandler>
复制代码

指定依赖包位置:

注意,这个相对位置不是相对于配置文件所在文件夹位置,而是相对core主目录的。比如我的配置文件在solr_home\core1\conf,但是我的jar包在solr_home\ extract那么我的相对路径就是../extract而不是../../extract。


  1. <lib dir="../extract" regex=".*\.jar" />
复制代码


4、配置schema.xml

4.1配置索引字段的类型,也就是field类型。

其中text_general类型我们用到2个txt文件(stopwords.txt、synonyms.txt),这2个txt文件在发布包示例core里面有位置在:solr-4.7.0\example\solr\collection1\conf,复制这2个txt文件到新建的$solr_home的那个新建的core下面的conf目录下,和schema.xml一个位置。


  1. <types>
  2.    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
  3.    <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
  4.    <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  5.       <analyzer type="index">
  6.         <tokenizer class="solr.StandardTokenizerFactory"/>
  7.         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  8.         <filter class="solr.LowerCaseFilterFactory"/>
  9.       </analyzer>
  10.       <analyzer type="query">
  11.         <tokenizer class="solr.StandardTokenizerFactory"/>
  12.         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  13.         <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
  14.         <filter class="solr.LowerCaseFilterFactory"/>
  15.       </analyzer>
  16.     </fieldType>
  17.   </types>
复制代码


4.2配置索引字段,也就是field

其中有个动态类型字段,attr_*,这个是什么意思呢。也就是solr在解析文件的时候,文件本身有很多属性,具体有哪些属性是不确定的,solr全部把他解析出来以attr作为前缀加上文件本身的属性名,组合在一起就成了field的名称

  1. <field name="id"        type="string"       indexed="true"  stored="true"  multiValued="false" required="true"/>
  2.   <field name="text"      type="text_general" indexed="true"  stored="true"/>
  3.   <field name="_version_" type="long"         indexed="true"  stored="true"/>
  4.   
  5.   <dynamicField name="attr_*" type="text_general" indexed="true" stored="true" multiValued="true"/>
复制代码


到这里solr服务端的配置以及完成了。

二、solrj测试

1、  需要的jar

1.jpg

Maven配置

  1. <dependency>
  2.             <groupId>org.apache.solr</groupId>
  3.             <artifactId>solr-solrj</artifactId>
  4.             <version>4.7.0</version>
  5.             <scope>test</scope>
  6.     </dependency>
  7.     <dependency>
  8.             <groupId>org.apache.httpcomponents</groupId>
  9.             <artifactId>httpclient</artifactId>
  10.             <version>4.3.2</version>
  11.             <scope>test</scope>
  12.     </dependency>
复制代码


2、 测试类CreateIndexFromPDF.java

Solrj4.7里面ContentStreamUpdateRequest的addFile方法多了一个contentType参数,指明内容类型。ContentType请参看:http://baike.baidu.com/link?url=panQQa04z0gc4-gQRnIoUhwOQPABfG6unIqE1-7SEe5ZMygYxWT2lkvoKlQmTEYIZDNhntB4T9aGQM5KhevKDa



  1. package com.clj.test.solr.solrj;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.solr.client.solrj.SolrQuery;
  5. import org.apache.solr.client.solrj.SolrServer;
  6. import org.apache.solr.client.solrj.SolrServerException;
  7. import org.apache.solr.client.solrj.impl.HttpSolrServer;
  8. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
  9. import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
  10. import org.apache.solr.client.solrj.response.QueryResponse;
  11. /**
  12. * 从PDF创建索引
  13. * <功能详细描述>
  14. *
  15. * @author  Administrator
  16. * @version  [版本号, 2014年3月18日]
  17. * @see  [相关类/方法]
  18. * @since  [产品/模块版本]
  19. */
  20. public class CreateIndexFromPDF
  21. {
  22.    
  23.     public static void main(String[] args)
  24.     {
  25.         String fileName = "e:/MyBatis3用户指南中文版.pdf";
  26.         String solrId = "MyBatis3用户指南中文版.pdf";
  27.         try
  28.         {
  29.             indexFilesSolrCell(fileName, solrId);
  30.         }
  31.         catch (IOException e)
  32.         {
  33.             e.printStackTrace();
  34.         }
  35.         catch (SolrServerException e)
  36.         {
  37.             e.printStackTrace();
  38.         }
  39.         
  40.     }
  41.    
  42.     /** 从文件创建索引
  43.      * <功能详细描述>
  44.      * @param fileName
  45.      * @param solrId
  46.      * @see [类、类#方法、类#成员]
  47.      */
  48.     public static void indexFilesSolrCell(String fileName, String solrId)
  49.         throws IOException, SolrServerException
  50.     {
  51.         String urlString = "http://localhost:8080/solr/core1";
  52.         SolrServer solr = new HttpSolrServer(urlString);
  53.         ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
  54.         
  55.         String contentType="application/pdf";
  56.         up.addFile(new File(fileName), contentType);
  57.         up.setParam("literal.id", solrId);
  58.         up.setParam("uprefix", "attr_");
  59.         up.setParam("fmap.content", "attr_content");
  60.         up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
  61.         
  62.         solr.request(up);
  63.         
  64.         QueryResponse rsp = solr.query(new SolrQuery("*:*"));
  65.         System.out.println(rsp);
  66.     }
  67.    
  68. }
复制代码


执行上面代码,便把我们的pdf文件上传到solr服务器,解析、创建索引,后面的solr.query是执行一个查询,查询解析索引后结果。解析后pdf就变成了纯文本的内容,在控制台可以看到很多文档其他信息。

Solr解析完pdf、创建索引后,我们也可以在solr的管理界面查看索引结果。Core1s就是我们新建的文件索引库。如下图。

2.jpg

















没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条