• springBoot整合solr


    述:接上篇solr学习笔记https://blog.csdn.net/Dawn____Dawn/article/details/126230673

    1. maven 依赖

    
    <dependency>
        <groupId>org.springframework.datagroupId>
        <artifactId>spring-data-solrartifactId>
    dependency>
    
    
    
    <dependency>
        <groupId>org.apache.solrgroupId>
        <artifactId>solr-solrjartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2. spring-data-solr对solrJ进行了封装。

    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.params.AuthPolicy;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.solr.core.RequestMethod;
    import org.springframework.data.solr.core.SolrTemplate;
    import org.springframework.data.solr.server.support.HttpSolrClientFactory;
    
    /**
     * SolrConfig
     *
     **/
    @Configuration
    public class SolrConfig {
    
        @Value("${spring.data.solr.name:}")
        private String name;
    
        @Value("${spring.data.solr.pwd:}")
        private String pwd;
    
        @Value("${spring.data.solr.host:}")
        private String serverUrl;
    
        @Bean
        public SolrTemplate solrTemplate(HttpSolrClientFactory httpSolrClientFactory) {
            return new SolrTemplate(httpSolrClientFactory, RequestMethod.POST);
        }
    
        @Bean
        public HttpSolrClientFactory httpSolrClientFactory() {
            EimsSolrClient solrClient = new EimsSolrClient(new EimsSolrClient.Builder(this.serverUrl));
            solrClient.setBasicAuthUser(this.name);
            solrClient.setBasicAuthPwd(this.pwd);
            Credentials defaultcreds = new UsernamePasswordCredentials(this.name, this.pwd);
            return new HttpSolrClientFactory(solrClient, defaultcreds, AuthPolicy.BASIC);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    3. 配置文件

    # solr
    spring.data.solr.host=http://dev.solr.himeili.com/solr
    spring.data.solr.name=admin
    spring.data.solr.pwd=cto856
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4. 调用示例

    添加索引

    @Autowired
    private SolrTemplate solrTemplate;
    
    @Autowired
    private ProjectService projectService;
    
    /**
     * 保存(新增、修改)
     *
     **/
    @Override
    public boolean save(long id) {
        ProjectModel model = this.projectService.getModelById(id);
        if(model == null) {
            logger.error("未找到要添加的项目!");
            return false;
        }
    
        if(!model.getMiniSale()) {
            return this.delById(model.getId());
        }
        ProjectOperationDto dto = new ProjectOperationDto();
        dto.setSolrId(String.valueOf(model.getId()));
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setStatus(model.getStatus());
        dto.setSummary(model.getSummary());
        dto.setCatNamePath(model.getProjectCatNamePath());
        dto.setSpecifications(model.getSpecifications());
        dto.setPrice(model.getPrice() != null? model.getPrice().doubleValue() : Double.valueOf(0));
        dto.setMiniSale(model.getMiniSale());
        dto.setSaleTimeLimited(model.getSaleTimeLimited());
        dto.setBeginTime(model.getSaleBeginTime());
        dto.setEndTime(model.getSaleEndTime());
    
        UpdateResponse response = null;
        try {
            response = solrTemplate.saveBean(SolrCollectionsEnum.PROJECT_CORE.getCore(), dto);
            solrTemplate.commit(SolrCollectionsEnum.PROJECT_CORE.getCore());
        } catch (Exception e) {
            logger.error("索引保存异常", e);
        }
    
        if(response != null && response.getStatus() == 0) {
            return true;
        }
        return false;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    删除索引

    /**
     * 删除
     **/
    @Override
    public boolean delById(long id) {
        UpdateResponse response = null;
        try {
            response = solrTemplate.deleteByIds(SolrCollectionsEnum.PROJECT_CORE.getCore(), String.valueOf(id));
            solrTemplate.commit(SolrCollectionsEnum.PROJECT_CORE.getCore());
        } catch (Exception e) {
            logger.error("索引删除异常", e);
        }
        if(response != null && response.getStatus() == 0) {
            return true;
        }
        return false;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5. 开启基本身份验证

    创建一个security.json文件开启基本的身份验证。

  • 相关阅读:
    【Android】坐标系
    笔试强训(三十五)
    rk3568 RT-LINUX 测试
    【算法】二分查找-20231120
    opencv c++ 霍夫圆检测
    Wireshark TS | SYN Flood 攻击案例
    OpenCV(Python)的二值化示例
    Linux本地WBO创作白板部署与远程访问
    HashMap原理
    Kaldi语音识别工具编译问题记录(踩坑记录)
  • 原文地址:https://blog.csdn.net/Dawn____Dawn/article/details/126252706