Spring-data-solr入門

前提:安裝好了solr 並且導入了ik分詞器,能夠啓動solr

1.導入jar包

<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>4.2.4.RELEASE</version> 
</dependency> 
<dependency> 
 	<groupId>junit</groupId> 
 	<artifactId>junit</artifactId> 
 	<version>4.9</version> 
</dependency> 

2.配置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.25.132:8080/solr" />


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

3.創建實體類

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.solr.client.solrj.beans.Field;

public class TbItem implements Serializable{

@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;

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;

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 BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal 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();
}

}

4.在solrhome/collection1/conf/schema.xml中配置相應的域

  <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="false" 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" />
 
<uniqueKey>id</uniqueKey>

<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

在這裏插入圖片描述5.solr基本操作

import java.lang.invoke.VarHandle;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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.Query;
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;

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

@Autowired
private SolrTemplate solrTemplate;

//添加
@Test
public void tes() {
	TbItem tbItem = new TbItem();
	tbItem.setId(1L);
	tbItem.setTitle("吃吃吃");
	tbItem.setBrand("華爲");
	tbItem.setSeller("威威");
	tbItem.setGoodsId(2L);
	tbItem.setPrice(new BigDecimal(3333));
	
	solrTemplate.saveBean(tbItem);
	solrTemplate.commit();
}

//更新
@Test
public void tes2() {
	TbItem tbItem = new TbItem();
	tbItem.setId(1L);
	tbItem.setTitle("吃吃吃");
	tbItem.setBrand("華爲");
	tbItem.setSeller("威威");
	tbItem.setGoodsId(10L);
	tbItem.setPrice(new BigDecimal(3333));
	
	solrTemplate.saveBean(tbItem);
	solrTemplate.commit();
}


@Test
public void tes3() {
	TbItem tbItem = new TbItem();
	tbItem.setId(2L);
	tbItem.setTitle("吃吃吃");
	tbItem.setBrand("華爲");
	tbItem.setSeller("威威");
	tbItem.setGoodsId(10L);
	tbItem.setPrice(new BigDecimal(3333));
	
	solrTemplate.saveBean(tbItem);
	solrTemplate.commit();
}

//根據id查詢
@Test
public void tes4() {
	TbItem tbItem = solrTemplate.getById("1",TbItem.class);
	System.out.println(tbItem.getTitle());
}

//根據id刪除
@Test
public void tes5() {
	solrTemplate.deleteById("1");
	solrTemplate.commit();
}

//批量導入
@Test
public void tes7() {
	List list = new ArrayList(); 
	for(int i = 0 ; i<=100;i++) {
		TbItem tbItem = new TbItem();
		tbItem.setId(2L+i);
		tbItem.setTitle("吃吃吃"+i);
		tbItem.setBrand("華爲"+i);
		tbItem.setSeller("威威"+i);
		tbItem.setGoodsId(10L);
		tbItem.setPrice(new BigDecimal(3333+i));
		list.add(tbItem);
	}
	
	solrTemplate.saveBeans(list);
	solrTemplate.commit();
}

//查詢
@Test
public void tes6() {
	Query query = new SimpleQuery("*:*");
	query.setOffset(10);
	query.setRows(20);
	ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
	List<TbItem> list = page.getContent();
	
	for(int i = 0;i<list.size();i++) {
		System.out.println(list.get(i).getBrand());
	}
}


//帶條件查詢
@Test
public void tes8() {
	Query query = new SimpleQuery("*:*");
	Criteria criteria = new Criteria("item_title").contains("4");
	criteria = criteria.and("item_title").contains("5");
	query.addCriteria(criteria);
	ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
	List<TbItem> list = page.getContent();
	System.out.println(list.size());
	for(int i = 0;i<list.size();i++) {
		System.out.println(list.get(i).getBrand());
	}
}

//帶條件刪除
@Test
public void tes9() {
	Query query = new SimpleQuery("*:*");
	solrTemplate.delete(query);
	solrTemplate.commit();
}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章