作者:akj | 来源:互联网 | 2023-05-18 09:36
前提:安装好了solr 并且导入了ik分词器,能够启动solr
1.导入jar包
org.springframework.data
spring-data-solr
1.5.5.RELEASE
org.springframework
spring-test
4.2.4.RELEASE
junit
junit
4.9
2.配置applicationContext-solr.xml文件
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">
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中配置相应的域
id
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 page = solrTemplate.queryForPage(query, TbItem.class);
List list = page.getContent();
for(int i = 0;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 page = solrTemplate.queryForPage(query, TbItem.class);
List list = page.getContent();
System.out.println(list.size());
for(int i = 0;i System.out.println(list.get(i).getBrand());
}
}
//带条件删除
@Test
public void tes9() {
Query query = new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}
}