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 如果使用,要下载对应版本,不然用不了。

 

祝大家工作顺利,生活如意!

 

原文地址:http://www.cnblogs.com/isyysblog/p/16921086.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性