springboot整合Elasticsearch
springboot 2.2.5.RELEASE
Elasticsearch 7.17.6
Elasticsearch下载地址https://www.elastic.co/cn/downloads/elasticsearch
还有个大概的对应版本图:
如果要使用IK分词器的话,也有版本对应关系:
先准备好环境,然后开始第一步:
一: 解压Elasticsearch,bin\elasticsearch.bat 启动。
二:pom 添加 Elasticsearch jar,版本让它自己控制,我这里没有指定。
<!-- 整合Elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
三:新建student 实体类(没有装IK分词器的时候,不要指定ik_smart,ik_max_word等等,不然报错。)
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; import java.io.Serializable; /** elasticsearch demo * @date 20221123 11:13 */ @Data @AllArgsConstructor @NoArgsConstructor //indexName名字如果是字母那么必须是小写字母 @Document(indexName = "student") public class Student implements Serializable { @Id @Field(store = true, type = FieldType.Keyword) private Integer id; @Field(store = true, type = FieldType.Keyword) private String name; @Field(store = true, type = FieldType.Text) //Text可以分词 ik_smart=粗粒度分词 ik_max_word 为细粒度分词 private String address; @Field(index = false, store = true, type = FieldType.Integer) private Integer age; @Field(index = false, store = true, type = FieldType.Keyword) private String createTime; }
第四步:创建接口类 StudentMapper
import ideal4j.pfa.resume.model.Student; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * @ClassName: StudentMapper * @Description: elasticsearch demo * @Date 2022/11/23 * @Version 1.0 */ public interface StudentMapper extends ElasticsearchRepository<Student, Integer> { }
第五步:创建action 类
import com.alibaba.fastjson.JSON; import ideal4j.pfa.resume.mapper.StudentMapper; import ideal4j.pfa.resume.model.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; /** * @ClassName: TestAction * @Description: * @Date 2022/11/23 13:59 */ @RestController public class TestAction { @Autowired public StudentMapper studentMapper; @PostMapping("/elasticsearch/test") public Student test(@RequestBody Student student) { return studentMapper.save(student); } @GetMapping("/elasticsearch/student") public Student get(@RequestParam Integer id) { Optional<Student> student = studentMapper.findById(id); System.out.println(JSON.toJSONString(student)); return student.get(); } }
第六步:postman展示测试成果
问题一:Elasticsearch启动控制台乱码,参考:https://www.cnblogs.com/isyysblog/p/16917608.html
问题二:Elasticsearch版本不对应,sava 一直报错,查询也查不到,刚开始用的5.5.3。
问题三:刚开始可以不安装IK分词器,事儿多。
问题四:kibana 如果使用,要下载对应版本,不然用不了。
祝大家工作顺利,生活如意!