Demo前的准备
1. 安装solr
2. 配置zk分词器
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
3. 配置自定义域
-------------------------自定义域起----------------------------------
普通域
<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_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" />
-------------------------自定义域结束---------------------------------
4. 了解属性含义
name: 域的名称
type: 域的类型,需要分词的字段需要把类型设置为 text_ik
index: 是否按照这个域进行索引(查询)
store: 是否进行存储
5.何时创建索引?如何创建索引?
* 是否需要在页面显示,显示就将store设置为true
* 是否需要被索引,也就是可以按这个域进行查询,如果需要 indexed=true
* 查看静态页面进行具体的分析
例如:
field1是index,store=false,field2是store=true,非index
那么你搜field1,可以搜到结果,但是只会显示field2的数据,因为field1的数据没有store,也就是存储的意思
搜field2你是搜不出来的,因为field2没有被index,也就是分词结果存储
1.导入maven坐标
<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>
2.配置spring配置文件
<?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">
<bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg index="0" value="http://192.168.25.128:8080/solr"></constructor-arg>
</bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
3.在实体类中加入Field注解(完成实体类与索引库域的映射)
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;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
@Dynamic
@Field("item_spec_*")
private Map<String, String> specMap;
public Map<String, String> getSpecMap() {
return specMap;
}
public void setSpecMap(Map<String, String> specMap) {
this.specMap = specMap;
}
}
4.对Solr索引库进行CRUD操作
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.domain.PageRequest
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
import com.pinyougou.pojo.TbItem
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestSolr {
@Autowired
private SolrTemplate solrTemplate
// 保存修改
@Test
public void saveOrUpdate() throws Exception {
TbItem item = new TbItem()
item.setId(1L)
item.setTitle("华为荣耀")
item.setImage("http://192.168.25.133/group1/M00/00/00/wKgZhVt2dxyAGd_NAAAXideDuZI485.jpg")
item.setBrand("华为")
item.setCategory("手机")
item.setSeller("华为小店")
item.setPrice(new BigDecimal(200.01))
item.setGoodsId(1L)
solrTemplate.saveBean(item)
solrTemplate.commit()
}
// 根据id查询
@Test
public void findById() throws Exception {
TbItem item = solrTemplate.getById(1L, TbItem.class)
System.out.println(item.getTitle())
}
// 根据id进行删除
@Test
public void delById() throws Exception {
solrTemplate.deleteById("1")
solrTemplate.commit()
}
// 批量导入
@Test
public void batchAdd() throws Exception {
List<TbItem> list = new ArrayList<TbItem>()
for (int i = 0
TbItem item = new TbItem()
item.setId(1L + i)
item.setTitle("华为荣耀" + i)
item.setImage("http://192.168.25.133/group1/M00/00/00/wKgZhVt2dxyAGd_NAAAXideDuZI485.jpg")
item.setBrand("华为")
item.setCategory("手机")
item.setSeller("华为小店")
item.setPrice(new BigDecimal(200.01 + i))
item.setGoodsId(1L + i)
list.add(item)
}
solrTemplate.saveBeans(list)
solrTemplate.commit()
}
// 分页查询
@Test
public void pageQuery() throws Exception {
Query query = new SimpleQuery("*:*")
// query.setPageRequest(new PageRequest(1, 20))
query.setOffset(21)
query.setRows(20)
ScoredPage<TbItem> pageResult = solrTemplate.queryForPage(query, TbItem.class)
System.out.println("总记录数:" + pageResult.getTotalElements())
System.out.println("总页数: " + pageResult.getTotalPages())
for (TbItem tbItem : pageResult.getContent()) {
System.out.println(tbItem.getTitle() + " " + tbItem.getPrice())
}
}
// 条件查询
@Test
public void criteriaQuery() throws Exception {
Query query = new SimpleQuery()
// 构建条件
Criteria criteria = new Criteria("item_category").contains("手")
criteria = criteria.and("id").contains("2")
criteria = criteria.and("item_title").contains("2")
query.addCriteria(criteria)
// 分页
query.setPageRequest(new PageRequest(0, 10))
ScoredPage<TbItem> pageResult = solrTemplate.queryForPage(query, TbItem.class)
System.out.println("总记录数:" + pageResult.getTotalElements())
System.out.println("总页数: " + pageResult.getTotalPages())
for (TbItem tbItem : pageResult.getContent()) {
System.out.println(tbItem.getId() + " " + tbItem.getTitle() + " " + tbItem.getPrice())
}
}
// 删除所有
@Test
public void delAll() throws Exception {
Query query = new SimpleQuery("*:*")
solrTemplate.delete(query)
solrTemplate.commit()
}
}