<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
@Component
public class MyBean {
private SolrServer solr;
@Autowired
public MyBean(SolrServer solr) {
this.solr = solr;
}
// ...
}
# SOLR (SolrProperties) spring.data.solr.host=http://localhost:8983/solr #spring.data.solr.zkHost= spring.data.solr.repositories.enabled=true
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="spring.solr")
public class SolrConfig {
private String host;
private String zkHost;
private String defaultCollection;
public String getDefaultCollection() {
return defaultCollection;
}
public void setDefaultCollection(String defaultCollection) {
this.defaultCollection = defaultCollection;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getZkHost() {
return zkHost;
}
public void setZkHost(String zkHost) {
this.zkHost = zkHost;
}
@Configuration
@EnableConfigurationProperties(SolrConfig.class)
public class SolrClientConfig {
@Autowired
private SolrConfig solrConfig;
private CloudSolrServer solrServer;
@PreDestroy
public void close() {
if (this.solrServer != null) {
try {
this.solrServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Bean
public CloudSolrServer SolrServer(){
if (StringUtils.hasText(this.solrConfig.getZkHost())) {
solrServer = new CloudSolrServer(this.solrConfig.getZkHost());
solrServer.setDefaultCollection(this.solrConfig.getDefaultCollection());
}
return this.solrServer;
}
}
@RestController
public class HelloController {
@Autowired
private CloudSolrServer solrserver;
public String hello(){
return"say hello";
}
@RequestMapping("test")
public void test(){
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q","demo:素文宅博客");
params.add("ws","json");
params.add("start","0");
params.add("rows","10");
QueryResponse response = null;
try{
response=solrserver.query(params);
SolrDocumentList results = response.getResults();
for (SolrDocument document : results) {
System.out.println( document.getFieldValue("demo"));
System.out.println(document.getFieldValue("id"));
}
}catch(Exception e){
e.getStackTrace();
}
System.out.println(response.toString());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有