Solrj创建doc/pdf/txt文件索引,高亮查询

在solrj这块折腾了挺长时间,项目终于有些进展,总结一下在项目过程中的一些知识点,以便以后查阅学习。

1. solrj的maven配置
项目使用maven进行管理的话,就需要引入maven配置。

        <!--solrj-->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.2</version>
            <!--这块千万不要添加<scope>test</scope>域,这个要表明的意思是httpclient的包只能在test中使用,因为没注意这个小细节被坑惨了-->
        </dependency>

2. Solrj文件索引、高亮查询工具类

具体的solr配置在这里我们不进行讲解,我们只关注solrj的使用。在项目中,将solrj的文件索引、高亮查询等功能进行了封装。废话不多说,代码上来。

import org.apache.log4j.Logger;
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;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class SolrHelper {
    private static Logger log=Logger.getLogger(SolrHelper.class);
    private static String SOLR_URL=null;
    private static String FILE_URL=null;

    public static QueryResponse solrQuery(SolrServer solr,String qt) throws Exception{
        SolrQuery query=new SolrQuery("attr_content:"+qt);
        query.setHighlight(true);
        query.setParam("hl.fl", "attr_content");//高亮显示字段
        query.setHighlightSimplePre("<font color=\'red\'>");//前缀
        query.setHighlightSimplePost("</font>");//后缀
        query.setHighlightFragsize(200);//返回字符数
        QueryResponse rsp = null;
        rsp = solr.query(query);
        return rsp;
    }
    /**
     * 读取配置文件中的SOLR服务地址和索引文件路径
     * @throws IOException
     */
    public static void loadProperties() throws IOException{
        Properties prop=PropertieHelper.getPropInstance("solr.properties");//封装了配置文件读取功能
        SOLR_URL = prop.getProperty("solrurl");
        FILE_URL = prop.getProperty("fileurl");
        log.info(SOLR_URL + "\n" + FILE_URL);
    }
    /**
     * 初始化solrserver
     * @return
     */
    public static SolrServer initSolrServer() throws IOException {
        loadProperties();
        SolrServer solr=new HttpSolrServer(SOLR_URL);
        return solr;
    }
    /**
     * 获取索引文件路径
     * @return
     * @throws IOException
     */
    public static String getFileUrl() throws IOException {
        if(FILE_URL==null){
            loadProperties();
        }
        return FILE_URL;
    }
    /**
     * 文件夹索引
     * @throws Exception
     */
    public static SolrServer indexFilesSolr() throws Exception {
        SolrServer solr=initSolrServer();
        File file=new File(FILE_URL);
        String files[]=file.list();
        for(int i=0;i<files.length;i++){
            indexFileSolr(files[i],FILE_URL+files[i],files[i],solr);
        }
        return solr;
    }
    /**
     * 单文件索引
     * @param id
     * @param fileurl
     * @param filename
     * @param solr
     * @throws Exception
     */
    public static void indexFileSolr(String id,String fileurl,String filename,SolrServer solr) throws Exception
    {
        ContentStreamUpdateRequest up=new ContentStreamUpdateRequest("/update/extract");
        String contenttype=getFileContentType(filename);
        if(!contenttype.equals("othertype"))
        {
            log.error(fileurl);
            File file=new File(fileurl);
            if(file.exists()){
                log.info("create index:" + fileurl);
                up.addFile(file, contenttype);
                up.setParam("literal.id", id);
                up.setParam("uprefix", "attr_");
                up.setParam("fmap.content", "attr_content");
                up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
                solr.request(up);
                log.info("end index:"+fileurl);
            }else
            {
                log.info("file no exist!");
            }
        }
    }

    /**
     * 根据文件名获取文件的ContentType类型
     * @param filename
     * @return
     */
    public static String getFileContentType(String filename)
    {
        String contentType=null;
        String prefix=filename.substring(filename.lastIndexOf(".")+1);
        if(prefix.equals("xlsx"))
        {
            contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        }else if(prefix.equals("pdf"))
        {
            contentType="application/pdf";
        }else if(prefix.equals("doc"))
        {
            contentType="application/msword";
        }else if(prefix.equals("txt"))
        {
            contentType="text/plain";
        }else if(prefix.equals("xls"))
        {
            contentType="application/vnd.ms-excel";
        }else if(prefix.equals("docx"))
        {
            contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        }else if(prefix.equals("ppt"))
        {
            contentType="application/vnd.ms-powerpoint";
        }else if(prefix.equals("pptx"))
        {
            contentType="application/vnd.openxmlformats-officedocument.presentationml.presentation";
        }
        else
        {
            contentType="othertype";
        }
        return contentType;
    }
}

至此,我们可以创建doc/pdf/txt等文件的索引,并能进行高亮查询。接下来,就让我们愉快的使用solrj吧^_^

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值