ES安装和简单讲解
介绍
ES 通常指 Elasticsearch,它是一个基于 Lucene 的开源分布式搜索引擎,具有高可扩展性、高实时性和强大的全文检索能力,被广泛用于日志分析、全文检索、数据分析等场景。
- 全文检索支持对结构化、非结构化数据(如文本、日志、文档)进行快速的全文搜索,能处理模糊查询、关键词匹配、短语搜索等复杂检索需求
- 实时数据分析可实时存储和分析海量数据,结合 Kibana 使用
- 分布式架构天生支持分布式部署
- 近实时响应数据写入后可在秒级内被检索到,适合对实时性要求高的场景
安装
安装 es 服务
# 添加仓库秘钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
# 添加镜像源仓库
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elasticsearch.list # 更新软件包列表
sudo apt update # 安装es
sudo apt-get install elasticsearch=7.17.21 # 启动es
sudo systemctl start elasticsearch
# 安装ik分词器插件
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install
https://get.infini.cloud/elasticsearch/analysis-ik/7.17.21
提醒:中间可能会出现一些问题,因为每个机器会不一样,这里建议将相应的问题发给 AI 进行解答
sudo systemctl start elasticsearchsudo systemctl status elasticsearch.service//es默认监听9200端口,我们可以通过9200端口连接验证es是否安装成功curl -X GET "http://localhost:9200/"
设置外网访问
es 服务安装好后默认只能在本机进行访问
Shell
vim /etc/elasticsearch/elasticsearch.yml # 新增配置
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
安装 kibana
使用 apt 命令安装 Kibana。
sudo apt install kibana 配置 Kibana(可选):
根据需要配置 Kibana。配置文件通常位于 /etc/kibana/kibana.yml。可能需要
设置如服务器地址、端口、Elasticsearch URL 等。 sudo vim /etc/kibana/kibana.yml
例如,你可能需要设置 Elasticsearch 服务的 URL: 大概32行左右
elasticsearch.host: "http://localhost:9200"
启动 Kibana 服务:
安装完成后,启动 Kibana 服务。
sudo systemctl start kibana//设置开机启动,不太建议
sudo systemctl enable kibana //检查状态
sudo systemctl status kibana //浏览器访问本机的5601端口,就能进行访问了http://<your-ip>:5601
ES 核心概念
索引
索引是 Elasticsearch 中数据的集合,类似于关系型数据库中的 “数据库(Database)” 或 “表(Table)”,但更灵活。
- 一个索引包含多个具有相似结构的文档(Document)(类似数据库中的 “行”)。
- 每个索引有唯一的名称(小写字母,不含特殊字符),通过名称操作索引(如创建、查询、删除)。
PUT /books
{"settings": {"number_of_shards": 3, # 3个主分片"number_of_replicas": 1 # 每个主分片1个副本},"mappings": {"properties": {"title": { "type": "text" }, # 书名(文本类型,支持全文检索)"price": { "type": "float" }, # 价格(数值类型)"publish_date": { "type": "date" } # 出版日期(日期类型)}}
}
字段
字段是文档(Document)的基本组成单元,类似关系型数据库中 “列” 的概念,用于存储具体的数据值(如文本、数值、日期等)。
- 名称:字段的唯一标识(如
title
、price
),需符合命名规范(小写、无特殊字符)。 - 类型(Type):定义字段的数据类型,决定了 ES 如何存储、索引和解析该字段(关键属性)。
- 其他属性:如是否分词、是否可索引、是否存储原始值等,通过映射配置。
常见字段类型
类型分类 | 常见类型 | 用途说明 |
文本类型 |
| - - |
数值类型 |
、 | 存储整数、小数 |
日期类型 |
| 存储日期 / 时间,支持日期范围查询。 |
布尔类型 |
| 存储 |
复合类型 |
| - - |
特殊类型 |
| - |
ES 客户端安装
ES C++的客户端选择并不多, 我们这里使用elasticlient库, 下面进行安装
# 克隆代码
git clone https://github.com/seznam/elasticlient # 切换目录
cd elasticlient # 更新子模块
git submodule update --init --recursive
# 编译代码
mkdir build
cd build //我们需要先安装这个依赖
sudo apt-get install libmicrohttpd-devcmake .. //在安装之前我们需要安装一个依赖的库
cd ../external/googletest/
mkdir cmake && cd cmake/
cmake -DCMAKE_INSTALL_PREFIX=/usr ..
make && sudo make install
ES 客户端介绍
数据搜索接口:
cpr::Response search(const std::string &indexName, const std::string &docType, const std::string &body,const std::string &routing = std::string());//依次填入索引名称、文档类型、请求正文、最后一个一般用默认参数
数据获取接口:
cpr::Response get(const std::string &indexName, const std::string &docType, const std::string &id = std::string(), const std::string &routing = std::string());//依次填入索引名称、文档类型、想要获取数据的id、最后一个一般用默认参数
创建或更新数据:
cpr::Response index(const std::string &indexName, const std::string &docType, const std::string &id, const std::string &body, const std::string &routing = std::string()); //依次填入目标索引、文档类型、文档的id、文档内容、一般为默认值
移除数据
cpr::Response remove(const std::string &indexName, const std::string &docType, const std::string &id, const std::string &routing = std::string());
//依次填入目标索引、文档类型、文档的id、一般为默认值