SpringDataSolr入门程序

阳宾实
2023-12-01

SpringDataSolr简介
如何将Solr的应用集成到Spring中?Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
入门程序:
1、在solr服务器下的webapps/solr/WEB-INF/lib加入ik分词器的jar,等会会用到。

链接:https://pan.baidu.com/s/1lKV9cSyu3tBPuOd8nnNm6Q 
提取码:9999

2、在solrhome\collection1\conf\schema.xml文件末尾 配置作用域:

	<!-- 配置IK分词器 -->
	<fieldType name="text_ik" class="solr.TextField">
		<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
	</fieldType>
	
	<!-- 配置业务域 -->
	<field name="item_goodsid" type="long" indexed="true" stored="true"/>
	<field name="item_title" type="text_ik" indexed="true" stored="true"/>
	<field name="item_price" type="double" indexed="true" stored="true"/>
	<field name="item_image" type="string" indexed="false" stored="true" />
	<field name="item_category" type="string" indexed="true" stored="true" />
	<field name="item_seller" type="text_ik" indexed="true" stored="true" />
	<field name="item_brand" type="string" indexed="true" stored="true" />

	<!-- 配置复制域 -->
	<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
	<copyField source="item_title" dest="item_keywords"/>
	<copyField source="item_category" dest="item_keywords"/>
	<copyField source="item_seller" dest="item_keywords"/>
	<copyField source="item_brand" dest="item_keywords"/>
	
	<!-- 配置动态域 -->
	<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />

3、创建一个普通的maven工程,引入依赖:

 <dependencies>
        <!--Spring整合solr框架坐标-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>1.5.5.RELEASE</version>
        </dependency>
        <!--Spring整合Junit单元测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <!--引入dao工程-->
        <dependency>
            <groupId>com.soft</groupId>
            <artifactId>legou_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>

4、在resources下创建spirng/applicationContext-solr.xml:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:solr="http://www.springframework.org/schema/data/solr"
   xsi:schemaLocation="http://www.springframework.org/schema/data/solr 
      http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
      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.xsd">
      
   <!-- solr服务器地址 -->
   <solr:solr-server id="solrServer" url="http://127.0.0.1:8090/solr" />
   
   <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
   <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
      <constructor-arg ref="solrServer" />
   </bean>
   
</beans>

5、在实体类上添加@Field(“作用域名”)注解:

package cn.tx.domain;
​
import org.apache.solr.client.solrj.beans.Field;
​
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
​
public class TbItem implements Serializable {
​
    @Field
    private Long id;
​
    @Field("item_title")
    private String title;
​
    @Field("item_price")
    private BigDecimal price;
    
    @Field("item_image")
    private String image;

    @Field("item_goodsid")
    private Long goodsId;
​
    @Field("item_category")
    private String category;
​
    @Field("item_brand")
    private String brand;

    @Field("item_seller")
    private String seller;
​
   // get... set...

6、编写测试类:

package com.soft.test;
​
import com.soft.domain.TbItem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
​
import java.math.BigDecimal;
​
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext*.xml")
public class SolrDemo1 {
​
    // 注入SolrTemplate模板对象
    @Autowired
    private SolrTemplate solrTemplate;
​
    /**
     * 测试数据的添加
     */
    @Test
    public void testAdd(){
        // 创建对象,添加数据
        TbItem item = new TbItem();
        item.setId(1L);
        item.setBrand("华为");
        item.setCategory("手机");
        item.setGoodsId(1L);
        item.setSeller("华为商家");
        item.setTitle("华为标题");
        item.setPrice(new BigDecimal(1999));
        // 保存
        solrTemplate.saveBean(item);
        // 提交事务
        solrTemplate.commit();
    }
​
    /**
     * 测试修改
     */
    @Test
    public void testUpdate(){
        TbItem item = new TbItem();
        item.setId(1L);
        item.setBrand("华为2");
        item.setCategory("手机2");
        item.setGoodsId(2L);
        item.setSeller("华为商家2");
        item.setTitle("华为标题2");
        item.setPrice(new BigDecimal(1999));
        // 修改
        solrTemplate.saveBean(item);
        // 提交事务
        solrTemplate.commit();
    }
​
    /**
     * 测试查询一条数据
     */
    @Test
    public void testFindOne(){
        TbItem item = solrTemplate.getById(1L, TbItem.class);
        System.out.println(item.getTitle());
    }
​
    /**
     * 测试删除
     */
    @Test
    public void testDelete(){
        solrTemplate.deleteById("1");
        solrTemplate.commit();
    }
  
      /**
     * 删除所有数据
     */
    @Test
    public void testDelAll(){
        // 查询所有
        SolrDataQuery query = new SimpleQuery("*:*");
        solrTemplate.delete(query);
        // 提交事务
        solrTemplate.commit();
    }
    
}
 类似资料: