6.聊天室环境安装 - Ubuntu22.04 - elasticsearch(es)的安装和使用

目录

  • 介绍
  • 安装
  • 安装kibana
  • 安装ES客户端
  • 使用

介绍

Elasticsearch, 简称 ES,它是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful 风格接口,多数据源,自动搜索负载等。它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理 PB 级别的数据。es 也使用 Java 开发并使用 Lucene 作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTful API 来隐藏 Lucene 的复杂性,从而让全文搜索变得简单。
Elasticsearch 是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索。在 Elasticsearch 中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。

安装

1.添加仓库密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
2.添加镜像源仓库
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elasticsearch.list
3.更新软件包列表
sudo apt update
4.安装es
sudo apt-get install elasticsearch=7.17.21
5.安装ik分词器插件
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/7.17.21
6.启动es
sudo systemctl start elasticsearch
6.5如果启动es失败
调整 ES 虚拟内存,虚拟内存默认最大映射数为 65530,无法满足 ES 系统要求,需要调整为 262144 以上
sysctl -w vm.max_map_count=262144
增加虚拟机内存配置
vim /etc/elasticsearch/jvm.options
新增如下内容:
-Xms512m
-Xmx512m
在这里插入图片描述

设置完后重启ubuntu
7.查看es服务的状态
sudo systemctl status elasticsearch.service
在这里插入图片描述
8.验证es是否安装成功
curl -X GET "http://localhost:9200/"
在这里插入图片描述
9.设置能够外部访问: 如果新配置完成默认只能在本机进行访问
vim /etc/elasticsearch/elasticsearch.yml
新增配置

network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

在这里插入图片描述
重启后sudo systemctl restart elasticsearch.service
浏览器访问http://自己的IP:9200/
在这里插入图片描述

安装kibana

kibana可以支持通过网页对ES进行访问(增删查改), 这可以让我们的测试更加直观一些
1.安装kibana
sudo apt install kibana
2.配置kibana
sudo vim /etc/kibana/kibana.yml
在7行修改为server.host: "0.0.0.0"
在这里插入图片描述

在32行修改为elasticsearch.hosts: ["http://0.0.0.0:9200"]
在这里插入图片描述
3.启动kibana服务
sudo systemctl start kibana
4.验证安装
sudo systemctl status kibana
5.访问kibana
在浏览器访问kibana, http://你的ip:5601
在这里插入图片描述

安装ES客户端

1.克隆代码
git clone https://github.com/seznam/elasticlient
注意: 这里不能从github下载源码然后拖拽进来, 因为内部有git子模块, 需要去更新子模块之后, 才能去编译
如果无法始终clone不下来, 也有对应的解决方案:https://blog.csdn.net/eyuyanniniu/article/details/145807381
2.切换目录
cd elasticlient
3.安装 MicroHTTPD 库
sudo apt-get install libmicrohttpd-dev
4.更新子模块
git submodule update --init --recursive
5.编译安装代码
mkdir build
cd build
cmake ..
make
make install

6.配置环境变量
因为我们的库默认安装路径是/usr/local/lib, 编译器可能找不到这个库目录的位置
所以我们需要配置(这些文件最好都进行配置):
全局设置: /etc/profile 当前用户设置: .bash_profil或.bashrc
在文件末尾加上 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

使用

封装icsearch.hpp文件

#include <elasticlient/client.h>
#include <cpr/cpr.h>
#include <json/json.h>
#include <iostream>
#include <memory>
#include "logger.hpp"//ES的二次封装, 原因: 为了简化ES的使用操作, 我们可以看到, 请求的时候, 正文很长, 我们希望只设置我们关心的参数即可, 而且能自动的构造完成
//封装四个操作: 索引创建, 数据新增, 数据查询, 数据删除namespace wufan_im{
bool UnSerialize(const std::string& src, Json::Value& val)
{// 同样的Read类, 需要先构造出工厂类Json::CharReaderBuilder crb;std::unique_ptr<Json::CharReader> cr(crb.newCharReader());std::string err;bool ret = cr->parse(src.c_str(), src.c_str() + src.size(), &val, &err);if (ret == false) {std::cout << "json反序列化失败: " << err << std::endl;return false;}return true;
}bool Serialize(const Json::Value& val, std::string& dst)
{// Writer(StreamWriter)类, 这个类就是用来序列化的, 但是这个类不能直接构造, 因为使用了工厂模式// 先定义Json::SreamWriter 工厂类 Json::StreamWriterBuilderJson::StreamWriterBuilder swb;  //构造出工厂类std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());// 通过Json::StreamWriter中的write接口进行序列化std::stringstream ss;int ret = sw->write(val, &ss);  //将其序列化到字符流里面if (ret != 0) {std::cout << "Json反序列化失败!\n";return false;}dst = ss.str();return true;
}// 索引创建:
// 传两个参数, 索引名称 和 索引类型 就可以创建出索引
// 能够添加字段, 并设置字段类型, 设置分词器类型, 是否构造索引
class ESIndex{public:ESIndex(std::shared_ptr<elasticlient::Client>& client, const std::string& name, const std::string& type = "_doc"):_name(name), _type(type), _client(client){Json::Value analysis;   //可以把Value当做Json里的{ }Json::Value analyzer;Json::Value ik;Json::Value tokenizer;tokenizer["tokenizer"] = "ik_max_word";ik["ik"] = tokenizer;analyzer["analyzer"] = ik;analysis["analysis"] = analyzer;_index["settings"] = analysis;}// 创建索引, 就相当于在设置表结构 - ai说的// 添加字段, 就相当于设置表的字段属性ESIndex& append(const std::string& key, const std::string& type = "text", const std::string& analyzer = "ik_max_word", bool enabled = true) {Json::Value fields;fields["type"] = type;fields["analyzer"] = analyzer;if (enabled == false) fields["enabled"] = enabled;_properties[key] = fields;return *this;}bool create(const std::string& index_id = "default_index_id") {Json::Value mappings;mappings["dynamic"] = true;mappings["properties"] = _properties;_index["mappings"] = mappings;std::string body;bool ret = Serialize(_index, body);if (ret == false) {LOG_ERROR("索引序列化失败! ");return false;}LOG_DEBUG("{}", body);// 2. 发起搜索请求try{    //因为请求失败就可能会抛异常, 异常你不接住, 程序就会崩溃auto rsp = _client->index(_name, _type, index_id, body);if (rsp.status_code < 200 || rsp.status_code >= 300) {LOG_ERROR("创建ES索引 {} 失败, 响应状态码异常: {}", _name, rsp.status_code);return false;}} catch(std::exception& e) {LOG_ERROR("创建ES索引 {} 失败: {}", _name, e.what());return false;}return true;}private:std::string _name;std::string _type;Json::Value _properties;Json::Value _index;std::shared_ptr<elasticlient::Client> _client;
};// 数据新增
class ESInsert{public:ESInsert(std::shared_ptr<elasticlient::Client>& client, const std::string& name,const std::string& type = "_doc"):_name(name), _type(type), _client(client){}ESInsert& append(const std::string& key, const std::string& val){_item[key] = val;return *this;}// 插入到哪个id里面 -  这个ID就相当于是每一次插入时数据的唯一标识bool insert(const std::string id = ""){std::string body;bool ret = Serialize(_item, body);if (ret == false) {LOG_ERROR("索引序列化失败! ");return false;}LOG_DEBUG("{}", body);// 2. 发起搜索请求try{    //因为请求失败就可能会抛异常, 异常你不接住, 程序就会崩溃auto rsp = _client->index(_name, _type, id, body);if (rsp.status_code < 200 || rsp.status_code >= 300) {LOG_ERROR("新增数据 {} 失败, 响应状态码异常: {}", body, rsp.status_code);return false;}} catch(std::exception& e) {LOG_ERROR("新增数据 {} 失败: {}", body, e.what());return false;}return true;}private:std::string _name;std::string _type;Json::Value _item;std::shared_ptr<elasticlient::Client> _client;
};// 数据删除
class ESRemove{public:ESRemove(std::shared_ptr<elasticlient::Client>& client, const std::string& name, const std::string& type):_name(name), _type(type), _client(client){}bool remove(const std::string& id) {try{    //因为请求失败就可能会抛异常, 异常你不接住, 程序就会崩溃auto rsp = _client->remove(_name, _type, id);if (rsp.status_code < 200 || rsp.status_code >= 300) {LOG_ERROR("删除数据 {} 失败, 响应状态码异常: {}", id, rsp.status_code);return false;}} catch(std::exception& e) {LOG_ERROR("删除数据 {} 失败: {}", id, e.what());return false;}return true;}private:std::string _name;std::string _type;std::shared_ptr<elasticlient::Client> _client;
};//数据查询
class ESSearch{public: //用户还会设置过滤条件,以及应该包含的字段ESSearch(std::shared_ptr<elasticlient::Client>& client, const std::string& name, const std::string& type = "_doc"):_name(name), _type(type), _client(client){}ESSearch& append_must_not_terms(const std::string& key, const std::vector<std::string>& vals){Json::Value fields;for (const auto& val : vals) {fields[key].append(val);}Json::Value terms;terms["terms"] = fields;_must_not.append(terms);return *this;}ESSearch& append_should_match(const std::string& key, const std::string& val) {Json::Value field;field[key] = val;Json::Value match;match["match"] = field;_should.append(match);return *this;}Json::Value search() {Json::Value cond;if (_must_not.empty() == false) cond["must_not"] = _must_not;if (_should.empty() == false) cond["should"] = _should;Json::Value query;query["bool"] = cond;Json::Value root;root["query"] = query;std::string body;bool ret = Serialize(root, body);if (ret == false) {LOG_ERROR("索引序列化失败! ");return Json::Value();}LOG_DEBUG("{}", body);// 2. 发起搜索请求cpr::Response rsp;try{    //因为请求失败就可能会抛异常, 异常你不接住, 程序就会崩溃rsp = _client->search(_name, _type, body);if (rsp.status_code < 200 || rsp.status_code >= 300) {LOG_ERROR("检索数据 {} 失败, 响应状态码异常: {}", body, rsp.status_code);return Json::Value();}} catch(std::exception& e) {LOG_ERROR("检索数据 {} 失败: {}", body, e.what());return Json::Value();}//3. 需要对响应正文进行反序列化LOG_DEBUG("检索响应正文: [{}]", rsp.text);Json::Value json_res;ret = UnSerialize(rsp.text, json_res);if (ret == false) {LOG_ERROR("检索数据 {} 结果反序列化失败", rsp.text);return Json::Value();}return json_res["hits"]["hits"];}private:std::string _name;std::string _type;//用户还会设置过滤条件,以及应该包含的字段Json::Value _must_not;  //必须不包含的Json::Value _should;    //必须包含的, 多选一即可std::shared_ptr<elasticlient::Client> _client;
};
}

main.cc文件

#include "../../common/icsearch.hpp"
#include <gflags/gflags.h>DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");int main(int argc, char* argv[])
{google::ParseCommandLineFlags(&argc, &argv, true);wufan_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);std::shared_ptr<elasticlient::Client> client(new elasticlient::Client({"http://127.0.0.1:9200/"}));bool ret = wufan_im::ESIndex(client, "test_user")// 创建索引, 就相当于在设置表结构 - ai说的// 添加字段, 就相当于设置表的字段属性.append("nickname").append("phone", "keyword", "standard", true) //手机号是不能进行分词的, 是一个关键字, 分词器用标准分词器, 需要构造索引.create();if (ret == false) {LOG_INFO("索引创建失败!");return -1;}LOG_INFO("索引创建成功");// 新增数据ret = wufan_im::ESInsert(client, "test_user").append("nickname", "张三").append("phone", "155666777").insert("00001");   // 这个ID就相当于是每一次插入时数据的唯一标识if (ret == false) {LOG_ERROR("数据插入失败!");return -1;}// 数据的修改ret = wufan_im::ESInsert(client, "test_user").append("nickname", "张三").append("phone", "1334444555").insert("00001");if (ret == false) {LOG_ERROR("数据更新失败!");return -1;}LOG_INFO("数据新增成功");Json::Value user = wufan_im::ESSearch(client, "test_user").append_should_match("phone.keyword", "1334444555")     //检索的时候, 告诉ES, 这个关键词不要进行分词// .append_must_not_terms("nickname.keyword", {"张三"}).search();if (user.empty() || user.isArray() == false) {LOG_ERROR("结果为空, 或者结果不是数组类型");return -1;}LOG_INFO("数据检索成功");int sz = user.size();LOG_DEBUG("检索结果条目数量: {}", sz);for (int i = 0; i < sz; ++i) {LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());}ret = wufan_im::ESRemove(client, "test_user", "_doc").remove("00001");if (ret == false) {LOG_ERROR("删除数据失败");return -1;}LOG_INFO("数据删除成功");return 0;
}

运行程序:
在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

蓝桥每日打卡--合根植物

#蓝桥#JAVA#合根植物 题目描述 w星球的一个种植园&#xff0c;被分成mn个小格子&#xff08;东西方向m行&#xff0c;南北方向n列&#xff09;。每个格子里种了一株合根植物。 这种植物有个特点&#xff0c;它的根可能会沿着南北或东西方向伸展&#xff0c;从而与另一个格子…

线性代数之矩阵特征值与特征向量的数值求解方法

文章目录 前言1. 幂迭代法&#xff08;Power Iteration&#xff09;幂法与反幂法求解矩阵特征值幂法求最大特征值编程实现补充说明 2. 逆幂迭代法&#xff08;Inverse Iteration&#xff09;移位反幂法 3. QR 算法&#xff08;QR Algorithm&#xff09;——稠密矩阵理论推导编程…

【Linux实践系列】:用c语言实现一个shell外壳程序

&#x1f525;本文专栏&#xff1a;Linux Linux实践项目 &#x1f338;博主主页&#xff1a;努力努力再努力wz 那么今天我们就要进入Linux的实践环节&#xff0c;那么我们之前学习了进程控制相关的几个知识点&#xff0c;比如进程的终止以及进程的等待和进程的替换&#xff0c;…

使用STM32CubeMX配置定时器中断实现LED每秒闪烁一次(STM32G070CBT6)

说明&#xff1a; 本案例采用的定时器3&#xff08;TIM3&#xff09;实现&#xff0c;使用其他定时器是一样配置。 如何新建一个工程以及如何配置LED的端口&#xff0c;请查看前面文章&#xff1a;使用STM32CubeMX实现LED灯每秒闪烁一次&#xff08;STM32G070CBT6单片机&…

2025年Draw.io最新版本下载安装教程,附详细图文

2025年Draw.io最新版本下载安装教程&#xff0c;附详细图文 大家好&#xff0c;今天给大家介绍一款非常实用的流程图绘制软件——Draw.io。不管你是平时需要设计流程图、绘制思维导图&#xff0c;还是制作架构图&#xff0c;甚至是简单的草图&#xff0c;它都能帮你轻松搞定。…

GStreamer —— 2.15、Windows下Qt加载GStreamer库后运行 - “播放教程 1:Playbin 使用“(附:完整源码)

运行效果 介绍 我们已经使用了这个元素&#xff0c;它能够构建一个完整的播放管道&#xff0c;而无需做太多工作。 本教程介绍如何进一步自定义&#xff0c;以防其默认值不适合我们的特定需求。将学习&#xff1a; • 如何确定文件包含多少个流&#xff0c;以及如何切换 其中。…

Python----数据可视化(Seaborn一:介绍,应用)

一、Seaborn的介绍 Seaborn 是一个基于 matplotlib 的 Python 库&#xff0c;对其进行了高级 API 的封装&#xff0c;使得作图更为方便和吸引人。尽管在大多数情况下&#xff0c;使用 Seaborn 就能够创建出美观的图表&#xff0c;但 matplotlib 提供了更高的灵活性和定制化的能…

小程序SSL证书过期怎么办?

SSL证书就像小程序的“安全锁”&#xff0c;一旦过期&#xff0c;用户访问时会被提示“不安全”&#xff0c;轻则流失客户&#xff0c;重则数据泄露&#xff01;作为企业负责人&#xff0c;如何快速解决证书过期问题&#xff1f;又该如何避免再次踩坑&#xff1f;这篇指南给你答…

Linux上位机开发实战(x86和arm自由切换)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 前面我们说过&#xff0c;qt本身支持windows系统&#xff0c;也支持linux系统。不仅如此&#xff0c;qt除了支持传统的x86 cpu之外&#xff0c;还支…

Mysql的卸载安装配置以及简单使用

MySQL其它问题已经更新在&#xff1a;MySQL完善配置---可视化-CSDN博客 一、卸载 ①控制面板卸载 ②C盘隐藏项目>ProgramData>mysql相关文件夹&#xff0c;还有Program file下的MySQL文件夹 ③开始菜单栏搜索>服务&#xff0c;找到MySQL相关服务删除&#xff0c;如果再…

RabbitMQ之旅(1)

相信自己,终会成功 目录 主流MQ产品 1.kafaka 2.RocketMQ 3.RabbitMQ 在xshell上安装RabbitMQ RabbitMQ七种工作模式 1.简单模式 ​编辑 2.工作队列模式 3.发布/订阅模式 4.路由模式 5.通配符模式 6.RPC模式 AMQP.BasicProperties 设置消息属性的类 7.发布确认模…

基于Matlab的人脸识别的二维PCA

一、基本原理 传统 PCA 在处理图像数据时&#xff0c;需将二维图像矩阵拉伸为一维向量&#xff0c;这使得数据维度剧增&#xff0c;引发高计算成本与存储压力。与之不同&#xff0c;2DPCA 直接基于二维图像矩阵展开运算。 它着眼于图像矩阵的列向量&#xff0c;构建协方差矩阵…

el-pagination的使用说明

<el-paginationv-model:current-page"pageNo" //当前第几页v-model:page-size"pageSize" //每页显示多少条数据:page-sizes"[10, 20, 30]" //控制每页显示的条数:small"true" //控制分页器大小:disabled&quo…

Redis Redis介绍、安装 - Redis客户端

目录 redis是什么&#xff0c;他的应用场景是什么&#xff1f; Redis的一些主要特点和应用场景&#xff1a; redis的官方网站&#xff1a;Redis redis是键值型数据库&#xff1a;&#xff08;也就是key-value模式&#xff09;&#xff08;跟python的字典很像&#xff09; …

LWIP网络模型及接口简介(DAY 01)

目录 1.网络协议分层模型 2. LWIP三种编程接口 1.网络协议分层模型 其中各层级的封装与拆封过程 2. LWIP三种编程接口 LwIP 提供了三种编程接口&#xff0c;分别为 RAW/Callback API、NETCONN API、SOCKET API。它们的易用性从左到右依次提高&#xff0c;而执行效率从左到右依…

【Python 数据结构 14.邻接表】

希望你的眼睛可以一直笑&#xff0c;想要的都得到 —— 25.3.11 一、邻接表的概念 1.邻接表的定义 邻接表是一种表示图的数据结构。邻接表的主要概念是&#xff1a;对于图中的每个顶点&#xff0c;维护一个由与其相邻的顶点组成的列表。这个列表可以用数组、链表或其他数据结构…

01 音视频知识学习(视频)

图像基础概念 ◼像素&#xff1a;像素是一个图片的基本单位&#xff0c;pix是英语单词picture的简写&#xff0c;加上英 语单词“元素element”&#xff0c;就得到了“pixel”&#xff0c;简称px&#xff0c;所以“像素”有“图像元素” 之意。 ◼ 分辨率&#xff1a;是指图像…

git文件过大导致gitea仓库镜像推送失败问题解决(push failed: context deadline exceeded)

问题描述&#xff1a; 今天发现gitea仓库推送到某个镜像仓库的操作几个月前已经报错终止推送了&#xff0c;报错如下&#xff1a; 首先翻译报错提示可知是因为git仓库大小超过1G限制。检查本地.git文件&#xff0c;发现.git文件大小已达到1.13G。确定是.git文件过大导致&…

clickhouse集群部署保姆级教程

ClickHouse安装 版本要求 23.8及之后的版本 硬件要求 三台机器 建议配置 磁盘 ssd 500G内存 32gcpu 16c 最低配置 磁盘 机械硬盘 50G内存 4gcpu 4c 容量规划 一亿条数据大约使用1TB磁盘容量 参考官方容量推荐 安装包准备 zookeeper安装 zookeeper需要java启动&…

FANformer:融合傅里叶分析网络的大语言模型基础架构

近期大语言模型(LLM)的基准测试结果引发了对现有架构扩展性的思考。尽管OpenAI推出的GPT-4.5被定位为其最强大的聊天模型&#xff0c;但在多项关键基准测试上的表现却不及某些规模较小的模型。DeepSeek-V3在AIME 2024评测中达到了39.2%的Pass1准确率&#xff0c;在SWE-bench Ve…