该篇文章记录的是springboot 连接solr进行添加以及修改
如果springboot还没有连接solr请参考上一篇文档springboot 连接solr操作(一)
一、创建自动挂载(SolrConfig)后续的操作都会用到这个文件(文件名可以以及自己的习惯进行命名)
下面的注解请参考参考这篇文章
package com.demo.demo.service;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
/**
* @program: com.demo.demo.service
* @author: zwx
* @create: 2020-12-01 17:00
**/
@Configuration
public class SolrConfig {
@Bean
@ConditionalOnMissingBean(SolrTemplate.class) // 根据情况
public SolrTemplate solrTemplate(SolrClient solrClient)
{
return new SolrTemplate(solrClient);
}
}
二、创建solr的操作类文件(GoodsSolrOperations)
package com.demo.demo.service;
import com.demo.demo.bean.solr.GoodsSolrEntity;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.stereotype.Service;
import java.util.Arrays;
/**
* @program: com.demo.demo.service
* @author: zwx
* @create: 2020-12-01 17:14
**/
@Service // 声明为一个服务类可以在控制器中使用@Autowired进行自动加载
public class GoodsSolrOperations {
@Autowired // 自动加载
private SolrTemplate solrTemplate;
/**
* 修改以及添加solr文档
* @param goodsSolrEntity solr对应的实体类
*/
public void addGoodsSolr(GoodsSolrEntity goodsSolrEntity)
{
UpdateResponse response = solrTemplate.saveBean("haircut_goods", goodsSolrEntity);
solrTemplate.commit("haircut_goods");
}
}
由于添加以及修改都是需要将所有数据传递过来的,所以添加和修改可以使用同一个方法,在修改时solr会检查主键是否存在如果存在则进行修改,如果不存在则进行添加
三、控制器中使用
package com.demo.demo.controller;
import com.demo.dmeo.bean.HaircutUsersEntity;
import com.demo.dmeo.bean.solr.GoodsSolrEntity;
import com.demo.dmeo.service.GoodsSolrOperations;
import com.demo.dmeo.service.impl.HaircutUsers;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping("index")
public class IndexController {
@Autowired
private HaircutUsers haircutUsers;
@Autowired
private SolrClient solrClient;
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private GoodsSolrOperations goodsSolrOperations;
@GetMapping("index/{nickname}")
public void Index(@PathVariable(value = "nickname") String nickname) {
GoodsSolrEntity goodsSolrEntity = new GoodsSolrEntity();
goodsSolrEntity.setId("5");
goodsSolrEntity.setCategory_id((long) 1);
goodsSolrEntity.setGoods_name("香蕉");
goodsSolrEntity.setGoods_img("11111111");
goodsSolrEntity.setGoods_thumb("["+"11111"+",111111"+"]");
goodsSolrEntity.setIs_hot((long) 1);
goodsSolrEntity.setIs_sale((long) 1);
goodsSolrEntity.setIs_new((long) 1);
goodsSolrEntity.setMarket_price("100");
goodsSolrEntity.setShop_price("100");
goodsSolrEntity.setGoods_attr_list("[]");
goodsSolrEntity.setDesc("描述");
goodsSolrEntity.setShelves_at("111111111111");
goodsSolrEntity.setCreated_at("2020-12-01 14:21:55");
goodsSolrEntity.setUpdated_at("2020-12-01 14:21:55");
goodsSolrEntity.setStore_id((long) 3);
goodsSolrOperations.addGoodsSolr(goodsSolrEntity);
}
}
添加修改还有其他的方法:
saveDocument() 该方法用的不是实体类的形式 实例化 SolrInputDocument
通过 document.addField(“id”, 3);方法进行添加
SolrInputDocument document = new SolrInputDocument();
document.addField("id", 3);
UpdateResponse response = solrTemplate.saveDocument("haircut_goods", document); // 同solr操作类一样 solrTemplate通过 @Autowired // 自动加载 private SolrTemplate solrTemplate;
批量添加其原理大同小异:
// saveBeans 方法 实例化多个solr实体类
GoodsSolrEntity goodsSolrEntity = new GoodsSolrEntity();
GoodsSolrEntity goodsSolrEntity1 = new GoodsSolrEntity();
// 然后通过实体类的set设置值
UpdateResponse response = solrTemplate.saveBeans("haircut_goods", Arrays.asList(goodsSolrEntity , goodsSolrEntity1));
// Arrays.asList() 将数组转化成List集合
采坑记录:
1、实体类一定要与solr的数据类型一致,否则会报错
2、如果在使用solr之前已经存在了数据建议先将solr的数据先删除掉(有可能会报错),然后在进行添加
传送门:
springboot 连接solr操作(一)
@Configuration 注解解析
以上的文件包名请依据自己的实际情况进行命名