spring-data-solr 4.x.x RELEASE 配置和基本用法

上一篇: Solr8 和 ik-analyzer中文分词器配置 以及 spring-data-solr 4.x.x RELEASE基本用法

承接上一篇Solr8 和 ik-analyzer中文分词器配置 这篇主要说明spring-data-solr 4.x.x RELEASE 配置和基本用法

本博客源代码下载: https://download.csdn.net/download/qq_14940627/11065231

一、配置solr

在自己创建的Core目录中找到managed-schema,我的路径是 /opt/solr/solr_home/collection/conf/managed-schema

添加如下代码,有关solr域的知识,请大家参考其它资料

    
	
	
	
	
	
    

    
    
    
    
    

    

二、配置Spring 

1)pom.xml 文件内容如下:



    4.0.0

    com.hyy.demo
    spring_data_solr_demo
    1.0-SNAPSHOT

    
        11
    

    
        
            org.springframework.data
            spring-data-solr
            4.0.5.RELEASE
        
        
            org.springframework
            spring-test
            5.1.3.RELEASE
        
        
            junit
            junit
            4.12
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    11
                    11
                
            
        
    


2)在Spring 的配置文件 applicationContext-solr.xml 中,如下配置:




	
	
		
	

3)在实体类Bean所需要搜索字段中添加 @Field 注解 如果字段名和managed-schema配置的不一样需要加别名:

package com.hyy.pojo;

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;

    private String sellPoint;

    @Field("item_price")
    private Long price;

    private Integer stockCount;

    private Integer num;

    private String barcode;

    @Field("item_image")
    private String image;

    private Long categoryid;

    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;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }

    public String getSellPoint() {
        return sellPoint;
    }

    public void setSellPoint(String sellPoint) {
        this.sellPoint = sellPoint == null ? null : sellPoint.trim();
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public Integer getStockCount() {
        return stockCount;
    }

    public void setStockCount(Integer stockCount) {
        this.stockCount = stockCount;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode == null ? null : barcode.trim();
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image == null ? null : image.trim();
    }

    public Long getCategoryid() {
        return categoryid;
    }

    public void setCategoryid(Long categoryid) {
        this.categoryid = categoryid;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status == null ? null : status.trim();
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getItemSn() {
        return itemSn;
    }

    public void setItemSn(String itemSn) {
        this.itemSn = itemSn == null ? null : itemSn.trim();
    }

    public BigDecimal getCostPirce() {
        return costPirce;
    }

    public void setCostPirce(BigDecimal costPirce) {
        this.costPirce = costPirce;
    }

    public BigDecimal getMarketPrice() {
        return marketPrice;
    }

    public void setMarketPrice(BigDecimal marketPrice) {
        this.marketPrice = marketPrice;
    }

    public String getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault == null ? null : isDefault.trim();
    }

    public Long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    public String getSellerId() {
        return sellerId;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId == null ? null : sellerId.trim();
    }

    public String getCartThumbnail() {
        return cartThumbnail;
    }

    public void setCartThumbnail(String cartThumbnail) {
        this.cartThumbnail = cartThumbnail == null ? null : cartThumbnail.trim();
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category == null ? null : category.trim();
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand == null ? null : brand.trim();
    }

    public String getSpec() {
        return spec;
    }

    public void setSpec(String spec) {
        this.spec = spec == null ? null : spec.trim();
    }

    public String getSeller() {
        return seller;
    }

    public void setSeller(String seller) {
        this.seller = seller == null ? null : seller.trim();
    }

    @Override
    public String toString() {
        return "TbItem{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", sellPoint='" + sellPoint + '\'' +
                ", price=" + price +
                ", stockCount=" + stockCount +
                ", num=" + num +
                ", barcode='" + barcode + '\'' +
                ", image='" + image + '\'' +
                ", categoryid=" + categoryid +
                ", status='" + status + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", itemSn='" + itemSn + '\'' +
                ", costPirce=" + costPirce +
                ", marketPrice=" + marketPrice +
                ", isDefault='" + isDefault + '\'' +
                ", goodsId=" + goodsId +
                ", sellerId='" + sellerId + '\'' +
                ", cartThumbnail='" + cartThumbnail + '\'' +
                ", category='" + category + '\'' +
                ", brand='" + brand + '\'' +
                ", spec='" + spec + '\'' +
                ", seller='" + seller + '\'' +
                '}';
    }
}

三、spring-data-solr 4.x.x的基本用法

一切配置准备好后,就可以使用 spring-data-solr 4 单元测试文件如下:

package com.hyy.pojo;


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.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Optional;

/**
 * Created by 勇哥.
 * Email: [email protected]
 * 2019-03-26
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TbItemTest {

    @Autowired
    private SolrTemplate solrTemplate;

    @Test
    public void testAdd(){
        TbItem item = new TbItem();
        item.setId(2L);
        item.setTitle("华为METE10");
        item.setCategory("手机");
        item.setSeller("华为旗舰店");
        item.setPrice(5100L);
        solrTemplate.saveBean("collection",item);
        solrTemplate.commit("collection");

    }

    @Test
    public void findById(){
        Optional item = solrTemplate.getById("collection", 2L, TbItem.class);
        assert item.orElse(null) != null;
        System.out.println(item.orElse(null).getTitle());
    }

    @Test
    public void deleteById(){
        SimpleQuery simpleQuery = new SimpleQuery("id:2");
        solrTemplate.delete("collection", simpleQuery);
        solrTemplate.commit("collection");
    }

    @Test
    public void testAddList(){

        for (int i=0; i<100; i++){
            TbItem item = new TbItem();
            item.setId(i+1L);
            item.setTitle("华为METE-"+i);
            item.setCategory("手机-"+i);
            item.setBrand("华为-"+i);
            item.setSeller("华为旗舰店-"+i);
            item.setPrice(5100L+i);
            solrTemplate.saveBean("collection", item);

        }

        solrTemplate.commit("collection");
    }

    @Test
    public void testPageQuery(){

        SimpleQuery query = new SimpleQuery("*:*");
        query.setOffset(20L); //开始索引
        query.setRows(20); //每页纪录数

        ScoredPage page = solrTemplate.queryForPage("collection", query, TbItem.class);

        for (TbItem item: page.getContent()){
            System.out.println(item.getTitle()+"  "+ item.getPrice()+ "  "+ item.getBrand());
        }
        System.out.println("总记录数:" + page.getTotalElements());
        System.out.println("总页数:"+ page.getTotalPages());
    }

    @Test
    public void testPageQueryMutil(){

        SimpleQuery query = new SimpleQuery("*:*");
        Criteria criteria = new Criteria("item_category").contains("手机");
        criteria= criteria.and("item_brand").contains("2");

//        query.setOffset(20L); //开始索引
//        query.setRows(20); //每页纪录数
        query.addCriteria(criteria);

        ScoredPage page = solrTemplate.queryForPage("collection", query, TbItem.class);

        for (TbItem item: page.getContent()){
            System.out.println(item.getTitle()+"  "+ item.getPrice()+ "  "+ item.getBrand());
        }
        System.out.println("总记录数:" + page.getTotalElements());
        System.out.println("总页数:"+ page.getTotalPages());

    }

    @Test
    public void deleteAll(){
        SimpleQuery simpleQuery = new SimpleQuery("*:*");
        solrTemplate.delete("collection", simpleQuery);
        solrTemplate.commit("collection");
    }






}

注意: collection 是所创建的Core 的名称不要写错了

最后我们看一下实际效果:

                       spring-data-solr 4.x.x RELEASE 配置和基本用法_第1张图片

 

你可能感兴趣的:(spring-data-solr 4.x.x RELEASE 配置和基本用法)