综述由AI生成Elasticsearch(ES)分布式搜索引擎的基本概念、安装配置及核心术语。内容包括 ES 与 Kibana 的安装步骤、索引、类型、字段、映射及文档等核心概念。重点讲解了 C++ 客户端 elasticlient 的使用,涵盖环境搭建、接口说明及代码示例,展示了如何通过 C++ 进行数据的增删改查操作。
# 调整 ES 虚拟内存,虚拟内存默认最大映射数为 65530,无法满足 ES 系统要求,需要调整为 262144 以上sudo sysctl -w vm.max_map_count=262144
# 增加虚拟机内存配置sudo vim /etc/elasticsearch/jvm.options
# 新增如下内容
-Xms512m
-Xmx512m
Job forelasticsearch.service failed because the control process exited with error code. See "systemctl status elasticsearch.service" and "journalctl -xeu elasticsearch.service"fordetails.
设置外网访问:默认只能在本机进行访问,修改后浏览器访问 IP:PORT
vim /etc/elasticsearch/elasticsearch.yml
# 新增配置
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
POST /user/_doc/_search
{"query":{"match_all":{}}}
删除索引:
DELETE /user
查看并搜索数据:
GET /user/_doc/_search?pretty
{"query":{"bool":{"must_not":[{"terms":{"user_id.keyword":["USER4b862aaa-2df8654a-7eb4bb65e3507f66","USER14eeeaa5-442771b9-0262e455e4663d1d","USER484a6734-03a124f0-996c169dd05c1869"]}}],"should":[{"match":{"user_id":"昵称"}},{"match":{"nickname":"昵称"}},{"match":{"phone":"昵称"}}]}}}
POST /user/_doc
{"settings":{"analysis":{"analyzer":{"ik":{"tokenizer":"ik_max_word"}}}},"mappings":{"dynamic":true,"properties":{"nickname":{"type":"text","analyzer":"ik_max_word"},"user_id":{"type":"keyword","analyzer":"standard"},"phone":{"type":"keyword","analyzer":"standard"},"description":{"type":"text","enabled":false},"avatar_id":{"type":"keyword","enabled":false}}}}
# 克隆代码
git clone https://github.com/seznam/elasticlient
# 切换目录cd elasticlient
# 更新子模块
git submodule update --init --recursive
# 编译代码
make build && cd build
cmake ..
make
# 安装
make install
前置安装:依赖 MicroHTTPD库
sudo apt-get install libmicrohttpd-dev
6.ES 客户端接口介绍
/**
* Perform search on nodes until it is successful. Throws exception if all nodes
* has failed to respond.
* \param indexName specification of an Elasticsearch index.
* \param docType specification of an Elasticsearch document type.
* \param body Elasticsearch request body.
* \param routing Elasticsearch routing. If empty, no routing has been used.
*
* \return cpr::Response if any of node responds to request.
* \throws ConnectionException if all hosts in cluster failed to respond.
*/cpr::Response search(const std::string &indexName,
const std::string &docType,
const std::string &body,
const std::string &routing = std::string());
/**
* Get document with specified id from cluster. Throws exception if all nodes
* has failed to respond.
* \param indexName specification of an Elasticsearch index.
* \param docType specification of an Elasticsearch document type.
* \param id Id of document which should be retrieved.
* \param routing Elasticsearch routing. If empty, no routing has been used.
*
* \return cpr::Response if any of node responds to request.
* \throws ConnectionException if all hosts in cluster failed to respond.
*/cpr::Response get(const std::string &indexName,
const std::string &docType,
const std::string &id = std::string(),
const std::string &routing = std::string());
/**
* Index new document to cluster. Throws exception if all nodes has failed to respond.
* \param indexName specification of an Elasticsearch index.
* \param docType specification of an Elasticsearch document type.
* \param body Elasticsearch request body.
* \param id Id of document which should be indexed. If empty, id will be generated
* automatically by Elasticsearch cluster.
* \param routing Elasticsearch routing. If empty, no routing has been used.
*
* \return cpr::Response if any of node responds to request.
* \throws ConnectionException if all hosts in cluster failed to respond.
*/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());
/**
* Delete document with specified id from cluster. Throws exception if all nodes
* has failed to respond.
* \param indexName specification of an Elasticsearch index.
* \param docType specification of an Elasticsearch document type.
* \param id Id of document which should be deleted.
* \param routing Elasticsearch routing. If empty, no routing has been used.
*
* \return cpr::Response if any of node responds to request.
* \throws ConnectionException if all hosts in cluster failed to respond.
*/cpr::Response remove(const std::string &indexName,
const std::string &docType,
const std::string &id,
const std::string &routing = std::string());