使用solrj进行solr查询

1.构建maven工程,使用spring boot ,添加如下依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
        <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>
        <!-- 替换solrj为solr服务器的版本 -->
        <dependency>
              <groupId>org.apache.solr</groupId>
              <artifactId>solr-solrj</artifactId>
              <version>6.5.1</version>
        </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
 </build>

2.编写查询接口IIndexSearch

public interface IndexSearch {

    /**
     * 根据id查询内容
     *
     * @param id
     * @return
     * @throws Exception
     * @Title: search
     * @Description:
     * @return: PageData
     */
    ResultEntity search(String id);

    /**
     * @param query
     * @param isHight 是否高亮显示
     * @return
     */
    ResultEntity execute(SolrQuery query, boolean isHight);

}
  • 接口ISolrIndexSearch
public interface ISolrIndexSearch extends IndexSearch {

    /**
     * 获取SolrQuery对象
     *
     * @return
     */
    SolrQuery getQuery();

    /**
     * 查询
     *
     * @param page    封装分页
     * @param query   查询条件
     * @param isHight 是否高亮显示
     * @return
     */
    ResultEntity search(Page page, SolrQuery query, boolean isHight);

}
  • 抽象实现类AbstractSolrIndexSearch
public abstract class AbstractSolrIndexSearch implements ISolrIndexSearch {

    private static final Logger LOGGER = Logger.getLogger(AbstractSolrIndexSearch.class);

    @Resource(name = "solrClient")
    protected SolrClient solr;

    public abstract SolrQuery setHighlightHandle(SolrQuery query);

    public abstract List<PageData> gethighlighDataList(SolrResponse response);

    private ResultEntity putResponseMessage(List<PageData> datas, int responseTime, Page page) {
        ResultEntity result = new ResultEntity();
        result.setResults(datas);
        result.setResponseTime(responseTime);
        if (page != null) {
            result.setPage(page);
        }
        return result;
    }

    private PageData getSolrDocumentToPageData(SolrDocument document) {
        Iterator<Entry<String, Object>> it = document.iterator();
        PageData pd = new PageData();
        while (it.hasNext()) {
            Entry<String, Object> entry = it.next();
            pd.put(entry.getKey(), entry.getValue());
        }
        return pd;
    }

    private List<PageData> getDataFromSolrDocumentList(SolrDocumentList documents) {
        List<PageData> dataList = Lists.newArrayList();
        Iterator<SolrDocument> it = documents.iterator();
        while (it.hasNext()) {
            SolrDocument document = it.next();
            PageData pds = getSolrDocumentToPageData(document);
            dataList.add(pds);
        }
        return dataList;
    }

    @Override
    public ResultEntity execute(SolrQuery query, boolean isHight) {
        CompletableFuture<ResultEntity> completableFuture = CompletableFuture.supplyAsync(() -> {
            return getDataAysc(query, isHight);
        });
        return completableFuture.join();
    }

    private ResultEntity getDataAysc(SolrQuery query, boolean isHight) {
        ResultEntity result = new ResultEntity();
        QueryResponse response;
        List<PageData> datas;
        try {
            response = solr.query(query);
            int responseTime = response.getQTime();
            SolrDocumentList documents = response.getResults();
            if (isHight) {
                datas = gethighlighDataList(response);
            } else {
                datas = getDataFromSolrDocumentList(documents);
            }
            result = putResponseMessage(datas, responseTime, null);
            LOGGER.info("查询参数:" + query.getQuery());
        } catch (SolrServerException e) {
        } catch (IOException e) {
        }
        return result;
    }

    @Override
    public ResultEntity search(String id) {
        long start = System.nanoTime();
        SolrQuery query = new SolrQuery();
        query.setQuery(SolrStatementUtils.generateBaseMatchStatement("id", id));
        //ResultEntity result = execute(query);
        ResultEntity result = getDataAysc(query, false);
        long end = System.nanoTime();
        LOGGER.info("异步查询用时:" + (end - start) + "s");
        return result;
    }

    public ResultEntity search(Page page, SolrQuery query, boolean isHight) {
        long start = System.nanoTime();
        // 从page中获取传入的参数对象pagedata,是一个map数据结构
        PageData pd = page.getPd();

        int showCount = 0;
        int currentPage = 0;
        int totalResult = 0;

        if (null != pd.getString("currentPage")) {
            currentPage = Integer.parseInt(pd.getString("currentPage"));
        }
        if (currentPage == 0) {
            currentPage = 1;
        }
        if (null != pd.getString("showCount")) {
            showCount = Integer.parseInt(pd.getString("showCount"));
        } else {
            showCount = Const.SHOW_COUNT;
        }
        query.setStart(showCount * (currentPage - 1)).setRows(showCount);
        if (isHight) {
            query = setHighlightHandle(query);
        }

        ResultEntity result = execute(query, isHight);

        page.setCurrentPage(currentPage);
        page.setShowCount(showCount);
        page.setTotalResult(totalResult);
        page = makePage(page);

        LOGGER.info("查询参数--" + pd);
        LOGGER.info("查询:" + query.toQueryString());
        long end = System.nanoTime();
        LOGGER.info("方法执行时间:" + (end - start));
        return result;
    }

    private Page makePage(Page page) {
        page.getTotalPage();
        page.setEntityOrField(true);
        page.getPageStr();
        return page;
    }

}
  • 子类去实现抽象类中的的抽象方法SolrIndexSearch
public class SolrIndexSearch extends AbstractSolrIndexSearch {
    private static final Logger LOGGER = Logger.getLogger(SolrIndexSearch.class);

    @Override
    public SolrQuery getQuery() {
        return new SolrQuery();
    }

    @Override
    public SolrQuery setHighlightHandle(SolrQuery query) {
        return null;
    }

    @Override
    public List<PageData> gethighlighDataList(SolrResponse response) {
        return null;
    }

}

3.编写索引操作接口IndexeWriter

public interface IndexeWriter {

    ResultEntity delete(List<String> ids);

    ResultEntity delete(String id);

    void deleteAll() throws Exception;
    
    ResultEntity execute(SolrInputDocument document);

    ResultEntity update(PageData document);
    
    ResultEntity updataBatch(List<Object> documents);

    ResultEntity write(List<Object> document);

    ResultEntity write(PageData pd);

    /**
     * 创建索引
     *
     * @throws Exception
     * @Title: createIndex
     * @Description:
     * @return: void
     */
    void createIndex(String beginTime);

    void commit();

    void close();

    /**
     * 提交操作释放链接
     */
    void detory();

}
  • 抽象实现类AbstractSolrIndexWriter
public abstract class AbstractSolrIndexWriter implements IndexeWriter {

    private static final Logger LOGGER = Logger.getLogger(AbstractSolrIndexWriter.class);

    @Resource(name = "concurrentUpdateSolrClient")
    protected SolrClient solr;

    private SolrInputDocument getDocument(PageData pd) {
        SolrInputDocument document = new SolrInputDocument();
        Iterator it = pd.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            document.addField(key, pd.get(key));
        }
        return document;
    }

    private List<SolrInputDocument> getDocuments(List<PageData> pds) {
        List<SolrInputDocument> datas = Lists.newArrayList();
        for (PageData pd : pds) {
            SolrInputDocument doc = getDocument(pd);
            datas.add(doc);
        }
        return datas;
    }

    /**
     * Title: delete
     *
     * @param id
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#delete(java.lang.String)
     */
    @Override
    public ResultEntity delete(String id) {
        ResultEntity result = new ResultEntity();
        UpdateResponse response;
        try {
            response = solr.deleteById(id);
            int resopnseTime = response.getQTime();
            result.setResponseTime(resopnseTime);
            commit();
            LOGGER.info("删除索引:编号--" + id + "用时:" + resopnseTime);
        } catch (SolrServerException e) {

        } catch (IOException e) {

        }
        return result;
    }

    @Override
    public ResultEntity execute(SolrInputDocument document) {
        CompletableFuture<ResultEntity> future = CompletableFuture.supplyAsync(() -> {
            return executeAysc(document);
        });
        return future.join();
    }

    private ResultEntity executeAysc(SolrInputDocument document) {
        ResultEntity result = new ResultEntity();
        UpdateRequest request = new UpdateRequest();
        request.setAction(ACTION.COMMIT, false, false);
        request.add(document);
        UpdateResponse response;
        try {
            response = request.process(solr);
            int responseTime = response.getQTime();
            result.setResponseTime(responseTime);
            LOGGER.info("时间:" + new Date() + "用时:" + responseTime);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Title: write
     *
     * @param pd
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#write(com.jianong.springbootproject.util.PageData)
     */
    @Override
    public ResultEntity write(PageData pd) {
        ResultEntity result = new ResultEntity();
        SolrInputDocument d = getDocument(pd);
        return execute(d);
    }

    /**
     * Title: write
     *
     * @param document
     * @return
     * @see com.jianong.springbootproject.util.indexes.indexing.IndexeWriter#write(java.util.List)
     */
    @Override
    public ResultEntity write(List<PageData> documents) {
        ResultEntity result = new ResultEntity();
        List<SolrInputDocument> datas = getDocuments(documents);
        int responseTime = 0;
        try {
            UpdateResponse response = solr.add(datas);
            commit();
            responseTime = response.getQTime();
            result.setResponseTime(responseTime);
            LOGGER.info("添加索引:" + documents.size() + "条--" + "用时:" + responseTime);
        } catch (SolrServerException e) {

        } catch (IOException e) {

        }
        return result;
    }

    private SolrInputDocument getUpdateDocument(PageData pd) {
        Iterator<String> it = pd.keySet().iterator();
        SolrInputDocument document = new SolrInputDocument();
        while (it.hasNext()) {
            String key = it.next();
            if (Objects.equals("id", key)) {
                document.setField(key, pd.get("id"));
            } else {
                Map<String, Object> updateMap = Maps.newHashMapWithExpectedSize(1);
                updateMap.put("set", pd.get(key));
                document.setField(key, updateMap);
            }
        }
        return document;
    }

    private List<SolrInputDocument> getUpdateDocuments(List<PageData> documents) {
        List<SolrInputDocument> datas = Lists.newArrayList();
        for (PageData pd : documents) {
            SolrInputDocument doc = getUpdateDocument(pd);
            datas.add(doc);
        }
        return datas;
    }

    /**
     * Title: update
     *
     * @param document
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#update(com.jianong.springbootproject.util.PageData)
     */
    @Override
    public ResultEntity update(PageData document) {
        ResultEntity result = new ResultEntity();
        SolrInputDocument doc = getUpdateDocument(document);
        return execute(doc);
    }

    /**
     * Title: updataBatch
     *
     * @param documents
     * @return
     * @see com.jianong.springbootproject.util.indexes.indexing.IndexeWriter#updataBatch(java.util.List)
     */
    @Override
    public ResultEntity updataBatch(List<PageData> documents) {
        ResultEntity result = new ResultEntity();
        int count = 0;
        if (documents.size() < 20) {
            for (PageData document : documents) {
                result = update(document);
            }
        } else {
            List<SolrInputDocument> datas = getDocuments(documents);
            try {
                UpdateResponse response = solr.add(datas);
                commit();
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * Title: commit
     *
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#commit()
     */
    @Override
    public void commit() {
        try {
            solr.commit();
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

    /**
     * Title: close
     *
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#close()
     */
    @Override
    public void close() {
        try {
            commit();
            solr.close();
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

    /**
     * Title: detory
     *
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#detory()
     */
    @Override
    public void detory() {
        commit();
        close();
    }

}
  • 子类实现抽象类中未实现的接口SolrIndexWriter
public class SolrIndexWriter extends AbstractSolrIndexWriter implements IndexeWriter {

    private static final Logger LOGGER = Logger.getLogger(SolrIndexWriter.class);

    /**
     * Title: delete
     * 
     * @param ids
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#delete(java.util.List)
     */
    @Override
    public ResultEntity delete(List<String> ids) {
        return null;
    }

    /**
     * Title: deleteAll
     * 
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#deleteAll()
     */
    @Override
    public void deleteAll() throws Exception {
        UpdateResponse response = solr.deleteByQuery("*:*");
        int responseTime = response.getQTime();
        solr.commit();
    }

    /**
     * Title: createIndex
     * 
     * @param beginTime
     * @throws Exception
     * @see com.jianong.springbootproject.util.indexes.IndexeWriter#createIndex(java.lang.String)
     */
    @Override
    public void createIndex(String beginTime) {

    }

}

4.创建solr查询和操作索引的工厂,供外界调用使用SolrManager

public final class SolrManager {

    @Resource(name = "solrIndexSearch")
    private ISolrIndexSearch solrIndexSearch;

    @Resource(name = "solrIndexWriter")
    private IndexeWriter indexeWriter;

    public SolrQuery getQuery() {
        return solrIndexSearch.getQuery();
    }

    public ResultEntity search(String id) {
        return solrIndexSearch.search(id);
    }

    /**
     * 删除索引
     *
     * @param ids
     * @throws Exception
     */
    public ResultEntity delete(List<String> ids) {
        return indexeWriter.delete(ids);
    }

    public ResultEntity delete(String id) {
        return indexeWriter.delete(id);
    }

    public void deleteAll() throws Exception {
        indexeWriter.deleteAll();
    }

    public ResultEntity update(PageData document) {
        return indexeWriter.update(document);
    }

    public ResultEntity write(List<PageData> document) {
        return indexeWriter.write(document);
    }

    public ResultEntity write(PageData pd) {
        return indexeWriter.write(pd);
    }

    /**
     * 创建索引
     *
     * @throws Exception
     * @Title: createIndex
     * @Description:
     * @return: void
     */
    public void createIndex(String beginTime) throws Exception {

    }

    /**
     * 查询
     *
     * @param page
     * @param query
     * @param isHight
     * @return
     * @throws Exception
     */
    public ResultEntity search(Page page, SolrQuery query, boolean isHight) throws Exception {
        return solrIndexSearch.search(page, query, isHight);
    }

}

5.在application.properties中配置solr

solr.url=http://host:port/solr/
solr.collection=collectionname
#是否为集群
solr.cluster=false
solr.timeout=10000
solr.maxconnection=100
solr.queuesize=20
#集群中必须配置zookeeper地址
solr.zookeeper.url=192.168.0.5:2181

6.配置solr,在spring boot启动中,覆盖默认的solrClient配置

public class SolrConfig implements EnvironmentAware {

    private static Logger LOGGER = Logger.getLogger(SolrConfig.class);

    private boolean solrCluster = false;

    private SolrConfigBean config;

    @Override
    public void setEnvironment(Environment environment) {
        config = new SolrConfigBean();
        loadProperties(environment);
    }

    private void loadProperties(Environment env) {
        String url = env.getProperty("solr.url");
        int maxConnection = Integer.parseInt(env.getProperty("solr.maxconnection"));
        String collection = env.getProperty("solr.collection");
        int timeout = Integer.parseInt(env.getProperty("solr.timeout"));
        boolean solrIsCluster = Boolean.parseBoolean(env.getProperty("solr.cluster"));

        if (solrIsCluster) {
            String zookeeperUrl = env.getProperty("solr.zookeeper.url");
            if (zookeeperUrl.indexOf(",") != -1) {
                String[] tmp = zookeeperUrl.split(",");
                List<String> zkList = Lists.newArrayList(tmp);
                config.setZookeeperUrl(zkList);
            } else {
                List<String> zkList = Lists.newArrayList(zookeeperUrl);
                config.setZookeeperUrl(zkList);
            }
        }

        config.setTimeout(timeout);
        config.setUrl(url);
        config.setCollection(collection);
        config.setMaxConnection(maxConnection);
        solrCluster = solrIsCluster;

    }

    @Bean
    public SolrClient solrClient() {
        boolean falg = isSolrCluster();
        if (falg) {
            SolrClient client = new CloudSolrClient.Builder().withZkHost(config.getZookeeperUrl()).build();
            ((CloudSolrClient) client).setDefaultCollection(config.getCollection());
            ((CloudSolrClient) client).setParser(new XMLResponseParser());
            ((CloudSolrClient) client).setRequestWriter(new BinaryRequestWriter());
            return client;
        } else {
            SolrClient solr = new HttpSolrClient.Builder(getSolrUrl()).build();
            ((HttpSolrClient) solr).setParser(new XMLResponseParser());
            ((HttpSolrClient) solr).setRequestWriter(new BinaryRequestWriter());
            ((HttpSolrClient) solr).setConnectionTimeout(config.getTimeout());
            ((HttpSolrClient) solr).setSoTimeout(100000);
            ((HttpSolrClient) solr).setDefaultMaxConnectionsPerHost(config.getMaxConnection());
            return solr;
        }
    }

    @Bean
    public ConcurrentUpdateSolrClient concurrentUpdateSolrClient() {
        ConcurrentUpdateSolrClient client =
                new ConcurrentUpdateSolrClient.Builder(getSolrUrl())
                        .withQueueSize(10)
                        .build();
        client.setRequestWriter(new BinaryRequestWriter());
        client.setParser(new XMLResponseParser());
        return client;
    }

    public SolrConfigBean getConfig() {
        return config;
    }

    public String getSolrUrl() {
        StringBuffer sb = new StringBuffer();
        sb.append(config.getUrl()).append(config.getCollection());
        return sb.toString();
    }

    public boolean isSolrCluster() {
        return solrCluster;
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,678评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,490评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,493评论 18 399
  • 今天是个好日子,知道为什么吗?因为我的架子鼓能考七级了。 虽然我能考七级,但是我特别紧张,我紧张的是我的鼓花是错的...
    有点意思的嗬阅读 148评论 0 1
  • 这是一个最好的时代,它给了你遍地的机会; 这是一个最坏的时代,它给了你莫大的压力。 作为一个不善言辞的你,想要有所...
    猴子本是王阅读 347评论 0 0