This article describes integrate solr in springboot in.
1, the premise of restraint
- Solr complete word in, stop words, and extended thesaurus configuration
https://www.jianshu.com/p/0e6f4f4a6505
2, Procedure
- Create a springboot project
https://www.jianshu.com/p/de979f53ad80 - Was added in pom.xml dependency:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- SolrConfig.java create a startup class in the main directory at the same level
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SolrConfig {
@Bean
public HttpSolrServer getHttpSolrServer() {
return new HttpSolrServer("http://192.168.100.192:8080/solr/collection1");
}
}
- SolrController.java create a startup class in the main directory at the same level
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class SolrController {
@Resource
HttpSolrServer httpSolrServer;
@GetMapping("/adddoc")
public String insertDoc() throws Exception {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "1");
document.addField("companyname", "江苏万和");
document.addField("companydesc", "万和IT教育创办于1993年,课程有Java开发培训、UI设计培训、Web前端培训、Python人工智能、软件测试、大数据等高薪包就业课程,以及华为认证、思科认证、Oracle认证等");
// 把文档对象写入索引库
httpSolrServer.add(document);
httpSolrServer.commit();
return "ok";
}
}
Browser to access http: // localhost: 8080 / adddoc , you can index the document.
These are integrated with solr of springboot.