SpringBoot项目中如何操作Solr

主要使用两种方式操作Solr

  1. 引入solrJ的坐标
  2. 使用spring-boot-data的起步依赖

第一种方式:

POM文件中引入solrJ的依赖
在这里插入图片描述
核心属性文件中配置相应信息,我这里连接的时solr的伪集群
在这里插入图片描述
创建核心属性文件中对用的配置对象

@ConfigurationProperties(prefix = "solr")
@Data
public class SolrConfig {

    private String zkhost;

    private String defaultCollection;
    
}

在启动类上添加注解@EnableConfigurationProperties(SolrConfig.class)
在这里插入图片描述
然后我又写了一个CloudSolrConfig配置类,目的是通过getCloudSolrClient()方法,生成CloudSolrClient对象

@Bean注解是将getCloudSolrClient()方法的返回值对象交给spring容器管理
@Primary注解目的是优先使用容器中这个对 象

@Configuration
public class CloudSolrConfig {

    @Autowired
    private SolrConfig solrConfig;

    @Bean
    @Primary
    public CloudSolrClient getCloudSolrClient(){
        List<String> list = new ArrayList<>();
        list.add(solrConfig.getZkhost());
        CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(list, Optional.empty()).build();
        cloudSolrClient.setDefaultCollection(solrConfig.getDefaultCollection());
        return cloudSolrClient;
    }

接着在测试类中进行了简单测试
将上面的getCloudSolrClient()方法返回值对象注入进来,通过cloudSolrClient来操作solr

@SpringBootTest
class SpringbootSolrjApplicationTests {

    @Autowired
    private CloudSolrClient cloudSolrClient;
    @Test
    void testSolrJ() {
        try {
            SolrInputDocument document = new SolrInputDocument();
            document.setField("id","0001");
            document.setField("item_title","vivo手机");
            cloudSolrClient.add(document);
            cloudSolrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

第二种方式:

创建项目时倒入spring-boot-data Solr的起步依赖
在这里插入图片描述
可以看到创建好的项目起步依赖中包含有solrJ的依赖(说明spring-boot-data-Solr对solrJ进行了封装)
在这里插入图片描述
首先还是在核心属性文件进行了配置
不同的是spring.data.solr.zk-host属于自动配置属性
solr.defaultCollection属于自定义属性
在这里插入图片描述
创建了一个SolrDataConfig类,目的是通过getSolrTemplate()方法,将此方法返回值放入spring容器中,通过SolrTemplate 对象操作solr

@Configuration
public class SolrDataConfig {

    @Value("${solr.defaultCollection}")
    private String defaultCollection;

    @Autowired
    private SolrClient solrClient;

    @Bean
    @Primary
    public SolrTemplate getSolrTemplate(){
        CloudSolrClient cloudSolrClient = (CloudSolrClient) this.solrClient;
        cloudSolrClient.setDefaultCollection(defaultCollection);
        SolrTemplate solrTemplate = new SolrTemplate(cloudSolrClient);
        return solrTemplate;
    }

}

使用测试类进行了简单测试

@SpringBootTest
class SpringbootDataSolrApplicationTests {

    @Autowired
    private SolrTemplate solrTemplate;

    @Value("${solr.defaultCollection}")
    private String defaultCollection;

    @Test
    void testSolr() {
        Item item = new Item();
        item.setId("0002");
        item.setTitle("oppo手机");
        item.setItem_price(5000);

        solrTemplate.saveBean(defaultCollection, item);
        solrTemplate.commit(defaultCollection);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值