Solr整合SpringBoot

搜索引擎

引入pom

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>

设置application.yml

1
2
3
4
spring:
data:
solr:
host: http://192.168.1.210:8983/solr

使用

service接口

1
2
3
4
5
6
public interface IUserService {
// 添加
String add();
// 查询
SolrDocumentList selectData(String condition);
}

service实现

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
@Service
public class UserServiceImpl implements IUserService{

@Autowired
SolrClient solrClient;

@Override
public String add() {
// 用SolrInputDocument把数据拼装成solr可以用的格式
SolrInputDocument document = new SolrInputDocument();
document.addField("bookName","《java数据结构》");
document.addField("bookPrice","32.9");
document.addField("bookAmount","32");
try {
// 存入solr客户端对象中
solrClient.add("jtxyh",document);
// 提交
solrClient.commit("jtxyh");
return "ok";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

@Override
public SolrDocumentList selectData(String condition) {
// 创建一个query对象
SolrQuery queryCondition = new SolrQuery();
// 设置检索的条件
queryCondition.setQuery(condition);
try {
// 提交检索条件,需要指明检索的库,返回QueryResponse对象,存储的是solr返回的所有数据信息
QueryResponse queryResponse = solrClient.query("jtxyh",queryCondition);
SolrDocumentList results = queryResponse.getResults();
// lambda表达式
results.forEach(System.out::println);
return results;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

Handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
public class UserHandler {

@Autowired
private IUserService userService;

// 添加
@GetMapping("add")
public String add(){
return userService.add();
}

// 查询
@GetMapping("select/{condition}")
public SolrDocumentList selectData(@PathVariable("condition") String condition){
return userService.selectData(condition);
}
}

相关文章

数据库连接池

SpringIOC

Junit和Spring

Tomcat

Servlet

Request,Response和ServletContext

Cookie和Session

JSP和EL和Jstl

Filter和Listener

Mybatis