述:接上篇solr学习笔记https://blog.csdn.net/Dawn____Dawn/article/details/126230673
1. maven 依赖2. spring-data-solr对solrJ进行了封装。org.springframework.data spring-data-solr org.apache.solr 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=cto8564. 调用示例
添加索引
@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文件开启基本的身份验证。



