如何将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. mysql按位的索引判断位的值

    DELIMITER $$ DROP FUNCTION IF EXISTS `value_of_bit_index`$$/*计算数字的某个位的值*/CREATE FUNCTION `value_of_b ...

  2. Bash 脚本 去除注释

    sed -i "/^#/d;/^ *$/d" /etc/squid/squid.conf

  3. 防止xss和sql注入:JS特殊字符过滤正则

    function stripscript(s) { var pattern = new RegExp("[%--`~!@#$^&*()=|{}':;',\\[\\].<> ...

  4. 下载频道--IT资源关东煮第二期[申明:来源于网络]

    下载频道–IT资源关东煮第二期[申明:来源于网络] 地址:http://geek.csdn.net/news/detail/129509?ref=myread

  5. EF Unknown column 'Project1.FamilyMembers_ID' in 'field list'

    产生成的查询语句中有:Project1.FamilyMembers_ID 字段 原因:就是对象主键对象中有一个属性!

  6. SpringMVC访问静态资源的三种方式

    如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题.如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg ...

  7. stm8s 引脚电平异常

    特别注意: 1.有iic 的引脚为了兼容电平,一般来说都是可忍受电平,同时该引脚也将被去除推挽输出和强输出能力,甚至是上拉,使用时候特别注意,这种引脚在stm8上非常常见 2.stm引脚对电平不匹配非 ...

  8. [No0000152]C#基础之IL,轻松读懂IL

    先说说学IL有什么用,有人可能觉得这玩意平常写代码又用不上,学了有个卵用.到底有没有卵用呢,暂且也不说什么学了可以看看一些语法糖的实现,或对.net理解更深一点这些虚头巴脑的东西.其实IL本身逻辑很清 ...

  9. .net 问题

    1.socket初始化三个步骤 2.多线程 3.mvc的理解

  10. [math] sagemath

    官网首页:http://www.sagemath.org 首页里引出的两个教程 http://www.gregorybard.com/Sage.html http://sagebook.gforge. ...