栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot整合solr

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot整合solr

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

1. maven 依赖


    org.springframework.data
    spring-data-solr





    org.apache.solr
    solr-solrj

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;


@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);
    }
}

3. 配置文件
# solr
spring.data.solr.host=http://dev.solr.himeili.com/solr
spring.data.solr.name=admin
spring.data.solr.pwd=cto856

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;

}

删除索引

@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;
}

5. 开启基本身份验证

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1039400.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号