一、ElasticSearch简介
1、elasticSearch
elasticsearch是一个实时分布式搜索和分析引擎,它用于全文搜索、结构化搜索、分析以及将这三种功能混合使用,使用java编写,基于Lucene 实现
Elaticsearch,简称为es, es是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。
ElasticSearch的底层使用Lucene作为其核心来实现所有索引和搜索的功能,但是你没办法直接用Lucene,必须自己写代码去调用它的接口,Elastic是Lucene的封装,提供了REST API的操作接口,开箱即用,天然的跨平台,从而让全文搜索变得简单。
全文检索是我们在实际项目开发中最常见的需求了,而ElasticSearch是目前全文检索引擎的首选,它可以快速的存储,搜索和分析海量的数据,维基百科,GitHub,Stack Overflow都采用了ElasticSearch。
2、官方网站
3、中文社区
4、优势
- 分布式的实时数据存储,每个字段都被索引并可被搜索
- 分布式搜索引擎可实时分析
- 横向可扩展:支持上百台服务节点的扩展,集群增加机器简单,支持处理PB级数据
- 分片机制
- 隐藏复杂实现:Elasticsearch 内部隐藏了分布式系统的复杂性,我们不用去关心它是如何做到高可用,可扩展,高性能的
- 简单易用:开源产品,不需要额外配置,就可以运行一个Elasticsearch服务
二、ElasticSearch安装
1、通过docker安装
1、安装docker环境这里就不细述了,网上资料挺多的
[CentOS Docker 安装](https://www.runoob.com/docker/centos-docker-install.html)
-
[Ubuntu Docker 安装](https://www.runoob.com/docker/ubuntu-docker-install.html)
-
[Docker 镜像加速](https://www.runoob.com/docker/docker-mirror-acceleration.html)
-
[docker官方教程](https://docs.docker.com/desktop/install/mac-install/)
2、启动elasticsearch镜像
2.1 拉取最新版本的elasticsearch
docker pull elasticsearch
2.2 拉取指定版本elasticsearch,这里是拉取7.11.1版本的elasticsearch
docker pull elasticsearch:7.11.1
2.3 启动 elasticsearch
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d elasticsearch:7.11.1
运行docker镜像参数说明:
-p:端口映射
-e:设置环境变量,discovery.type=single-node(单机运行),ES_JAVA_OPTS="-Xms512m -Xmx512m"(设置JVM参数)
-d:后台启动
–name:容器名称
2.4 验证elasticsearch是否启动成功,浏览器输入http://localhost:9200,展示类似如下的信息表示安装成功
{
"name" : "e870c1e3d666",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "KFkoZJqXQBGPOb7PjCinwQ",
"version" : {
"number" : "7.11.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ff17057114c2199c9c1bbecc727003a907c0db7a",
"build_date" : "2021-02-15T13:44:09.394032Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
2、官网下载
去elasticsearch官网下载对应的版本安装,下载地址
-
将下载的Archive包解压到本地
-
命令行启动 bin/elasticsearch
-
验证elasticsearch是否启动成功,浏览器输入http://localhost:9200
-
获取运行的pid ps -ef | grep Elasticsearch
-
kill -SIGTERM pid 关闭elasticsearch服务
三、可视化工具
安装
使用 docker 安装 Dejavu 服务
docker run -p 1358:1358 -d appbaseio/dejavu
要确保为Elasticsearch实例启用CORS设置,请在Elasticsearch.yml配置文件中添加以下几行
http.port: 9200
http.cors.allow-origin: 'http://localhost:1358'
http.cors.enabled: true
http.cors.allow-headers: X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
http.cors.allow-credentials: true
使用
安装完成后可以使用浏览器打开 http://ip:1358 使用。
四、ElasticSearch基本概念
1、Index:索引
一个索引就是一个拥有相似特征和机构的文档集合,可类比mysql中的数据库。比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。在一个集群中,可以定义任意多的索引。
新建索引
API:PUT /
this.esService.indices.create({
index: indexName,
body: {
mappings: {
properties: {
traceId: { type: 'text' },
order: { type: 'integer' },
isAutoTrack: { type: 'boolean' },
event: { type: 'text' },
eventDesc: { type: 'text' },
appId: { type: 'text' },
version: { type: 'text' },
appName: { type: 'text' },
userId: { type: 'text' },
path: { type: 'text' },
query: { type: 'text' },
logType: { type: 'text' },
isError: { type: 'boolean' },
errMessage: { type: 'text' },
consumeTime: { type: 'integer' },
reportTime: { type: 'date' },
createTime: { type: 'date' },
},
},
},
}
查询索引
API:GET /_cat/indices
this.esService.cat.indices({})
删除索引
API:DELETE /
this.esService.indices.delete({
index: indexName,
})
2、Mapping:映射
映射是定义文档及其包含的字段如何存储和索引的过程。
动态映射
动态映射不需要事先定义映射,文档在写入es的时候,会根据文档字段自动识别类型
Json 数据类型 | es 数据类型 |
---|---|
null | 没有字段添加 |
true,false | boolean |
integer | long |
object | object |
array | 依赖于数组中首个非空值 |
string | text和keyword |
日期 | date或text |
静态映射
静态映射需要事先定义好映射,包含文档的各个字段及其类型
mappings: {
properties: {
ext: { type: 'object' },
isAutoTrack: { type: 'boolean' },
event: { type: 'text' },
appId: { type: 'text' },
errMessage: { type: 'text' },
consumeTime: { type: 'integer' },
reportTime: { type: 'date' },
createTime: { type: 'date' },
},
}
3、Type:类型
一个索引可以对应一个或者多个类型,类型可以当做是索引的逻辑分区,作用相当于mysql中的表。index的mapping会存储完整的多个type的字段信息,如果type的字段差别太大,那么就会导致mapping需要存储的字段过多。lasticSearch维护组织后面发现这多个type的情况确实有点烦人。于是他们准备让一个index只放一个type了,在6.x版本中,一个索引下只有一个type,如果声明多个type,会被标记为过期,但是仍可以使用。7.0之后的版本将完全移除 。
4、Document:文档
存储在es中的一个JSON格式的字符串,每一个文档有一个文档ID,如果没有自己指定ID,系统会自动生成一个ID,文档的index/id必须是唯一的,作用相当于mysql中的行
5、Field:字段
一个文档会包含多个字段,每个字段都对应一个字段类型,类似于mysql中的列,可通过mapping设置文档的各个字段及其类型
五、文档操作
以下操作均基于7.X以后的Api
添加
API:POST //_doc/
this.esService.index({
index: 'test',
type: '_doc',
body: {
},
})
检索文档
从索引中检索指定的JSON文档
API:GET /_doc/<_id>
this.esService.get({
index: 'test',
id: 'fgBvd4UBTBV8j2Kcg1q_',
});
搜索
API:GET //_search
this.esService.search({
index: 'test',
body: {
query: {
term: {
_id: { value: 'fgBvd4UBTBV8j2Kcg1q_' },
},
},
},
});
更新
API:POST //_update/<_id>
this.esService.update({
index: 'test',
id: 'fgBvd4UBTBV8j2Kcg1q_',
body: {
doc: {
},
},
});
删除
API:DELETE //_update/<_id>
this.esService.delete({
index: 'test',
id: 'fgBvd4UBTBV8j2Kcg1q_',
});