elasticsearch7.9整合springboot
1、需要添加依赖
2、添加配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.example.elasticsearch.shop.configure; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; @Configuration class RestClientConfig extends AbstractElasticsearchConfiguration { @Bean @Override public RestHighLevelClient elasticsearchClient() { final ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo( "10.138.130.1:30217" , "10.138.130.1:32002" , "10.138.130.1:33785" ) .build(); return RestClients.create(clientConfiguration).rest(); } @Bean (name = { "elasticsearchOperations" , "elasticsearchRestTemplate" }) public ElasticsearchRestTemplate elasticsearchTemplate() { return new ElasticsearchRestTemplate(elasticsearchClient()); } } |
3、创建索引
1 2 3 | public Boolean createIndex() { return elasticsearchRestTemplate.indexOps(ShopEntity. class ).create(); } |
4、创建mapping
1 2 3 4 | Document mapping = elasticsearchRestTemplate.indexOps(ShopEntity. class ).createMapping(ShopEntity. class ); boolean brue = elasticsearchRestTemplate.indexOps(ShopEntity. class ).putMapping(mapping); System.out.println(brue); System.out.println(mapping); |
docker部署elasticsearch环境变量设置
cluster.name: "elasticsearch-cluster" network.host: 0.0.0.0 node.name: elasticsearch-server-01 node.master: true node.data: true discovery.seed_hosts: ["elasticsearch-server-01", "elasticsearch-server-02", "elasticsearch-server-03"] cluster.initial_master_nodes: ["elasticsearch-server-01", "elasticsearch-server-02", "elasticsearch-server-03"]
docker-compose部署es集群
version: '2.2' services: es01: image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2 container_name: es01 environment: - node.name=es01 - cluster.name=es-docker-cluster - discovery.seed_hosts=es02,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ports: - 9200:9200 networks: - elastic es02: image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2 container_name: es02 environment: - node.name=es02 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ports: - 9201:9200 networks: - elastic es03: image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2 container_name: es03 environment: - node.name=es03 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es02 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ports: - 9202:9200 networks: - elastic networks: elastic: driver: bridge
使用 Docker 环境变量设置单独的 Elasticsearch 配置参数
- 将设置名称更改为大写
- 前缀为
ES_SETTING_
_
通过复制 下划线 ( ) 来转义它们- 将所有句点 (
.
)转换为下划线 (_
)
例如,-e bootstrap.memory_lock=true
变成 -e ES_SETTING_BOOTSTRAP_MEMORY__LOCK=true
。
挂载 Elasticsearch 配置文件
- v full_path_to / custom_elasticsearch 。yml :/ usr / share / elasticsearch / config / elasticsearch 。yml
如果您绑定挂载自定义elasticsearch.yml
文件,请确保它包含该 network.host: 0.0.0.0
设置。此设置可确保节点可访问 HTTP 和传输流量,前提是其端口已公开。elasticsearch.yml
默认情况下,Docker 映像的内置文件包含此设置。
elasticsearch类型为啥去掉?
elasticsearch强掉的是全文检索,所以type没有意义了
elasticsearch倒排索引理解
1001 zhangsan
1002 lisi
1003 wagnwu zhang
倒排索引表
zhang 1001 1003
san 1001
li 1002
si 1002
9300和9200端口区别
9300是es集群之间通信端口,9200是与客户端通信端口
集群配置
config-elasticsearch.yml配置
cluster配置
cluster.name:es
node配置
node.name:node01
node.master:true
node.data:true
network配置
network.host:localhost
http.port:1001
transport.tcp.port:9301
discovery.send_hosts:["localhost:9301"] //集群第一台不需要配置,第二台需要配置,查找另外的一台es
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
http.cors.enabled:true
http.cors.allow-origin:"*"
挂载数据卷:/usr/share/elasticsearch/data
分片的理解
数据分别存放到不同 的分片上,所有的分片数据组成完整的数据,分片是最小的单元
学习参考视频:https://www.bilibili.com/video/BV1hh411D7sb?p=62&spm_id_from=pageDriver
错误问题解决:
命令行执行:
wsl -d docker-desktop
sysctl -w vm.max_map_count=262144

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?