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

ES安装和简单讲解

介绍

ES 通常指 Elasticsearch,它是一个基于 Lucene 的开源分布式搜索引擎,具有高可扩展性、高实时性和强大的全文检索能力,被广泛用于日志分析、全文检索、数据分析等场景。

  1. 全文检索支持对结构化、非结构化数据(如文本、日志、文档)进行快速的全文搜索,能处理模糊查询、关键词匹配、短语搜索等复杂检索需求
  2. 实时数据分析可实时存储和分析海量数据,结合 Kibana 使用
  3. 分布式架构天生支持分布式部署
  4. 近实时响应数据写入后可在秒级内被检索到,适合对实时性要求高的场景

安装

安装 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)的基本组成单元,类似关系型数据库中 “列” 的概念,用于存储具体的数据值(如文本、数值、日期等)。

  • 名称:字段的唯一标识(如 titleprice),需符合命名规范(小写、无特殊字符)。
  • 类型(Type):定义字段的数据类型,决定了 ES 如何存储、索引和解析该字段(关键属性)。
  • 其他属性:如是否分词、是否可索引、是否存储原始值等,通过映射配置。

常见字段类型

类型分类

常见类型

用途说明

文本类型

textkeyword

- text:用于全文检索(会被分词”)。

- keyword:用于精确匹配(不分词)。

数值类型

integerlongfloat

double

存储整数、小数

日期类型

date

存储日期 / 时间,支持日期范围查询。

布尔类型

boolean

存储 truefalse,用于逻辑判断。

复合类型

objectnested

- object:存储嵌套 JSON 对象。

- nested:存储嵌套数组。

特殊类型

ip

- ip:存储 IP 地址,支持 IP 范围查询。

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、一般为默认值
http://www.dtcms.com/a/511962.html

相关文章:

  • Microtest的整套承包系统(turnkey system)目标电源设备特性
  • 程序员学习大模型必备:2025年“人工智能+“行业标杆案例荟萃(附下载)
  • 山西做网站的公司有哪些网站开发做前端还是后端
  • Ubuntu部署redis
  • 国内高端医疗网站建设网站搜索引擎优化诊断
  • 一次完整的 HTTP 请求经历什么步骤?
  • 清理与重装Docker的完整步骤
  • 一个浏览器多人用?Docker+Neko+cpolar实现跨网共享
  • 石灰土做击实检测网站怎么填教育培训网站源码 模板 php培训机构网站源码培训学校网站源码
  • Rust 与 Python:语法对比全景指南
  • 使用vgpu_unlock在ubuntu 22.04上解锁GTX1060 (by quqi99)
  • 北京制作网站的公司简介下载站源码cms
  • MySQL 8+ 日志管理与数据备份恢复实战指南
  • 【MySQL 数据库】MySQL用户管理
  • EXPLAIN执行计划详解
  • 【文档】配置 prometheus-webhook-dingtalk + alertmanager 细节
  • higress开通tcp和websocket网关
  • 国外优秀网站建设什么样的网站可以做外链
  • 【JavaWeb|第一篇】Maven篇
  • 如何上传网页到网站好玩网页传奇
  • 打造专属Spring Boot Starter
  • Elasticsearch面试精讲 Day 30:Elasticsearch面试真题解析与答题技巧
  • 单一key-value对象工具-org.apache.commons.lang3.tuple.Pair
  • h5游戏免费下载:3D小车车
  • 分布式事务详解
  • Flink重启策略有啥用
  • 怎样做好物流网站建设免费商业wordpress主题
  • 怎么把qq空间做成企业网站网站用ps下拉效果怎么做
  • 输电线路绝缘子污秽度在线监测装置工作原理及优势解析
  • MOSHELL (7) : 构建3G RNC端到端性能可观测性体系