ElasticSearch 学习笔记总结(四)

news2024/9/27 15:29:21

文章目录

  • 一、ES继承 Spring Data 框架
  • 二、SpringData 功能集成
  • 三、ES SpringData 文档搜索
  • 四、ES 优化 硬件选择
  • 五、ES 优化 分片策略
  • 六、ES 优化 路由选择
  • 七、ES 优化 写入速度优化
  • 七、ES 优化 内存设置
  • 八、ES 优化 重要配置

一、ES继承 Spring Data 框架

Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问。
在这里插入图片描述

Spring Data的官方:https://spring.io/projects/spring-data

其实Spring Data框架的出现,是为了更好快速的操作ES服务器,简化ES的操作。

在这里插入图片描述

自然操作的就是Spring Data Elasticsearch对应的内容。

二、SpringData 功能集成

创建一个springboot项目。

第一步:项目依赖,配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.6.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.itholmes</groupId>
    <artifactId>es-spring</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>

</project>

第二步:配置application.properties文件。

# es服务地址 为了项目引用
elasticsearch.host=127.0.0.1
# es服务端口
elasticsearch.port=9200
# 配置日志级别,开启debug日志
logging.level.com.itholmes.es=debug

第三步:创建SpringBoot主程序。

package com.itholmes.es;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDataElasticSearchMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringDataElasticSearchMainApplication.class,args);
    }
}

第四步:创建实体类,通过实体类来作为数据进行相关操作。

package com.itholmes.es;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
    private Long id; // 商品唯一标识
    private String title; // 商品名称
    private String category; // 分类名称
    private Double price; // 商品价格
    private String images; // 图片地址
}

第五步:创建ElasticsearchConfig类,对应的配置文件。

package com.itholmes.es;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

// @ConfigurationProperties(prefix = "elasticsearch") 去匹配 elasticsearch.host 和 elasticsearch.port。
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    private String host;
    private Integer port;
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}

第六步:配置 DAO数据 访问对象,获取数据。

package com.itholmes.es;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

// ElasticsearchRepository<Product,Long> 来进行操作
@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {

}

第七步:配置 实体类映射操作。

package com.itholmes.es;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
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;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
// 关联索引,分片,备份
@Document(indexName = "product",shards = 3,replicas = 1)
public class Product {
    /**
     * 必须有id,这里的id是全局唯一的标识,等同于es中的_id。
     */
    @Id
    private Long id; // 商品唯一标识
    /**
     * type: 字段数据类型
     * analyzer:分词器类型
     * index: 是否索引(默认为:true)
     * Keyword:短语,不进行分词 就是关键字不能分开
     */
    @Field(type = FieldType.Text) // ,analyzer = "ik_max_word"
    private String title; // 商品名称
    @Field(type = FieldType.Keyword)
    private String category; // 分类名称
    @Field(type = FieldType.Double)
    private Double price; // 商品价格
    @Field(type = FieldType.Keyword,index = false) // index = false 就是不做索引查询的
    private String images; // 图片地址
}

第八步:做一个测试,简单走个测试就会把对应索引创建出来(初始化创建)。

package com.itholmes.es;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESProductDaoTest {

    // 注入ElasticsearchRestTemplate
    @Autowired
    private ProductDao productDao;

    // 新增数据
    @Test
    public void save(){
        // 其实就是把后台相关数据,存储到了ES中。
        Product product = new Product();
        product.setId(2L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.itholmes/hw.jpg");
        productDao.save(product);
        // 查看:get方法 http://127.0.0.1:9200/product/_doc/2
    }

    // 修改数据
    @Test
    public void update(){
        Product product = new Product();
        product.setId(2L); // id相同就是修改数据
        product.setTitle("华为222手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.itholmes/hw.jpg");
        productDao.save(product);
        //查看:get方法 http://127.0.0.1:9200/product/_doc/2
    }

    // 根据id查询
    @Test
    public void findById(){
        Product product = productDao.findById(2L).get();
        System.out.println(product);
    }

    // 查询所有
    @Test
    public void findAll(){
        Iterable<Product> all = productDao.findAll();
        for (Product product : all) {
            System.out.println(product);
        }
    }

    // 删除
    @Test
    public void delete(){
        Product product = new Product();
        product.setId(2L);
        productDao.delete(product);
    }

    // 批量新增
    @Test
    public void saveAll(){
        ArrayList<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i));
            product.setTitle("[" + i + "]" + "小米手机");
            product.setCategory("手机");
            product.setPrice(1999.0 + i);
            product.setImages("http://itholems.com" + i);
            productList.add(product);
        }
        productDao.saveAll(productList);
    }

    // 分页查询
    @Test
    public void findByPageable(){
        // 设置排序(排序方式,正序还是倒序,排序的id)
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        int currentPage = 0; // 当前页 第一页从0开始,1表示第二页
        int pageSize = 5; // 每页显示多少条
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
        Page<Product> productPage = productDao.findAll(pageRequest);
        for (Product product : productPage) {
            System.out.println(product);
        }
    }

}

在这里插入图片描述

三、ES SpringData 文档搜索

SpringData 文档搜索

package com.itholmes.es;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESSearchTest {

    @Autowired
    ProductDao productDao;

    // 文档搜索
    @Test
    public void termQuery(){
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Iterable<Product> products = productDao.search(termQueryBuilder);
        for (Product product : products) {
            System.out.println(product);
        }
    }

    // 分页请求
    @Test
    public void termQueryByPage(){
        int currentPage = 0;
        int pageSize = 5;
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Page<Product> products = productDao.search(termQueryBuilder, pageRequest);
        for (Product product : products) {
            System.out.println(product);
        }
    }

}

此外还有,Spark Streaming框架 集成、Flink框架集成等等,先将数据经过它们处理,之后存储到ES服务器中。

四、ES 优化 硬件选择

Elasticsearch的基础是 Lucene,所有的索引和文档数据是存储在本地的磁盘中,具体路径可在ES的配置文件 …/config/elasticsearch.yml中配置。

SSD是 固态硬盘。

硬件选择推荐如下:
在这里插入图片描述

五、ES 优化 分片策略

分片 和 副本并不是无限分配的。

在这里插入图片描述

分片的代价:
在这里插入图片描述
在这里插入图片描述

具体要根据架构师技术人员进行确认:
在这里插入图片描述

遵循的原则:
在这里插入图片描述


推迟分片分配:
在这里插入图片描述

六、ES 优化 路由选择

存放规则:
在这里插入图片描述

查询的时候有两种情况:不带routing路由查询、待routing路由查询。
在这里插入图片描述

七、ES 优化 写入速度优化

针对搜索性能要求不高,但是对于写入要求较高的场景,我们需要尽可能的选择恰当写优化策略。
在这里插入图片描述

批量操作:
在这里插入图片描述
优化存储设备:
在这里插入图片描述

合理的使用合并:(将 段 合并 )
在这里插入图片描述
减少Refresh次数:
在这里插入图片描述
加大Flush设置:
在这里插入图片描述
减少副本的数量:
在这里插入图片描述

七、ES 优化 内存设置

ES默认安装后设置的内存是1GB ,对于一个现实业务来说设个设置太小了。

不过,可以配置一下。

jvm.options文件进行配置:
在这里插入图片描述

配置内存的原则:
在这里插入图片描述
像上面那个64g的 最佳配置就是设置为31G。 -Xms 31g -Xmx 31g。

八、ES 优化 重要配置

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/382030.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【案例教程】拉格朗日粒子扩散模式FLEXPART

拉格朗日粒子扩散模式FLEXPART通过计算点、线、面或体积源释放的大量粒子的轨迹&#xff0c;来描述示踪物在大气中长距离、中尺度的传输、扩散、干湿沉降和辐射衰减等过程。该模式既可以通过时间的前向运算来模拟示踪物由源区向周围的扩散&#xff0c;也可以通过后向运算来确定…

CKKS自举笔记(CKKS Bootstrapping)

文章目录CKKS Bootstrapping流程流程的框架如何做同态取模操作直接泰勒展开&#xff08;naive idea&#xff09;采用二倍角公式来拟合&#xff08;欧密2018&#xff09;如何做同态编码或解码CKKS的编码和解码基础知识&#xff08;明文下面怎么做&#xff09;同态的旋转、共轭&a…

Linux 进程:进程控制

目录一、进程创建1.fork2.vfork二、进程终止三、进程等待四、进程替换1.理解程序替换2.子进程在程序替换中的作用Linux的进程控制分为四部分&#xff1a; 进程创建进程终止进程等待进程替换 一、进程创建 常见的创建进程的函数有两个&#xff1a; pid_t fork(void)pid_t vf…

一篇文章帮助你初步了解CDN内容分发网络

文章目录CDN内容分发网络CDN内容分发网络的工作原理CDN的作用CDN如何实现内容的加速CDN内容分发网络 CDN&#xff08;Content Delivery Network&#xff09;内容分发网络。CDN 是构建在现有网络基础之上的智能虚拟网络&#xff0c;依靠部署在各地的边缘服务器&#xff0c;通过…

手撕CSDN博文:学用curl命令获取博文页面源码,学不会爬虫先手剥CSDN博文阅读点赞收藏和评论数量

学用curl命令获取博文页面源码&#xff0c;学不会爬虫先手剥CSDN博文阅读点赞收藏和评论数量。 (本文获得CSDN质量评分【xx】)【学习的细节是欢悦的历程】Python 官网&#xff1a;https://www.python.org/ Free&#xff1a;大咖免费“圣经”教程《 python 完全自学教程》&…

客户服务软件推荐榜:28款!

在这个竞争激烈的时代&#xff0c;做到服务对企业的存亡有着深刻的意义。改善客户服务&#xff0c;做好客户服务工作&#xff0c;是关键&#xff0c;因为客户服务团队代表着企业的形象&#xff0c;面孔&#xff0c;客户有可能 不大会记得企业的某个东西&#xff0c;但是他们将会…

module java.base does not “opens java.xxx“ to unnamed module @xxxx

错误截图 在springboot集成dubbo中 消费者服务和生产者复核都报错 错误原因 高版本JDK禁止了报错所提示的几个包的反射 而dubbo里用到了 解决 看自己的报错里有几个包被禁止了 我这有两个java.math和java.lang 添加两个JVM启动参数 –add-opens java.base/java.mathALL…

Python爬虫之Scrapy框架爬虫实战

Python爬虫中Scrapy框架应用非常广泛&#xff0c;经常被人用于属于挖掘、检测以及自动化测试类项目&#xff0c;为啥说Scrapy框架作为半成品我们又该如何利用好呢 &#xff1f;下面的实战案例值得大家看看。 目录&#xff1a; 1、Scrapy框架之命令行 2、项目实现 Scrapy框架…

安卓手机当旁路网关

一、安卓shell调试工具下载【电脑版下载地址】安卓adb调试工具&#xff0c;包含MAC苹果、Windows和Linux 三种版【手机版下载地址Termux】下载地址&#xff1a;https://github.com/termux/termux-app/releases如果不懂下载哪个版本&#xff0c;可以直接下载通用版&#xff1a;t…

Jackson CVE-2017-7525 反序列化漏洞

0x00 前言 Jackson 相对应fastjson来说利用方面要求更加苛刻&#xff0c;默认情况下无法进行利用。 同样本次的调用链也可以参考fastjson内容&#xff1a;Java代码审计——Fastjson TemplatesImpl调用链 相关原理&#xff0c;可以参考&#xff1a;Jackson 反序列化漏洞原理 …

基于Java+SpringBoot+Vue+Uniapp(有教程)前后端分离健身预约系统设计与实现

博主介绍&#xff1a;✌全网粉丝3W&#xff0c;全栈开发工程师&#xff0c;从事多年软件开发&#xff0c;在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战✌ 博主作品&#xff1a;《微服务实战》专栏是本人的实战经验总结&#xff0c;《Spring家族及…

6款yyds的可视化搭建开源项目

之前我一直在研究低代码可视化相关的技术和产品, 也主导过很多可视化搭建项目, 主要目的是降低企业研发成本和缩短产品交付周期, 随着互联网技术的发展也陆陆续续有很多优秀的技术产品问世, 接下来我就和大家分享几款非常有价值的可视化搭建项目, 助力企业数字化转型. 1. Form…

轻松搞懂Linux中的用户管理

文章目录概念用户账户用户组用户权限用户管理工具概念 用户管理是Linux系统管理员必须掌握的重要技能之一。Linux系统是一个多用户操作系统&#xff0c;可以支持多个用户同时使用&#xff0c;每个用户拥有自己的账户和权限&#xff0c;因此管理员需要了解如何创建、管理和删除…

当参数调优无法解决kafka消息积压时可以这么做

今天的议题是&#xff1a;如何快速处理kafka的消息积压 通常的做法有以下几种&#xff1a; 增加消费者数增加 topic 的分区数&#xff0c;从而进一步增加消费者数调整消费者参数&#xff0c;如max.poll.records增加硬件资源 常规手段不是本文的讨论重点或者当上面的手段已经使…

vue 在install时候node-sass@4.14.1 postinstall: node scripts/build.js错误

今天重装了node和Vue脚手架&#xff0c;在install的时候报了下面的错误 报错如下&#xff1a; Build failed with error code: 1 [npminstall:runscript:error] node-sass^4.14.1 run postinstall node scripts/build.js error: Error: Command failed with exit code 1: node…

Allegro如何输出钻孔表操作指导

Allegro如何输出钻孔表操作指导 用Allegro做PCB设计的时候,需要输出钻孔表格,用于生产加工,如下图 如何输出钻孔表,具体操作如下 点击Manufacture点击NC

面试问题【集合】

集合常见的集合有哪些List、Set、Map 的区别ArrayList 和 Vector 的扩容机制Collection 和 Collections 有什么区别ArrayList 和 LinkedList 的区别是什么ArrayList 和 Vector 的区别是什么ArrayList 和 Array 有何区别ArrayList 集合加入1万条数据&#xff0c;应该怎么提高效率…

全面了解 B 端产品设计 — 基础扫盲篇

在今天,互联网的影响力与作用与日俱增,除了我们日常生活领域的改变以外,对于商业领域的渗透也见效颇丰。 越来越多的企业开始使用数字化的解决方案来助力企业发展,包括日常管理、运营、统计等等。或者通过互联网的方式开发出新的业务形态,进行产业升级,如这几年风头正劲的…

WMS相关知识点

目录一、WMS简介二、窗口的分类三、添加Window一、WMS简介 Window&#xff1a;在Android视图体系中Window就是一个窗口的概念。Android中所有的视图都是依赖于Window显示的。 Window是一个抽象的概念&#xff0c;它对应屏幕上的一块显示区域&#xff0c;它不是实实在在的内容&…

大学生实践| 微软ATP“师徒制”AI实战项目收获满满!

ChatGPT在极短时间内掀起了一轮AI狂潮&#xff0c;AI数据、AI大模型、AIGC……对我们AI实践项目感兴趣的同学也越来越多&#xff01;微软(亚洲)互联网工程院下属的微软ATP为大学生们提供了丰富的企业级实践项目。2个月内&#xff01;本期优秀的Chen同学在微软AI工程师团队带领下…