solr单机版、集群版整合spring

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38111957/article/details/82935605

一、引言

今个起个大早,阳光依旧耀眼,赶紧把solr剩下未完成的结束掉。solr单机版、集群版咱们都已经搭建好了,之前solr单机版也使用java中的solrj来维护索引库,那么今天讲下solr集群版怎么使用solr来维护索引库,以及solr整合spring框架。

首先solr需要使用的jar,引入进来!

     <!-- solr客户端 -->
     <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>4.10.3</version>
     </dependency>

二、使用solrj维护solr集群索引库

  /**
     * 添加、修改文档
     * @throws Exception
     */
    @Test
    public void addDocment() throws Exception {

        //建立solr连接
        //单机版
        //SolrServer server = new HttpSolrServer("http://111.231.123.81:8080/solr");

        //集群版
        //zhHost表示zookeeper的集群地址
        String zkHost = "111.231.110.123:2181,111.231.110.123:2182,111.231.110.123:2183";
        CloudSolrServer server = new CloudSolrServer(zkHost);
        //设置默认Collection
        server.setDefaultCollection("collection1");

        //以下操作都是一致的,删除,查询相同道理,只是连接方式不同

        //创建一个文档对象
        SolrInputDocument document = new SolrInputDocument();
        //如果需要修改,即id一致即可
        //item_title、item_price、item_desc这些字段都是之前添加的业务字段
        document.addField("id","001");
        document.addField("item_title","iPhone XS Max");
        document.addField("item_price","9599");
        document.addField("item_desc","iPhone XS Max 支持双卡,给你更多选择,为工作、生活都带来更多便利5。这两款 iPhone 还都提供最高达 512GB 的存储容量,给你更广阔的施展空间。另外,就算它们电量...");
        //把文档写入索引库
        server.add(document);
        //提交操作
        server.commit();
    }

三、整合spring

步骤一applicationContext-solrj.xml,新建配置文件,写入如下配置。${SOLR.SERVER.URL}、${SOLR.zkHost.URL}、${SOLR.DEFAULTCOLLECTION},这些相对应的值小编放在properties配置文件中了,也可以直接写固定。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!-- 配置SolrServer对象 -->
    <!-- 单机版 -->
    <!--<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>
    </bean>-->

     <!-- 集群版 -->
    <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg name="zkHost" value="${SOLR.zkHost.URL}"></constructor-arg>
        <property name="defaultCollection" value="${SOLR.DEFAULTCOLLECTION}"></property>
    </bean>

</beans>

步骤二java中使用,直接使用spring注解,@Autowired注入SolrServer。 SolrServer是HttpSolrServer、CloudSolrServer的父类,所以如果需要切换集群版,只需要把单机版配置文件注释掉即可。不需要修改代码。

public interface SearchDao {

    /**
     * solr查询是根据SolrQuery对象来的,所以参数是SolrQuery
     * @param solrQuery
     * @return
     * @throws Exception
     */
    SearchResult getSearchList(SolrQuery solrQuery) throws Exception;
}
@Repository
public class SearchDaoImpl implements SearchDao {

    
    @Autowired
    private SolrServer solrServer;


    public Object getSearchList(SolrQuery solrQuery) throws Exception{
          //根据项目的需求,具体实现,小编只是演示怎么使用
          return null;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38111957/article/details/82935605