虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
搭建工程
<dependencies>
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>4.2.4.RELEASEversion> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.9version> dependency> dependencies> |
2.在src/main/resources下创建 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: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-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg ref="solrServer" /> bean> beans> |
@Field 注解
将项目的TbItem实体类拷入本工程 ,属性使用@Field注解标识 。 如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。
public class TbItem 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;
.......
}
增加(修改)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
@Test
public void testAdd(){
TbItem item=new TbItem();
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 testFindOne(){
TbItem item = solrTemplate.getById(1, TbItem.class);
System.out.println(item.getTitle());
}
按主键删除
@Test
public void testDelete(){
solrTemplate.deleteById("1");
solrTemplate.commit();
}
分页查询
@Test
public void testPageQuery(){
Query query=new SimpleQuery("*:*");
query.setOffset(20);//开始索引(默认0)
query.setRows(20);//每页记录数(默认10)
ScoredPage page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List list = page.getContent();
showList(list);
}
//显示记录数据
private void showList(List list){
for(TbItem item:list){
System.out.println(item.getTitle() +item.getPrice());
}
}
条件查询
Criteria 用于对条件的封装:
@Test
public void testPageQueryMutil(){
Query query=new SimpleQuery("*:*");
Criteria criteria=new Criteria("item_title").contains("2");
criteria=criteria.and("item_title").contains("5");
query.addCriteria(criteria);
//query.setOffset(20);//开始索引(默认0)
//query.setRows(20);//每页记录数(默认10)
ScoredPage page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List list = page.getContent();
showList(list);
}
删除全部数据
@Test
public void testDeleteAll(){
Query query=new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}
@Dynamic注解
修改TbItem.java ,添加属性
@Dynamic
@Field("item_spec_*")
private Map specMap;
public Map getSpecMap() {
return specMap;
}
public void setSpecMap(Map specMap) {
this.specMap = specMap;
}
导入商品数据
/**
* 导入商品数据
*/
public void importItemData(){
TbItemExample example=new TbItemExample();
Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");//已审核
List itemList = itemMapper.selectByExample(example);
System.out.println("===商品列表===");
for(TbItem item:itemList){
Map specMap= JSON.parseObject(item.getSpec());//将spec字段中的json字符串转换为map
item.setSpecMap(specMap);//给带注解的字段赋值
System.out.println(item.getTitle());
}
solrTemplate.saveBeans(itemList);
solrTemplate.commit();
System.out.println("===结束===");
}