Solr4.7从文件创建索引

来源:互联网 发布:linux 挂载iso命令 编辑:程序博客网 时间:2024/05/21 09:20

索引数据源并不会一定来自于数据库、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

添加请求解析配置:

<requestHandler name="/update/extract" class="solr.extraction.ExtractingRequestHandler" >   <lst name="defaults">    <str name="fmap.content">text</str>    <str name="lowernames">true</str>    <str name="uprefix">attr_</str>    <str name="captureAttr">true</str>   </lst>  </requestHandler>

指定依赖包位置:

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

<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一个位置。

 <types>   <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>   <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>   <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">      <analyzer type="index">        <tokenizer class="solr.StandardTokenizerFactory"/>        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />        <filter class="solr.LowerCaseFilterFactory"/>      </analyzer>      <analyzer type="query">        <tokenizer class="solr.StandardTokenizerFactory"/>        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>        <filter class="solr.LowerCaseFilterFactory"/>      </analyzer>    </fieldType>  </types>

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

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

 <field name="id"        type="string"       indexed="true"  stored="true"  multiValued="false" required="true"/>  <field name="text"      type="text_general" indexed="true"  stored="true"/>  <field name="_version_" type="long"         indexed="true"  stored="true"/>    <dynamicField name="attr_*" type="text_general" indexed="true" stored="true" multiValued="true"/>

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

二、solrj测试

1、  需要的jar

Maven配置

<dependency>    <groupId>org.apache.solr</groupId>    <artifactId>solr-solrj</artifactId>    <version>4.7.0</version>    <scope>test</scope>    </dependency>    <dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.3.2</version>    <scope>test</scope>    </dependency>



2、
测试类CreateIndexFromPDF.java

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

 

package com.clj.test.solr.solrj;import java.io.File;import java.io.IOException;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.request.AbstractUpdateRequest;import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;import org.apache.solr.client.solrj.response.QueryResponse;/** * 从PDF创建索引 * <功能详细描述> *  * @author  Administrator * @version  [版本号, 2014年3月18日] * @see  [相关类/方法] * @since  [产品/模块版本] */public class CreateIndexFromPDF{        public static void main(String[] args)    {        String fileName = "e:/MyBatis3用户指南中文版.pdf";         String solrId = "MyBatis3用户指南中文版.pdf";         try        {            indexFilesSolrCell(fileName, solrId);        }        catch (IOException e)        {            e.printStackTrace();        }        catch (SolrServerException e)        {            e.printStackTrace();        }            }        /** 从文件创建索引     * <功能详细描述>     * @param fileName     * @param solrId     * @see [类、类#方法、类#成员]     */    public static void indexFilesSolrCell(String fileName, String solrId)         throws IOException, SolrServerException    {        String urlString = "http://localhost:8080/solr/core1";        SolrServer solr = new HttpSolrServer(urlString);        ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");                String contentType="application/pdf";        up.addFile(new File(fileName), contentType);        up.setParam("literal.id", solrId);        up.setParam("uprefix", "attr_");        up.setParam("fmap.content", "attr_content");        up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);                solr.request(up);                QueryResponse rsp = solr.query(new SolrQuery("*:*"));        System.out.println(rsp);    }    }

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

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


5 0
原创粉丝点击