Elasticsearch
,简称ES,它是个开源分布式搜索引擎
restful
风格接口,多数据源,自动搜索负载等PB
级别的数据ES
也使用Java开发并使用Lucene
作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful
API来隐藏Lucene
的复杂性,从而让全文搜索变得简单Elasticsearch
是**面向文档**(document oriented
)的
document
)index
)每个文档的内容使之可以被搜索
apt-key
的警告,如果不想报警告使用下边这个# 1.
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
# 2.
curl -s https://artifacts.elastic.co/GPG-KEY-elasticsearch | \
sudo gpg --no-default-keyring \
--keyring gnupg-ring:/etc/apt/trusted.gpg.d/icsearch.gpg --import
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" \
| sudo tee /etc/apt/sources.list.d/elasticsearch.list
sudo apt update
sudo apt-get install elasticsearch=7.17.21
sudo systemctl start elasticsearch
ik
分词器插件:sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install \
https://get.infini.cloud/elasticsearch/analysis-ik/7.17.21
sudo systemctl status elasticsearch.service
curl -X GET "http://localhost:9200/"
IP:PORT
vim /etc/elasticsearch/elasticsearch.yml
# 新增配置
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
# 调整ES虚拟内存,虚拟内存默认最大映射数为65530,无法满足ES系统要求, 需要调整为262144以上
sudo sysctl -w vm.max_map_count=262144
# 增加虚拟机内存配置
sudo vim /etc/elasticsearch/jvm.options
# 新增如下内容
-Xms512m
-Xmx512m
Job for elasticsearch.service failed because the control process exited with error code.
See "systemctl status elasticsearch.service" and "journalctl -xeu elasticsearch.service" for details.
sudo apt install kibana
Kibana
,配置文件通常位于/etc/kibana/kibana.yml
,可能需要设置如服务器地址、端口、Elasticsearch URL
等sudo systemctl start kibana
sudo systemctl enable kibana
http://<ip>:5601
enabled
:是否仅作存储,不做搜索和分析
true(默认)/false
index
:是否构建倒排索引(决定了是否分词,是否被索引)
true(默认)/false
index_option
dynamic
:控制mapping
的自动更新
true(默认)/false
doc_value
:是否开启doc_value
,用户聚合和排序分析,分词字段不能使用
true(默认)/false
fielddata
:是否为text
类型启动fielddata
,实现排序和聚合分析
doc_value
fielddata": {
"format": "disabled"
}
store
:是否单独设置此字段的是否存储而从_source
字段中分离
true/false(默认)
coerce
:是否开启自动数据类型转换功能,如字符串转整形,浮点转整形
true(默认)/false
analyzer
:指定分词器,默认分词器是standard analyzer
”analyzer”: “ik”
boost
:字段级别的分数加权,默认值是1.0
”boost”: 1.25
fields
:对一个字段提供多种索引模式,同一个字段的值,一个分词一个不分词"fields": {
"raw": {
"type": "text",
"index": "not_analyzed"
}
}
data_detection
:是否自动识别日期类型
true(默认)/false
一个文档是一个可被索引的基础信息单元
例如:某一个客户的文档,某一个产品的一个文档或者某个订单的一个文档
index/type
里面,可以存储任意多的文档type
Elasticsearch
与传统关系性数据库相比:
DB | Database | Table | Row | Column |
---|---|---|---|---|
ES | Index | Type | Document | Field |
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
}
}
}
}
POST /user/_doc/_bulk
{"index":{"_id":"1"}}
{"user_id" : "USER4b862aaa-2df8654a-7eb4bb65e3507f66","nickname" : "昵称1","phone" : "手机号1","description" : "签名1","avatar_id" : "头像1"}
{"index":{"_id":"2"}}
{"user_id" : "USER14eeeaa5-442771b9-0262e455e4663d1d","nickname" : "昵称2","phone" : "手机号2","description" : "签名2","avatar_id" : "头像2"}
{"index":{"_id":"3"}}
{"user_id" : "USER484a6734-03a124f0-996c169dd05c1869","nickname" : "昵称3","phone" : "手机号3","description" : "签名3","avatar_id" : "头像3"}
{"index":{"_id":"4"}}
{"user_id" : "USER186ade83-4460d4a6-8c08068f83127b5d","nickname" : "昵称4","phone" : "手机号4","description" : "签名4","avatar_id" : "头像4"}
{"index":{"_id":"5"}}
{"user_id" : "USER6f19d074-c33891cf-23bf5a8357189a19","nickname" : "昵称5","phone" : "手机号5","description" : "签名5","avatar_id" : "头像5"}
{"index":{"_id":"6"}}
{"user_id" : "USER97605c64-9833ebb7-d045535335a59195","nickname" : "昵称6","phone" : "手机号6","description" : "签名6","avatar_id" : "头像6"}
[
{
"index": {
"_id": "1"
},
"user": {
"user_id": "USER4b862aaa-2df8654a-7eb4bb65e3507f66",
"nickname": "昵称1",
"phone": "手机号1",
"description": "签名1",
"avatar_id": "头像1"
}
},
{
"index": {
"_id": "2"
},
"user": {
"user_id": "USER14eeeaa5-442771b9-0262e455e4663d1d",
"nickname": "昵称2",
"phone": "手机号2",
"description": "签名2",
"avatar_id": "头像2"
}
},
{
"index": {
"_id": "3"
},
"user": {
"user_id": "USER484a6734-03a124f0-996c169dd05c1869",
"nickname": "昵称3",
"phone": "手机号3",
"description": "签名3",
"avatar_id": "头像3"
}
},
{
"index": {
"_id": "4"
},
"user": {
"user_id": "USER186ade83-4460d4a6-8c08068f83127b5d",
"nickname": "昵称4",
"phone": "手机号4",
"description": "签名4",
"avatar_id": "头像4"
}
},
{
"index": {
"_id": "5"
},
"user": {
"user_id": "USER6f19d074-c33891cf-23bf5a8357189a19",
"nickname": "昵称5",
"phone": "手机号5",
"description": "签名5",
"avatar_id": "头像5"
}
},
{
"index": {
"_id": "6"
},
"user": {
"user_id": "USER97605c64-9833ebb7-d045535335a59195",
"nickname": "昵称6",
"phone": "手机号6",
"description": "签名6",
"avatar_id": "头像6"
}
}
]
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" : "昵称"
}
}
]
}
}
}
DELETE /user
POST /user/_doc/_search
{
"query":
{
"match_all":{}
}
}
elasticlient
库MicroHTTPD
库sudo apt-get install libmicrohttpd-dev
# 克隆代码
git clone https://github.com/seznam/elasticlient
# 切换目录
cd elasticlient
# 更新子模块
git submodule update --init --recursive
# 编译代码
make build && cd build
cmake ..
make
# 安装
make install
/**
* 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());
http://127.0.0.1:9200/
#include <iostream>
#include <elasticlient/client.h>
#include <cpr/cpr.h>
int main()
{
// 1.构造ES客户端
elasticlient::Client client({"http://127.0.0.1:9200/"});
// 2.发起搜索请求
try
{
auto resp = client.search("user", "_doc",
"{\"query\": { \"match_all\":{} }}");
std::cout << resp.status_code << std::endl;
std::cout << resp.text << std::endl;
}
catch(std::exception &e)
{
std::cout << e.what() << std::endl;
return -1;
}
return 0;
}