Spring整合单机版SolrJ

版权声明:本文为博主原创文章,转载请注明链接地址。谢谢! https://blog.csdn.net/wdy_2099/article/details/78732832

一.引入maven依赖

这里用的是solr6.6.2

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

二.创建applicationContext-solr.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!--定义solr的客户端-->
    <context:property-placeholder location="classpath:solr.properties"/>
    <bean id="httpSolrClient" class="org.apache.solr.client.solrj.impl.HttpSolrClient">
        <!--<constructor-arg index="0" value="${solr.baseUrl}"/>-->
        <constructor-arg index="0" value="http://localhost:8080/solr/stu_core"/>
        <!-- 设置响应解析器 -->
        <property name="parser">
            <bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
        </property>
        <!-- 建立连接的最长时间 -->
        <!--<property name="connectionTimeout" value="${solr.connectionTimeout}"/>-->
        <property name="connectionTimeout" value="3000"/>
    </bean>
</beans>

solr.properties:
我这里的核心core名称是stu_core

solr.baseUrl = http://localhost:8080/solr/stu_core
solr.connectionTimeout=3000

三.将applicationContext-solr.xml引入到Spring主配置文件中

放置到applicationContext.xml文件最后即可

<!--solr 开始-->
    <import resource="applicationContext-solr.xml"/>
<!--solr 结束-->

四.编写测试Controller

package com.wdy.controller;

import com.wdy.entity.TStudent;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;


@RequestMapping("solr")
@RestController
public class ImportSolrData {

    @Autowired
    private HttpSolrClient httpSolrClient;

    /**
     * Solr整合Spring测试
     * @param keywords
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    @RequestMapping("keywordsSearchSpring")
    public List<TStudent> keywordsSearchSpring(String keywords) throws IOException, SolrServerException {
        List<TStudent> tStudents;
        //2.调用Solr查询关键字方法
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("name:"+keywords);
        //执行查询
        QueryResponse query = httpSolrClient.query(solrQuery);
        //解析返回集合
        tStudents = query.getBeans(TStudent.class);
        return tStudents;
    }
}

五.测试及其结果

使用HttpClientUtil测试:

        String url = "http://localhost:8089/solr/keywordsSearchSpring";
        Map<String, String> params = new HashMap<String, String>();
        params.put("keywords", "散文");
        String res = HttpClientUtil.sendPost(url, params, "utf-8",null);
        System.out.println(res);

测试结果如下:

[
    {
        "id": 1,
        "name": "散文是一种抒发作者真情实感、写作方式灵活的记叙类文学小明",
        "age": 111,
        "classid": 1,
        "tid": 1
    },
    {
        "id": 5,
        "name": "散文是一种抒发作者真情实感、写作方式灵活的记叙类文学体裁",
        "age": 0,
        "classid": 111,
        "tid": 1
    }
]

为了对比结果,附上所有数据:
这里写图片描述

再附上使用Solr的web管理工具查询到的结果:结果一致。
这里写图片描述

当然,solrJ也可以单独封装工具类使用,没必要和Spring整合。这里只是给出一种配置方案。以供大家学习参考。

猜你喜欢

转载自blog.csdn.net/wdy_2099/article/details/78732832