搜索引擎入门

搜索学习--Solr全文搜索服务器的基本使用(一)——SolrJ

2018-06-08  本文已影响0人  _时间海

作为一般的Lucene使用者,我们直接用Lucene虽然可以实现很多自己想要的自定义功能,但是对于一般的项目,为了方便开发和维护,我们通常会使用现成的搜索服务器。现在常用的有SolrElasticSearch
对于Solr服务器如何搭建,Solr服务器的搭建,之前已经整理过,这次,我将使用SolrJ来调用Solr服务器。

在Solr中配置Core

在managed-schema中,主要的配置为:

<uniqueKey>blogId</uniqueKey>
<field name="blogId" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="blogTitle" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="blogContent" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="createTime" type="date" indexed="false" stored="true" required="true" multiValued="false" /> 
<field name="keywords" type="text_ik" indexed="true" stored="false"  multiValued="true"/> 
<copyField source="blogTitle" dest="keywords"/>
<copyField source="blogContent" dest="keywords"/>
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" useSmart="false"
                class="org.wltea.analyzer.lucene.IKAnalyzer" />
<analyzer type="query" useSmart="true"
                class="org.wltea.analyzer.lucene.IKAnalyzer" />
</fieldType>

依赖

<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>6.5.1</version>
</dependency>

SolrServer.Java

package top.yuyufeng.learn.lucene.solr;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
public class SolrServer {
    private static HttpSolrClient server = null;
    private final static String solrServerUrl = "http://127.0.0.1:8983/solr/blog";

    public static HttpSolrClient getServer() {
        if (server == null) {
            server = new HttpSolrClient(solrServerUrl);
            server.setDefaultMaxConnectionsPerHost(1000);
            server.setMaxTotalConnections(10000);//最大连接数
            server.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000
            server.setSoTimeout(60000);//// 设置读数据超时时间(单位毫秒) 1000
            server.setFollowRedirects(false);//遵循从定向
            server.setAllowCompression(true);//允许压缩
        }
        return server;
    }

    public static void main(String[] args) {
        HttpSolrClient client = getServer();
        System.out.println(client);
    }
}

BlogCore.java

package top.yuyufeng.learn.lucene.solr.core;

import org.apache.solr.client.solrj.beans.Field;

import java.util.Date;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
public class BlogCore {
    @Field
    private String blogId;
    @Field
    private String blogTitle;
    @Field
    private String blogContent;
    @Field
    private Date createTime;
    @Field
    private String keywords;

    public String getBlogId() {
        return blogId;
    }

    public void setBlogId(String blogId) {
        this.blogId = blogId;
    }

    public String getBlogTitle() {
        return blogTitle;
    }

    public void setBlogTitle(String blogTitle) {
        this.blogTitle = blogTitle;
    }

    public String getBlogContent() {
        return blogContent;
    }

    public void setBlogContent(String blogContent) {
        this.blogContent = blogContent;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    @Override
    public String toString() {
        return "BlogCore{" +
                "blogId='" + blogId + '\'' +
                ", blogTitle='" + blogTitle + '\'' +
                ", blogContent='" + blogContent + '\'' +
                ", createTime=" + createTime +
                ", keywords='" + keywords + '\'' +
                '}';
    }
}

BlogSolrDao.java

package top.yuyufeng.learn.lucene.solr.dao;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import top.yuyufeng.learn.lucene.solr.SolrServer;
import top.yuyufeng.learn.lucene.solr.core.BlogCore;

import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
public class BlogSolrDao{
    HttpSolrClient server;

    public BlogSolrDao() {
        server = SolrServer.getServer();
    }

    /**
     * 按Bean  添加/修改 索引
     *
     * @throws Exception
     */
    public int addIndex(BlogCore entity) throws Exception {
        server.addBean(entity);
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }


    /**
     * 按Bean  添加/修改 索引
     *
     * @throws Exception
     */
    public int addIndexList(List<BlogCore> entitys) throws Exception {
        server.addBeans(entitys);
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();

    }

    /**
     * 删除索引 按查询
     *
     * @throws Exception
     */
    public int deleteAll() throws Exception {
        String query = "*:*";
        server.deleteByQuery(query);
        server.commit();
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }

    /**
     * 删除索引 按id
     *
     * @throws Exception
     */
    public int deleteByQuery(Long id) throws Exception {
        server.deleteById(id + "");
        server.commit();
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }

    //查询所有
    public List<BlogCore> query() throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setStart(0);//开始记录数
        query.setRows(10000);//总条数
        QueryResponse queryResponse = server.query(query);
        List<BlogCore> results = queryResponse.getBeans(BlogCore.class);
        return results;
    }


    //搜索keywords
    public  List<BlogCore> queryByKeyWords(String keywords) throws Exception {

        SolrQuery query = new SolrQuery();
        query.set("q", "keywords:" + keywords);//*通配多个字符
//        query.set("sort", "product_price desc");
        //======高亮设置===
        //开启高亮
        query.setHighlight(true);
        //高亮域
        query.addHighlightField("blogContent");
        query.addHighlightField("blogTitle");
        //前缀
        query.setHighlightSimplePre("<B>");
        //后缀
        query.setHighlightSimplePost("</B>");
        //query.setHighlightSnippets(1);//结果分片数,默认为1
        //query.setHighlightFragsize(1000);//每个分片的最大长度,默认为100

        query.setStart(0);//开始记录数
        query.setRows(100);//总条数
        QueryResponse queryResponse = server.query(query);
        long sum = queryResponse.getResults().getNumFound();
        List<BlogCore> results = queryResponse.getBeans(BlogCore.class);
        //输出高亮
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

        for (BlogCore result : results) {
            Map<String, List<String>> map = highlighting.get(result.getBlogId()+"");
            List<String> list = map.get("blogTitle");
            if (list != null && list.size() > 0) {
                result.setBlogTitle(list.get(0));
            }
            list = map.get("blogContent");
            if (list != null && list.size() > 0) {
                result.setBlogContent(list.get(0));
            }
        }
       return results;
    }
}

SolrTest.java

package top.yuyufeng.learn.lucene.solr;

import org.junit.Test;
import top.yuyufeng.learn.lucene.solr.core.BlogCore;
import top.yuyufeng.learn.lucene.solr.dao.BlogSolrDao;

import java.util.Date;
import java.util.List;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
public class SolrTest {

    @Test
    public void testIndex() throws Exception {
        BlogSolrDao blogSolrDao = new BlogSolrDao();
        BlogCore blog = new BlogCore();
        blog.setBlogId("2");
        blog.setBlogTitle("达摩院超越业界龙头");
        blog.setBlogContent("达摩院一定也必须要超越英特尔,必须超越微软,必须超越IBM,因为我们生于二十一世纪,我们是有机会后发优势的。");
        blog.setCreateTime(new Date());
        blogSolrDao.addIndex(blog);
    }

    @Test
    public void testFindAll() throws Exception {
        BlogSolrDao blogSolrDao = new BlogSolrDao();
        List<BlogCore> list = blogSolrDao.query();
        for (BlogCore blogCore : list) {
            System.out.println(blogCore);
        }
    }


    @Test
    public void testSearch() throws Exception {
        BlogSolrDao blogSolrDao = new BlogSolrDao();
        List<BlogCore> list = blogSolrDao.queryByKeyWords("达摩院");
        for (BlogCore blogCore : list) {
            System.out.println(blogCore);
        }
    }
}

运行结果:

启动Solr服务器后,我建立了一些索引,然后我执行Search方法

BlogCore{blogId='2', blogTitle='<B>达摩</B><B>院</B>超越业界龙头', blogContent='<B>达摩</B><B>院</B>一定也必须要超越英特尔,必须超越微软,必须超越IBM,因为我们生于二十一世纪,我们是有机会后发优势的。', createTime=Wed Dec 06 13:38:12 CST 2017, keywords='null'}
BlogCore{blogId='1', blogTitle='马云表达愿景', blogContent='10月11日杭州云栖大会上,马云表达了对新建成的阿里巴巴全球研究<B>院</B>—阿里巴巴<B>达摩</B><B>院</B>的愿景,希望<B>达摩</B><B>院</B>二十年内成为世界第一大经济体,服务世界二十亿人,创造一亿个工作岗位。', createTime=Wed Dec 06 12:03:56 CST 2017, keywords='null'}
上一篇下一篇

猜你喜欢

热点阅读