ES与Spring Boot整合&性能优化

目录

 

1、前言

2、集成准备工作

2.1 依赖坐标

2.2 数据实体

2.3 数据交互接口

2.4 服务启动类

2.5 springboot配置文件

2.6 测试类

3、ES基本操作

3.1 索引操作

3.2 新增/更新文档

3.2 删除文档

3.3 查询文档

4、ES性能优化

4.1 批量提交

4.2 复杂组合查询

4.3 优化存储设备

4.4 加大tranlog flush间隔

4.5 增加index refresh间隔

4.5 锁定交换分区


1、前言

首先说明,springboot和elasticsearch的兼容性并不是很好,主要是因为elasticsearch的版本之间API差异太大。向后兼容性不强,特别是es6和es7,差异巨大!所以大家一定要注意自己服务器es的版本和springboot的依赖坐标版本对应,如我本地es版本为7,所以我导入的依赖坐标如下:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1eGlhbmcxOTg1MTExNA==,size_16,color_FFFFFF,t_70

es7后操作模板使用ElasticsearchRestTemplate,已经集成了高级API!

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1eGlhbmcxOTg1MTExNA==,size_16,color_FFFFFF,t_70

2、集成准备工作

2.1 依赖坐标

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.2 数据实体

package com.ydt.springboot.elasticsearch.domain;
​
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;
​
//@Document 文档对象 (索引信息、文档类型 )
/*Spring Data通过注解来声明字段的映射属性,有下面的三个注解:
​
        - `@Document` 作用在类,标记实体类为文档对象,一般有四个属性
        - indexName:对应索引库名称
        - type:对应在索引库中的类型   在ElasticSearch7.x中取消了type的概念
        - shards:分片数量,默认5
        - replicas:副本数量,默认1
        -
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值