SpringDataSolr

Spring Data Solr

Spring Data Solr简介

Spring Data Solr 就是为了方便 Solr 的开发所研制的一个框架
其底层是对 SolrJ(官方API)的封装

Spring Data Solr入门

(1)创建 maven 工程 SpringSolrProject jar 项目

(2)pom.xml 中引入依赖

<properties>
    <spring.version>5.1.7.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-solr</artifactId>
        <version>1.5.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

(3)在 src/main/resources下创建 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://192.168.0.88:8080/solr"/>

    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer"/>
    </bean>
</beans>

(4)创建 com.javaboy.pojo 包引入 Item 实体类

@Data
public class Item implements Serializable {

    /**
     * 商品id,同时也是商品编号
     */
    @Field
    private Long id;

    /**
     * 商品标题
     */
    @Field("item_title")
    private String title;

    /**
     * 商品卖点
     */
    private String sellPoint;

    /**
     * 商品价格,单位为:元
     */
    @Field("item_price")
    private BigDecimal price;

    private Integer stockCount;

    /**
     * 库存数量
     */
    private Integer num;

    /**
     * 商品条形码
     */
    private String barcode;

    /**
     * 商品图片
     */
    @Field("item_image")
    private String image;

    /**
     * 所属类目,叶子类目
     */
    private Long categoryid;

    /**
     * 商品状态,1-正常,2-下架,3-删除
     */
    private String status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    private String itemSn;

    private BigDecimal costPirce;

    private BigDecimal marketPrice;

    private String isDefault;

    @Field("item_goodsid")
    private Long goodsId;

    private String sellerId;

    private String cartThumbnail;

    @Field("item_category")
    private String category;

    @Field("item_brand")
    private String brand;

    private String spec;

    @Field("item_seller")
    private String seller;

    private static final long serialVersionUID = 1L;
}

(5)配置业务域

修改 solrhome 的 schema.xml 文件 设置业务系统 Field

<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_updatetime" type="date" indexed="true" stored="true" />

复制域的作用在于将某一个 Field 中的数据复制到另一个域中

<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" />

配置,如果没有搭建好solr服务器,请参考:(地址)[http://www.baidu.com]

cd /usr/local/solr/solrhome/collection1/conf
vim schema.xml

重启 tomcat

/usr/local/solr/tomcat/bin/shutdown.sh
/usr/local/solr/tomcat/bin/startup.sh

(6)在实体类字段上添加 @Fiel 注解

@Data
public class Item implements Serializable {
    /**
     * 商品id,同时也是商品编号
     */
    @Field
    private Long id;

    /**
     * 商品标题
     */
    @Field("item_title")
    private String title;

    /**
     * 商品卖点
     */
    private String sellPoint;

    /**
     * 商品价格,单位为:元
     */
    @Field("item_price")
    private BigDecimal price;

    private Integer stockCount;

    /**
     * 库存数量
     */
    private Integer num;

    /**
     * 商品条形码
     */
    private String barcode;

    /**
     * 商品图片
     */
    @Field("item_image")
    private String image;

    /**
     * 所属类目,叶子类目
     */
    private Long categoryid;

    /**
     * 商品状态,1-正常,2-下架,3-删除
     */
    private String status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    private String itemSn;

    private BigDecimal costPirce;

    private BigDecimal marketPrice;

    private String isDefault;

    @Field("item_goodsid")
    private Long goodsId;

    private String sellerId;

    private String cartThumbnail;

    @Field("item_category")
    private String category;

    @Field("item_brand")
    private String brand;

    private String spec;

    @Field("item_seller")
    private String seller;

    private static final long serialVersionUID = 1L;

}

(7)创建测试类

添加

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-solr.xml"})
public class SpringDatSolrDemo {

    @Autowired
    private SolrTemplate solrTemplate;

    @Test
    public void testIndexCreatAndUpdate(){
        List<Item> itemList = new ArrayList();
        for (int i = 0; i < 100; i++) {
            Item item = new Item();
            item.setId((long) i);
            item.setTitle("鲁班" + i);
            item.setCategory("射手");
            item.setPrice(new BigDecimal("100" + i));
            item.setBrand("王者");

            itemList.add(item);
        }

        // 保存
        solrTemplate.saveBeans(itemList);

        // 提交
        solrTemplate.commit();
    }

}

删除

@Test
public void testIndexDelte() {
    //根据主键域id删除
    //solrTemplate.deleteById("1");

    //创建查询对象
    Query query = new SimpleQuery("*:*");
    //根据查询条件删除
    solrTemplate.delete(query);
    //提交
    solrTemplate.commit();
}

查询

@Test
public void testSearch() {
    //创建查询对象
    //Query query = new SimpleQuery("*:*");

    //创建查询对象
    Query query = new SimpleQuery();
    //创建查询条件对象, 注意这里的Criteria对象和mybatis中的那个不是同一个, 只是同名而已
    Criteria criteria = new Criteria("item_title").contains("鲁班");
    //查询对象中放入查询条件
    query.addCriteria(criteria);
    //从第几条开始查询
    query.setOffset(11);
    //设置每页查询多少条数据
    query.setRows(20);
    //查询并返回结果
    ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
    //总页数
    int totalPages = items.getTotalPages();
    //查询到的总记录数
    long totalElements = items.getTotalElements();
    //查询到的数据集合
    List<Item> content = items.getContent();
    //每页有多少条数据
    int numberOfElements = items.getNumberOfElements();
    System.out.println("==========");
    System.out.println(content);
}
posted @ 2020-04-04 21:27  Leader_TBlog  阅读(111)  评论(0编辑  收藏  举报