当前位置: 首页 > news >正文

ElasticSearch 学习笔记总结(四)

文章目录

  • 一、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;@Overridepublic 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。*/@Idprivate 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@Autowiredprivate ProductDao productDao;// 新增数据@Testpublic 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}// 修改数据@Testpublic 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查询@Testpublic void findById(){Product product = productDao.findById(2L).get();System.out.println(product);}// 查询所有@Testpublic void findAll(){Iterable<Product> all = productDao.findAll();for (Product product : all) {System.out.println(product);}}// 删除@Testpublic void delete(){Product product = new Product();product.setId(2L);productDao.delete(product);}// 批量新增@Testpublic 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);}// 分页查询@Testpublic 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 {@AutowiredProductDao productDao;// 文档搜索@Testpublic void termQuery(){TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");Iterable<Product> products = productDao.search(termQueryBuilder);for (Product product : products) {System.out.println(product);}}// 分页请求@Testpublic 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 优化 重要配置

在这里插入图片描述

相关文章:

ElasticSearch 学习笔记总结(四)

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

HDFS文件块大小

HDFS中的文件在物理上是分块存储&#xff08;Block&#xff09;&#xff0c;块的大小可以通过配置参数&#xff08;dfs.blocksize&#xff09;来规定&#xff0c;默认大小在Hadooop2X版本中是128M&#xff0c;老版本中是64M。 思考&#xff1a;为什么块的大小不能设置太小&…...

C++——优先级队列(priority_queue)的使用及实现

目录 一.priority_queue的使用 1.1、基本介绍 1.2、优先级队列的定义 1.3、基本操作(常见接口的使用&#xff09; 1.4、重写仿函数支持自定义数据类型 二.priority_queue的模拟实现 2.1、构造&&重要的调整算法 2.2、常见接口的实现 push() pop() top() empt…...

Linux学习记录——십일 环境变量

文章目录1、认识2、通过代码获取环境变量1、手动获取2、函数获取3、重新认识环境变量1、认识 在云服务器上写程序时&#xff0c;最终的执行需要./文件名&#xff0c;点表示当前目录&#xff0c;/是文件分隔符&#xff0c;之后就会打印程序&#xff0c;这是用户的操作&#xff…...

【人工智能 Open AI 】我们程序员真的要下岗了- 全能写Go / C / Java / C++ / Python / JS 人工智能机器人

文章目录[toc]人工智能 AI Code 写代码测试用golang实现冒泡排序用golang实现计算环比函数goroutine and channel用golang实现二叉树遍历代码用golang实现线程安全的HashMap操作代码using C programming language write a tiny Operation Systemuse C language write a tiny co…...

STM32 EXTI外部中断

本文代码使用 HAL 库。 文章目录前言一、什么是外部中断&#xff1f;二、外部中断中断线三、STM32F103的引脚复用四、相关函数&#xff1a;总结前言 一、什么是外部中断&#xff1f; 外部中断 是单片机实时地处理外部事件的一种内部机制。当某种外部事件发生时&#xff0c;单片…...

Mapper代理开发——书接MaBatis的简单使用

在这个mybatis的普通使用中依旧存在硬编码问题,虽然静态语句比原生jdbc都写更少了但是还是要写&#xff0c;Mapper就是用来解决原生方式中的硬编码还有简化后期执行SQL UserMapper是一个接口&#xff0c;里面有很多方法&#xff0c;都是一一和配置文件里面的sql语句的id名称所对…...

实体对象说明

1.工具类层Utilutil 工具顾明思义&#xff0c;util层就是存放工具类的地方&#xff0c;对于一些独立性很高的小功能&#xff0c;或重复性很高的代码片段&#xff0c;可以提取出来放到Util层中。2.数据层POJO对象&#xff08;概念比较大&#xff09; 包含了以下POJO plain ord…...

JAVA中加密与解密

BASE64加密/解密 Base64 编码会将字符串编码得到一个含有 A-Za-z0-9/ 的字符串。标准的 Base64 并不适合直接放在URL里传输&#xff0c;因为URL编码器会把标准 Base64 中的“/”和“”字符变为形如 “%XX” 的形式&#xff0c;而这些 “%” 号在存入数据库时还需要再进行转换&…...

改进YOLO系列 | ICLR2022 | OMNI-DIMENSIONAL DYNAMIC CONVOLUTION: 全维动态卷积

单个静态卷积核是现代卷积神经网络(CNNs)的常见训练范式。然而,最近的动态卷积研究表明,学习加权为其输入依赖注意力的n个卷积核的线性组合可以显著提高轻量级CNNs的准确性,同时保持高效的推理。然而,我们观察到现有的作品通过卷积核空间的一个维度(关于卷积核数量)赋予…...

信息收集之Github搜索语法

信息收集之Github搜索语法1.Github的搜索语法2.使用 Github 进行邮件配置信息收集3.使用Github进行数据库信息收集4.使用Github进行 SVN 信息收集5.使用Github进行综合信息收集在测试的信息收集阶段&#xff0c;可以去Github和码云上搜索与目标有关的信息&#xff0c;或者就有意…...

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

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

试题 算法训练 自行车停放

问题描述 有n辆自行车依次来到停车棚&#xff0c;除了第一辆自行车外&#xff0c;每辆自行车都会恰好停放在已经在停车棚里的某辆自行车的左边或右边。(e.g.停车棚里已经有3辆自行车&#xff0c;从左到右编号为&#xff1a;3,5,1。现在编号为2的第4辆自行车要停在5号自行车的左…...

泛型与Map接口

Java学习之道 泛型 泛型这种参数类型可以用在类、方法和接口中&#xff0c;分别被称为泛型类&#xff0c;泛型方法&#xff0c;泛型接口 参数化类型&#xff1a;将类型由原来的具体的类型参数化&#xff0c;在使用/调用时传入具体的类型JDK5引入特性提供了安全检测机制&#xf…...

Unity Bug记录本

//个人记录&#xff0c;持续更新 1、将此代码挂载到空脚本上&#xff1a; bool flag (object)GetComponent<Camera>() null; bool flag1 (object)GetComponent<Text>() null; Debug.Log(flag"::"flag1); //输出结果&#xff1a;False::True bool…...

B. The Number of Products)厉害

You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn non-zero integers (i.e. ai≠0ai≠0). You have to calculate two following values: the number of pairs of indices (l,r)(l,r) (l≤r)(l≤r) such that al⋅al1…ar−1⋅aral⋅al1…ar−1⋅ar is neg…...

一起Talk Android吧(第五百一十二回:自定义Dialog)

文章目录整体思路实现方法第一步第二步第三步第四步各位看官们大家好&#xff0c;上一回中咱们说的例子是"自定义Dialog主题",这一回中咱们说的例子是" 自定义Dialog"。闲话休提&#xff0c;言归正转&#xff0c; 让我们一起Talk Android吧&#xff01;整体…...

GinVueAdmin源码分析3-整合MySQL

目录文件结构数据库准备配置文件处理config.godb_list.gogorm_mysql.gosystem.go初始化数据库gorm.gogorm_mysql.go开始初始化测试数据库定义实体类 Userserviceapi开始测试&#xff01;文件结构 本文章将使用到上一节创建的 CommonService 接口&#xff0c;用于测试连接数据库…...

大数据框架之Hadoop:MapReduce(三)MapReduce框架原理——MapReduce开发总结

在编写MapReduce程序时&#xff0c;需要考虑如下几个方面&#xff1a; 1、输入数据接口&#xff1a;InputFormat 默认使用的实现类是&#xff1a;TextInputFormatTextInputFormat的功能逻辑是&#xff1a;一次读一行文本&#xff0c;然后将该行的起始偏移量作为key&#xff0…...

requests---(4)发送post请求完成登录

前段时间写过一个通过cookies完成登录&#xff0c;今天我们写一篇通过post发送请求完成登录豆瓣网 模拟登录 1、首先找到豆瓣网的登录接口 打开豆瓣网站的登录接口&#xff0c;请求错误的账号密码&#xff0c;通过F12或者抓包工具找到登录接口 通过F12抓包获取到请求登录接口…...

PHP和Node.js哪个更爽?

先说结论&#xff0c;rust完胜。 php&#xff1a;laravel&#xff0c;swoole&#xff0c;webman&#xff0c;最开始在苏宁的时候写了几年php&#xff0c;当时觉得php真的是世界上最好的语言&#xff0c;因为当初活在舒适圈里&#xff0c;不愿意跳出来&#xff0c;就好比当初活在…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

【AI学习】三、AI算法中的向量

在人工智能&#xff08;AI&#xff09;算法中&#xff0c;向量&#xff08;Vector&#xff09;是一种将现实世界中的数据&#xff08;如图像、文本、音频等&#xff09;转化为计算机可处理的数值型特征表示的工具。它是连接人类认知&#xff08;如语义、视觉特征&#xff09;与…...

爬虫基础学习day2

# 爬虫设计领域 工商&#xff1a;企查查、天眼查短视频&#xff1a;抖音、快手、西瓜 ---> 飞瓜电商&#xff1a;京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空&#xff1a;抓取所有航空公司价格 ---> 去哪儿自媒体&#xff1a;采集自媒体数据进…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

动态 Web 开发技术入门篇

一、HTTP 协议核心 1.1 HTTP 基础 协议全称 &#xff1a;HyperText Transfer Protocol&#xff08;超文本传输协议&#xff09; 默认端口 &#xff1a;HTTP 使用 80 端口&#xff0c;HTTPS 使用 443 端口。 请求方法 &#xff1a; GET &#xff1a;用于获取资源&#xff0c;…...

【JavaSE】多线程基础学习笔记

多线程基础 -线程相关概念 程序&#xff08;Program&#xff09; 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序&#xff0c;比如我们使用QQ&#xff0c;就启动了一个进程&#xff0c;操作系统就会为该进程分配内存…...

windows系统MySQL安装文档

概览&#xff1a;本文讨论了MySQL的安装、使用过程中涉及的解压、配置、初始化、注册服务、启动、修改密码、登录、退出以及卸载等相关内容&#xff0c;为学习者提供全面的操作指导。关键要点包括&#xff1a; 解压 &#xff1a;下载完成后解压压缩包&#xff0c;得到MySQL 8.…...

HTML前端开发:JavaScript 获取元素方法详解

作为前端开发者&#xff0c;高效获取 DOM 元素是必备技能。以下是 JS 中核心的获取元素方法&#xff0c;分为两大系列&#xff1a; 一、getElementBy... 系列 传统方法&#xff0c;直接通过 DOM 接口访问&#xff0c;返回动态集合&#xff08;元素变化会实时更新&#xff09;。…...

Linux 下 DMA 内存映射浅析

序 系统 I/O 设备驱动程序通常调用其特定子系统的接口为 DMA 分配内存&#xff0c;但最终会调到 DMA 子系统的dma_alloc_coherent()/dma_alloc_attrs() 等接口。 关于 dma_alloc_coherent 接口详细的代码讲解、调用流程&#xff0c;可以参考这篇文章&#xff0c;我觉得写的非常…...