关于版本 项目、 es、 中文分词器 Logstash 都要贴合
可以直接使用 阿里云提供的 ES
image.png

引入依赖

在 SpringBoot2.2.x 版本才支持 ElasticSearch7.x

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. <version>2.3.11.RELEASE</version>
  5. </dependency>

配置 yml

旧版本

spring:
  data:
    elasticsearch:
      cluster-name: es6
      cluster-nodes: 192.168.1.187:9300

新版本 x-pack

spring.elasticsearch.rest.uris = 172.31.0.207:9300
spring.elasticsearch.rest.username = elastic
spring.elasticsearch.rest.password = changeme

原来 spring-boot-starter-data-elasticsearch 连接 ES 主要使用 ElasticsearchTemplate 进行操作,新版本主要使用 ElasticsearchRestTemplate

pojo 类

@Document(indexName = "produts", type = "doc", createIndex = false)
public class Items {
    @Id
    @Field(store = true, type = FieldType.Text, index = flase)
    private String itemId;

    @Field(store = true, type = FieldType.Text, index = true)
    private String itemName;

    @Field(store = true, type = FieldType.Integer)
    private Integer price;
}

elasticsearch 的分页默认从 0 开始,page 传过来要 —

报错

Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]

@Configuration
public class ESConfig {

    /**
     * 解决netty引起的issue
     */
    @PostConstruct
    void init() {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }

}