SpringBoot整合Solr

释放双眼,带上耳机,听听看~!

文章目录

  • SpringBoot整合Solr

  • 【一】创建项目

    • 【二】添加maven依赖
    • 【三】配置yml
    • 【四】创建Bean
  • 4.1 创建Bean
    * 4.2 添加字段索引

    • 【五】service
  • 5.1 StudentService
    * 5.2 StudentServiceImpl

    • 【六】controller
    • 【七】链接

SpringBoot整合Solr

【一】创建项目

创建springboot项目,可以跟着创建项目一步一步地走

SpringBoot整合Solr

【二】添加maven依赖


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4    <modelVersion>4.0.0</modelVersion>
5    <parent>
6        <groupId>org.springframework.boot</groupId>
7        <artifactId>spring-boot-starter-parent</artifactId>
8        <version>2.2.2.RELEASE</version>
9        <relativePath/> <!-- lookup parent from repository -->
10    </parent>
11    <groupId>com.feng</groupId>
12    <artifactId>springboot-solr</artifactId>
13    <version>0.0.1-SNAPSHOT</version>
14    <name>springboot-solr</name>
15    <description>Demo project for Spring Boot</description>
16
17    <properties>
18        <java.version>1.8</java.version>
19    </properties>
20
21    <dependencies>
22        <!--整合solr-->
23        <dependency>
24            <groupId>org.springframework.boot</groupId>
25            <artifactId>spring-boot-starter-data-solr</artifactId>
26        </dependency>
27
28        <!--web-->
29        <dependency>
30            <groupId>org.springframework.boot</groupId>
31            <artifactId>spring-boot-starter-web</artifactId>
32        </dependency>
33
34        <!--lombok简化-->
35        <dependency>
36            <groupId>org.projectlombok</groupId>
37            <artifactId>lombok</artifactId>
38            <optional>true</optional>
39        </dependency>
40
41        <!--测试-->
42        <dependency>
43            <groupId>org.springframework.boot</groupId>
44            <artifactId>spring-boot-starter-test</artifactId>
45            <scope>test</scope>
46            <exclusions>
47                <exclusion>
48                    <groupId>org.junit.vintage</groupId>
49                    <artifactId>junit-vintage-engine</artifactId>
50                </exclusion>
51                <exclusion>
52                    <groupId>org.springframework.boot</groupId>
53                    <artifactId>spring-boot-starter-logging</artifactId>
54                </exclusion>
55            </exclusions>
56        </dependency>
57
58        <!-- 引入log4j日志依赖 -->
59        <dependency>
60            <groupId>org.springframework.boot</groupId>
61            <artifactId>spring-boot-starter-log4j</artifactId>
62            <version>1.3.8.RELEASE</version>
63        </dependency>
64    </dependencies>
65
66    <build>
67        <plugins>
68            <plugin>
69                <groupId>org.springframework.boot</groupId>
70                <artifactId>spring-boot-maven-plugin</artifactId>
71            </plugin>
72        </plugins>
73    </build>
74
75</project>
76
77
78

【三】配置yml

在yml添加配置,当前的路径保证服务器上已经安装solr

ps:如果服务器上海没有安装好solr请先安装好solr:https://blog.csdn.net/wenge1477/article/details/103446992


1
2
3
4
5
6
1spring:
2  data:
3    solr:
4      host: http://192.168.183.129:8080/solr
5
6

【四】创建Bean

4.1 创建Bean


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
45
46
47
48
49
50
1package com.feng.springbootsolr.pojo;
2
3import lombok.AllArgsConstructor;
4import lombok.Data;
5import lombok.NoArgsConstructor;
6import org.apache.solr.client.solrj.beans.Field;
7
8import java.io.Serializable;
9
10/**
11 * @Description 学生
12 * @Author fengwen
13 * @Date 2019/12/8 19:43
14 * @Version V1.0
15 */
16@Data
17@NoArgsConstructor
18@AllArgsConstructor
19public class Student implements Serializable {
20
21    private static final long serialVersionUID = -2221755230808487993L;
22
23    /**
24     * 学生id
25     */
26    @Field("id")
27    private String id;
28
29    /**
30     * 学生姓名
31     */
32    @Field("studentName")
33    private String studentName;
34
35    /**
36     * 学生年龄
37     */
38    @Field("studentAge")
39    private Integer studentAge;
40
41    /**
42     * 学生性别
43     */
44    @Field("studentGrander")
45    private Integer studentGrander;
46
47}
48
49
50

4.2 添加字段索引

其中@Field为solr服务器上创建的字段索引,需要去solr配置文件中添加字段索引


1
2
3
4
5
1到服务器上的solr的配置文件中进行设置:
2
3vim /usr/local/solr-4.10.3/example/solr/collection1/conf/schema.xml
4
5

配置文件中添加字段


1
2
3
4
5
6
7
8
1<!-- Student start -->
2 <field name="studentName" type="text_ik" indexed="true" stored="true" required="false" multiValued="false" />
3 <field name="studentAge" type="int" indexed="true" stored="true" required="false" multiValued="false" />
4 <field name="studentGrander" type="int" indexed="true" stored="true" required="false" multiValued="false" />
5<!-- student end -->
6
7
8

ps: 其中type="text_ik"意思是使用中文分割器

【五】service

5.1 StudentService


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
1package com.feng.springbootsolr.service;
2
3import com.feng.springbootsolr.pojo.Student;
4import java.util.List;
5
6/**
7 * @Description
8 * @Author fengwen
9 * @Date 2019/12/8 19:48
10 * @Version V1.0
11 */
12public interface StudentService {
13
14    /**
15     * 保存学生
16     * @param student
17     * @return
18     */
19    boolean saveStudent(Student student);
20
21    /**
22     * 根据条件查询学生
23     * @param param
24     * @param pagerNum
25     * @param pageSize
26     * @param sort
27     * @return
28     */
29    List<Student> queryStudent(String param,int pagerNum ,int pageSize,String sort);
30
31    /**
32     * 通过id查询学生
33     * @param id
34     * @return
35     */
36    Student queryById(String id);
37
38}
39
40
41

5.2 StudentServiceImpl


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
1package com.feng.springbootsolr.service.impl;
2
3import com.feng.springbootsolr.pojo.Student;
4import com.feng.springbootsolr.service.StudentService;
5import lombok.extern.slf4j.Slf4j;
6import org.apache.solr.client.solrj.SolrClient;
7import org.apache.solr.client.solrj.SolrQuery;
8import org.apache.solr.client.solrj.SolrServerException;
9import org.apache.solr.client.solrj.response.QueryResponse;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.stereotype.Service;
12
13import java.util.List;
14
15/**
16 * @Description
17 * @Author fengwen
18 * @Date 2019/12/8 19:49
19 * @Version V1.0
20 */
21@Service
22@Slf4j
23public class StudentServiceImpl implements StudentService {
24
25    /**
26     * 调用solr的api查询学生
27     */
28    @Autowired
29    private SolrClient solrClient;
30
31
32    /**
33     * 保存学生信息
34     *
35     * @param student
36     * @return
37     */
38    @Override
39    public boolean saveStudent(Student student) {
40        try {
41            solrClient.addBean(student);
42            solrClient.commit();
43        } catch (Exception e) {
44            e.printStackTrace();
45            log.error("保存学生信息出错:{}", student);
46            return false;
47        }
48        return true;
49    }
50
51    @Override
52    public List<Student> queryStudent(String param, int pagerNum, int pageSize, String sort) {
53        SolrQuery query = new SolrQuery();
54        query.set("q", param);
55        query.set("start", (pagerNum - 1) * pageSize);
56        query.set("rows", pageSize);
57        query.set("sort", sort);
58
59        List<Student> studentList = null;
60        try {
61            QueryResponse response = solrClient.query(query);
62            studentList = response.getBeans(Student.class);
63        } catch (Exception e) {
64            log.error("查询学生信息出错:{}、{}、{}、{}", param, pagerNum, pageSize, sort);
65        }
66        return studentList;
67    }
68
69    @Override
70    public Student queryById(String id) {
71        SolrQuery query = new SolrQuery();
72        String param = "id:" + id;
73        query.set("q", param);
74
75        List<Student> studentList = null;
76        try {
77            QueryResponse response = solrClient.query(query);
78            studentList = response.getBeans(Student.class);
79            if (studentList != null && studentList.size() > 0) {
80                return studentList.get(0);
81            }
82        } catch (Exception e) {
83            e.printStackTrace();
84            log.error("查询学生信息出错:{}", id);
85        }
86        return null;
87    }
88}
89
90
91

【六】controller


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
1package com.feng.springbootsolr.controller;
2
3import com.feng.springbootsolr.pojo.Student;
4import com.feng.springbootsolr.service.StudentService;
5import lombok.extern.slf4j.Slf4j;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.web.bind.annotation.GetMapping;
8import org.springframework.web.bind.annotation.PathVariable;
9import org.springframework.web.bind.annotation.PostMapping;
10import org.springframework.web.bind.annotation.RequestMapping;
11import org.springframework.web.bind.annotation.RequestParam;
12import org.springframework.web.bind.annotation.RestController;
13
14import java.util.List;
15
16/**
17 * @Description
18 * @Author fengwen
19 * @Date 2019/12/8 19:48
20 * @Version V1.0
21 */
22@RestController
23@RequestMapping("student")
24@Slf4j
25public class StudentController {
26
27    @Autowired
28    private StudentService studentService;
29
30    /**
31     * 保存学生
32     *
33     * @param student
34     * @return
35     */
36    @PostMapping("save")
37    public String saveStudent(Student student) {
38        boolean result = studentService.saveStudent(student);
39        if (!result) {
40            return "保存失败";
41        }
42        return "保存成功";
43    }
44
45    /**
46     * 通过id查询学生
47     *
48     * @param id
49     * @return
50     */
51    @GetMapping("{id}")
52    public Student getById(@PathVariable("id") String id) {
53        Student student = studentService.queryById(id);
54        return student;
55    }
56
57    /**
58     * 通过条件查询学生
59     *
60     * @param param
61     * @param pageNum
62     * @param pageSize
63     * @param sort
64     * @return
65     */
66    @GetMapping("query")
67    public List<Student> queryStudent( String param,
68                                      @RequestParam(required = false,defaultValue = "1") Integer pageNum,
69                                      @RequestParam(required = false,defaultValue = "5") Integer pageSize,
70                                      @RequestParam(required = false,defaultValue = "id desc") String sort) {
71        List<Student> studentList = studentService.queryStudent(param, pageNum, pageSize, sort);
72        return studentList;
73    }
74}
75
76
77

SpringBoot整合Solr
项目github地址:https://github.com/fengsri/springboot-solr

【七】链接

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++迭代器

2022-1-11 12:36:11

安全技术

Java电商秒杀系统性能优化(八)——流量削峰技术-削峰填谷之神级操作

2022-1-11 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索