如何将Solr的应用集成到Spring中?

SpringDataSolr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ的封装.

SpringDataSolr入门小Demo

首先目录结构为:

搭建工程,需要引入的依赖为:

   <dependencies>
<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>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</build>

如果jar包不完全引入,就会出现报错的情况的.

然后在Resource下创建的配置文件是:

<?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.200.128:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
http://192.168.200.128:8080   这是我在虚拟机中配置的tomcat,然后在虚拟机中配置的solr

如果属性和配置文件定义的域名不一致的时候,需要在注解中指定域名称.
这是我配置的pojo类
import org.apache.solr.client.solrj.beans.Field;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; public class Item implements Serializable {
@Field
private Long id; @Field("item_title")
private String title; @Field("item_price")
private BigDecimal price; @Field("item_image")
private String image; @Field("item_goodsid")
private Long goodsId; @Field("item_category")
private String category; @Field("item_brand")
private String brand; @Field("item_seller")
private String seller; /**
* 商品id,同时也是商品编号
*/ /**
* 商品标题
*/ /**
* 商品卖点
*/
private String sellPoint; /**
* 商品价格,单位为:元
*/ private Integer stockCount; /**
* 库存数量
*/
private Integer num; /**
* 商品条形码
*/
private String barcode; /**
* 商品图片
*/ /**
* 所属类目,叶子类目
*/
private Long categoryid; /**
* 商品状态,1-正常,2-下架,3-删除
*/
private String status; /**
* 创建时间
*/
private Date createTime; /**
* 更新时间
*/
@Field("item_updatetime")
private Date updateTime; private String itemSn; private BigDecimal costPirce; private BigDecimal marketPrice; private String isDefault; private String sellerId; private String cartThumbnail; private String spec; private static final long serialVersionUID = 1L; 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();
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", title=").append(title);
sb.append(", sellPoint=").append(sellPoint);
sb.append(", price=").append(price);
sb.append(", stockCount=").append(stockCount);
sb.append(", num=").append(num);
sb.append(", barcode=").append(barcode);
sb.append(", image=").append(image);
sb.append(", categoryid=").append(categoryid);
sb.append(", status=").append(status);
sb.append(", createTime=").append(createTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", itemSn=").append(itemSn);
sb.append(", costPirce=").append(costPirce);
sb.append(", marketPrice=").append(marketPrice);
sb.append(", isDefault=").append(isDefault);
sb.append(", goodsId=").append(goodsId);
sb.append(", sellerId=").append(sellerId);
sb.append(", cartThumbnail=").append(cartThumbnail);
sb.append(", category=").append(category);
sb.append(", brand=").append(brand);
sb.append(", spec=").append(spec);
sb.append(", seller=").append(seller);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
} @Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Item other = (Item) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getTitle() == null ? other.getTitle() == null : this.getTitle().equals(other.getTitle()))
&& (this.getSellPoint() == null ? other.getSellPoint() == null : this.getSellPoint().equals(other.getSellPoint()))
&& (this.getPrice() == null ? other.getPrice() == null : this.getPrice().equals(other.getPrice()))
&& (this.getStockCount() == null ? other.getStockCount() == null : this.getStockCount().equals(other.getStockCount()))
&& (this.getNum() == null ? other.getNum() == null : this.getNum().equals(other.getNum()))
&& (this.getBarcode() == null ? other.getBarcode() == null : this.getBarcode().equals(other.getBarcode()))
&& (this.getImage() == null ? other.getImage() == null : this.getImage().equals(other.getImage()))
&& (this.getCategoryid() == null ? other.getCategoryid() == null : this.getCategoryid().equals(other.getCategoryid()))
&& (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getItemSn() == null ? other.getItemSn() == null : this.getItemSn().equals(other.getItemSn()))
&& (this.getCostPirce() == null ? other.getCostPirce() == null : this.getCostPirce().equals(other.getCostPirce()))
&& (this.getMarketPrice() == null ? other.getMarketPrice() == null : this.getMarketPrice().equals(other.getMarketPrice()))
&& (this.getIsDefault() == null ? other.getIsDefault() == null : this.getIsDefault().equals(other.getIsDefault()))
&& (this.getGoodsId() == null ? other.getGoodsId() == null : this.getGoodsId().equals(other.getGoodsId()))
&& (this.getSellerId() == null ? other.getSellerId() == null : this.getSellerId().equals(other.getSellerId()))
&& (this.getCartThumbnail() == null ? other.getCartThumbnail() == null : this.getCartThumbnail().equals(other.getCartThumbnail()))
&& (this.getCategory() == null ? other.getCategory() == null : this.getCategory().equals(other.getCategory()))
&& (this.getBrand() == null ? other.getBrand() == null : this.getBrand().equals(other.getBrand()))
&& (this.getSpec() == null ? other.getSpec() == null : this.getSpec().equals(other.getSpec()))
&& (this.getSeller() == null ? other.getSeller() == null : this.getSeller().equals(other.getSeller()));
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
result = prime * result + ((getSellPoint() == null) ? 0 : getSellPoint().hashCode());
result = prime * result + ((getPrice() == null) ? 0 : getPrice().hashCode());
result = prime * result + ((getStockCount() == null) ? 0 : getStockCount().hashCode());
result = prime * result + ((getNum() == null) ? 0 : getNum().hashCode());
result = prime * result + ((getBarcode() == null) ? 0 : getBarcode().hashCode());
result = prime * result + ((getImage() == null) ? 0 : getImage().hashCode());
result = prime * result + ((getCategoryid() == null) ? 0 : getCategoryid().hashCode());
result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getItemSn() == null) ? 0 : getItemSn().hashCode());
result = prime * result + ((getCostPirce() == null) ? 0 : getCostPirce().hashCode());
result = prime * result + ((getMarketPrice() == null) ? 0 : getMarketPrice().hashCode());
result = prime * result + ((getIsDefault() == null) ? 0 : getIsDefault().hashCode());
result = prime * result + ((getGoodsId() == null) ? 0 : getGoodsId().hashCode());
result = prime * result + ((getSellerId() == null) ? 0 : getSellerId().hashCode());
result = prime * result + ((getCartThumbnail() == null) ? 0 : getCartThumbnail().hashCode());
result = prime * result + ((getCategory() == null) ? 0 : getCategory().hashCode());
result = prime * result + ((getBrand() == null) ? 0 : getBrand().hashCode());
result = prime * result + ((getSpec() == null) ? 0 : getSpec().hashCode());
result = prime * result + ((getSeller() == null) ? 0 : getSeller().hashCode());
return result;
}
}

对solr进行增删改查的步骤为:

import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; /**
* @Auther:qingmu
* @Description:脚踏实地,只为出人头地
* @Date:Created in 16:44 2019/4/19
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestTemplate { @Autowired
private SolrTemplate solrTemplate; /**
* 添加
*/
@Test
public void testAdd() {
Item item = new Item();
item.setId(1L);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate9");
item.setPrice(new BigDecimal(2000));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
/**
* 按主键进行查询
*/
@Test
public void testQueryById(){
Item item = solrTemplate.getById(1, Item.class);
System.out.println(item.getTitle());
} /**
*按主键删除
*/
@Test
public void testDelete(){
solrTemplate.deleteById("1");
solrTemplate.commit();
}
//创建100条数据
@Test
public void test100(){
ArrayList<Item> itemArrayList = new ArrayList<Item>();
for (long i = 0; i < 100; i++) {
Item item = new Item();
item.setId(i);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(i);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate9");
item.setPrice(new BigDecimal(2000+i));
itemArrayList.add(item);
}
solrTemplate.saveBeans(itemArrayList);
solrTemplate.commit();
} /**
* 编写分页查询的代码
*/
@Test
public void queryAll(){
// 创建查询对象
Query query = new SimpleQuery("*:*");
//开始
query.setOffset(20);
query.setRows(28);
ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
System.out.println( "总记录:"+items.getTotalElements());
List<Item> content = items.getContent(); for (Item item : content) {
System.out.println(item.getTitle()+"..."+item.getPrice());
}
} /**
* 条件查询
*/
@Test
public void queryByEx(){
Query query = new SimpleQuery("*:*");
Criteria criteria = new Criteria();
criteria = criteria.or("item_title").contains("9");
criteria = criteria.or("item_title").contains("9");
query.addCriteria(criteria);
System.out.println(criteria.toString());
ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
System.out.println(items.getTotalElements());
for (Item item : items) {
System.out.println(item.getTitle()+"...."+item.getPrice());
}
} @Test
public void deleteTest(){
SimpleQuery simpleQuery = new SimpleQuery("*:*");
solrTemplate.delete(simpleQuery);
solrTemplate.commit();
} }

Spring Data Solr入门的更多相关文章

  1. Solr学习笔记(5)—— Spring Data Solr入门

    一.Spring Data Solr简介 前面已经介绍了通过solrJ来操作solr,那么我们如何将Solr的应用集成到Spring中?Spring Data Solr就是为了方便Solr的开发所研制 ...

  2. Spring Data Solr入门小Demo

    package com.offcn.pojo; import java.io.Serializable; import java.math.BigDecimal; import java.util.D ...

  3. Spring Data Solr教程(翻译) 开源的搜索服务器

    Solr是一个使用开源的搜索服务器,它采用Lucene Core的索引和搜索功能构建,它可以用于几乎所有的编程语言实现可扩展的搜索引擎. Solr的虽然有很多优点,建立开发环境是不是其中之一.此博客条 ...

  4. Spring Data Solr教程(翻译)

    大多数应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能 这就是为什么转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr ...

  5. Spring Data Solr

    1.什么是spring data solr? Solr是一个开源搜索平台,用于构建搜索应用程序.简单的来说就是作为一个搜索引擎使用. 2.solr的安装(本地安装,远程安装同) 1)解压一个tomca ...

  6. Spring Data Solr —— 快速入门

    Solr是基于Lucene(全文检索引擎)开发,它是一个独立系统,运行在Tomcat或Jetty(solr6以上集成了jetty,无需再部署到servlet容器上),但其原生中文的分词词功能不行,需要 ...

  7. Spring Data Solr相关配置

    1.增加Maven POM文件的存储库:pom配置如下: <repositories> <repository> <id>spring-milestone</ ...

  8. Spring Data Solr操作solr的简单案例

    Spring Data Solr简介 虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便 ...

  9. 记录一次Spring Data Solr相关的错误解决

    记录一次Spring Data Solr相关的错误解决 生活本不易,流人遂自安 相信大家也使用过SpringDataSolr,但是在最新版的SpringDataSolr 4.0.5 RELEASE中有 ...

随机推荐

  1. apache 2.4.23 只能本地访问,其他用户不能访问,提示You don't have permission to access

    这个版本的httpd.conf的配置方法跟原版本的设置不一样了. 需要在目录安全配置中 修改为 Require all granted 比如  把Require local 修改为Require al ...

  2. VSCode远程调试Go程序方法(Attach)

    set launch.json { "name": "Attach", "type": "go", "requ ...

  3. PCB画板总结

    最近几天完成了第一个PCB电路板.虽然器件不是很多,手动布线了4次才达到自己理想的效果. 但是还是有很多细节只有亲自拿到了自己做的板子,亲自焊接之后,才知道自己哪里不合适. 这是修改了4次之后的最终的 ...

  4. meat标签

    1.文档兼容模式的定义 Edge 模式告诉 IE 以最高级模式渲染文档,也就是任何 IE 版本都以当前版本所支持的最高级标准模式渲染,避免版本升级造成的影响.简单的说,就是什么版本 IE 就用什么版本 ...

  5. gnome-shell 扩展

    application menu topicons plus dash to dock native window placement netspeed no topleft hot corner o ...

  6. 方差+标准差+四分位数+z-score公式

    一.方差公式 $S^2 = \frac{1}{N}\sum_{i=1}^{N}(X_i - \mu)^2 = \frac{1}{N}[(X_1-\mu)^2 + (X_2-\mu)^2 + ... + ...

  7. ios学习--iphone 实现下拉菜单

    原文地址:ios学习--iphone 实现下拉菜单作者:sdglyuan00 #import @interface DropDown1 : UIView <</span>UITabl ...

  8. python使用suds调用webservice接口

    最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了 1.安装suds mac: sudo pip in ...

  9. odoo二次开发 tips

    1.model属性 每个对象(即class)一般由字段(变量)和函数组成,每个对象对应着数据库中的一张表,驼峰命名方式 models.Model 基础模块,会根据字段和模型名在后台数据库生成对应得表文 ...

  10. 初试kotlin:用Kotlin开发桌面/CommandLine 工具

    既然kotlin是google和jetbrain联合搞的,开发环境不用说了肯定是Intellij Idea了. 先创建一个kotlin项目. 先来一个HelloWorld package com.xi ...