上一篇: 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域的知识,请大家参考其它资料
<field name="item_goodsid" type="plong" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="string" 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" />二、配置Spring
1)pom.xml 文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hyy.demo</groupId>
<artifactId>spring_data_solr_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>2)在Spring 的配置文件 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:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd">
<solr:solr-client id="solrClient" url="http://localhost:8983/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrClient" />
</bean>
</beans>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: yonggemails@gmail.com
* 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<TbItem> 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<TbItem> 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<TbItem> 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 的名称不要写错了
最后我们看一下实际效果:

版权声明:本文为qq_14940627原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。