安装
Linux安装示例
-
- 下载
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
-
- 解压
tar -xvf elasticsearch-7.2.0-linux-x86_64.tar.gz
-
- 配置
配置文件位于config/elasticsearch.yml,部分配置说明如下:
- cluster.name 集群名称,以此作为是否同一集群的判断条件
- node.name 节点名称,以此作为集群中不同节点的区分条件
- network.host/http.port 网络地址和端口,用于http和transport服务使用
- path.data 数据存储地址
- path.log 日志存储地址
修改参数的第二种方式
启动时在启动命令后跟需要修改的参数信息,如下:
- 配置
./elasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_name
-
- 单节点启动
# 前台启动
/bin/elasticsearch
# 后台运行
/bin/elasticsearch -d
-
- 简单集群启动
bin/elasticsearch -Ehttp.port=9200 -Epath.data=master -Enode.name=master
bin/elasticsearch -Ehttp.port=9201 -Epath.data=slave_1 -Enode.name=node_salve_1
bin/elasticsearch -Ehttp.port=9202 -Epath.data=slave_2 -Enode.name=node_salve_2
集群管理
Elasticsearch提供了一个非常全面和强大的REST API,可以使用它与集群进行交互。使用API可以完成以下操作:
- 检查群集,节点和索引运行状况,状态和统计信息
- 管理您的群集,节点和索引数据和元数据
- 对索引执行CRUD(创建,读取,更新和删除)和搜索操作
- 执行高级搜索操作,例如分页,排序,过滤,脚本编写,聚合等等
REST API
健康检查
kibana console
GET /_cat/health?v
curl
curl -XGET "http://localhost:9200/_cat/health?v"
响应
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1564112148 03:35:48 es-cluster green 3 3 10 5 0 0 0 0 - 100.0%
- status
- Green:一切都很好(集群功能齐全)
- Yellow:所有数据都可用,但尚未分配一些副本(群集功能齐全)
- Red:某些数据由于某种原因不可用(群集部分功能)
注意:当群集为Red时,它将继续提供来自可用分片的搜索请求,但您可能需要尽快修复它,因为存在未分配的分片。
查看节点信息
kibana console
GET /_cat/nodes?v
curl
curl -XGET "http://localhost:9200/_cat/nodes?v"
响应
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 16 98 16 2.97 mdi - node_salve_2
127.0.0.1 20 98 16 2.97 mdi * master
127.0.0.1 27 98 16 2.97 mdi - node_salve_1
创建索引
kibana console
PUT /user
curl
curl -XPUT "http://localhost:9200/user"
响应
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "user"
}
列出所有索引信息
kibana console
GET /_cat/indices?v
curl
curl -XGET "http://localhost:9200/_cat/indices?v"
响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open user pmN9spFrQautZJI-xym1QA 1 1 0 0 460b 230b
- pri 主分片数
- rep 副本数
创建文档
kibana console
PUT /user/_doc/1
{
"name": "white colde",
"age":18
}
curl
curl -XPUT "http://localhost:9200/user/_doc/1" -H 'Content-Type: application/json' -d'{ "name": "white colde", "age":18}'
1为文档的唯一id,可不指定,当不指定时,系统会随机生产一个字符串作为文档的id
响应
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
修改数据
- 方式一
kibana console
PUT /user/_doc/1
{
"name": "white colde",
"age":19
}
curl
curl -XPUT "http://localhost:9200/user/_doc/1" -H 'Content-Type: application/json' -d'{ "name": "white colde", "age":19}'
此时id必须指定,如果id对应的文档不存在,则为新建
响应
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
- 方式二
kibana console
POST /user/_update/1
{
"doc":{
"name":"whtie fisher",
"age":21,
"school":"uc"
}
}
curl
curl -XPOST "http://localhost:9200/user/_update/1" -H 'Content-Type: application/json' -d'{ "doc":{ "name":"whtie fisher", "age":21, "school":"uc" }}'
响应
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
- 脚本更新
kibana console
POST /user/_update/1
{
"script":"ctx._source.age+=5"
}
curl
curl -XPOST "http://localhost:9200/user/_update/1" -H 'Content-Type: application/json' -d'{ "script":"ctx._source.age+=5"}'
ctx._source指的是即将更新的当前源文档
查询文档
- 使用ID进行查询
kibana console
GET /user/_doc/1
curl
curl -XGET "http://localhost:9200/user/_doc/1"
响应
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "white colde",
"age" : 18
}
}
删除索引
kibana console
DELETE /user
curl
curl -XDELETE "http://localhost:9200/user"
{
"acknowledged" : true
}
批量操作
kibana console
POST /user/_bulk?pretty
# 指定id,如果当前索引中存在文档,则执行更新操作
{"index":{"_id":"1"}}
{"name":"colde1","age":21}
# 未指定id,系统随机生产一个字符串作为文档id
{"index":{}}
{"name":"colde2","age":28}
# 删除文档id为2的文档
{"delete":{"_id":"2"}}
curl
curl -XPOST "http://localhost:9200/user/_bulk?pretty" -H 'Content-Type: application/json' -d'{"index":{"_id":"1"}}{"name":"colde1","age":21}{"index":{}}{"name":"colde2","age":28}{"delete":{"_id":"2"}}'
响应
{
"took" : 47,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 8,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 19,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : {
"_index" : "user",
"_type" : "_doc",
"_id" : "bDEQLWwBhMGMKE1AFwmS",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 20,
"_primary_term" : 1,
"status" : 201
}
}
]
}
搜索文档
匹配所有文档
- 方式一
kibana console
GET /user/_search?q=*&sort=age:desc&size=10&from=0
curl
curl -XGET "http://localhost:9200/user/_search?q=*&sort=age:desc&size=10&from=0"
请求说明
- q 需要匹配的关键字
- sort 排序字段
- size 返回Y条数据
- from 跳过前X条数据
- 匹配所有文档方式二
kibana console
GET /user/_search
{
"query": { "match_all": {} },
"size":10,
"from":0,
"sort": [
{ "age": "asc" }
]
}
curl
curl -XGET "http://localhost:9200/user/_search" -H 'Content-Type: application/json' -d'{ "query": { "match_all": {} }, "size":10, "from":0, "sort": [ { "age": "asc" } ]}'
响应
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "aTENLWwBhMGMKE1ArAnX",
"_score" : null,
"_source" : {
"name" : "white",
"age" : 212
},
"sort" : [
212
]
},
{
"_index" : "user",
"_type" : "_doc",
"_id" : "ajEOLWwBhMGMKE1ALAko",
"_score" : null,
"_source" : {
"name" : "white",
"age" : 212
},
"sort" : [
212
]
},
{
"_index" : "user",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "colde2"
},
"sort" : [
9223372036854775807
]
}
]
}
}
响应说明
- took 执行搜索的时间(单位毫秒)
- timed_out 搜索是否超市
- _shards 搜索分片总量,搜索成功/失败分片的数量
- hits 搜索结果
- hits.total 匹配搜索条件的文档总数
- hits.hits 实际的搜索结果数组
- hits.sort 为每个结果排序键的排序值
- hits.hits._score 查询匹配度,分数越高,文档越相关,分数越低,文档的相关性越低。(此处未传入对应的匹配条件,_score未null)
指定返回字段
GET /user/_search
{
"query": { "match_all": {} },
"_source": ["name"]
}
# 相当于SQL
SELECT NAME FROM USER
返回source中只包含name,不显示age字段
{
"_index" : "user",
"_type" : "_doc",
"_id" : "azEPLWwBhMGMKE1AvgkV",
"_score" : 1.0,
"_source" : {
"name" : "colde3"
}
}
匹配搜索查询
GET /user/_search
{
"query": {
"match": {
"name": "white blank"
}
}
}
es首先会对匹配条件“white blank”进行分词,拆分为“white”和“blank”,最终返回包含white或blank的文档
短语匹配查询
GET /user/_search
{
"query": {
"match_phrase": {
"name": "white blank"
}
}
}
match_phrase不会对匹配条件“white blank进行分词”,只会返回包含“white blank”这个短语的文档
bool查询
- must
GET /user/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "white" } },
{ "match": { "name": "blank" } }
]
}
}
}
返回同时包含“white”和“blank”的文档
- should
GET /user/_search
{
"query": {
"bool": {
"should": [
{ "match": { "name": "white" } },
{ "match": { "name": "blank" } }
]
}
}
}
返回包含“white”或“blank”的文档,等价于match white blank
- must_not
GET /user/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "name": "white" } },
{ "match": { "name": "blank" } }
]
}
}
}
返回既不包含“white”也不包含“blank”的文档
- must and must_not
GET /user/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "white" } }
],
"must_not": [
{ "match": { "age": 21 } }
]
}
}
}
返回name包含“white”并且age不等于21的文档
过滤器
范围查询
GET /user/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"age": {
"gte": 21,
"lte": 29
}
}
}
}
}
}
返回must匹配条件数据(这里是match_all),在结果集中执行过滤器规则返回age大于等于21并且小于等于29的所有用户
聚合查询
GET /user/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "name.keyword"
}
}
}
}
返回统计同名用户数量
GET /user/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "name.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "age"
}
}
}
}
}
}
返回统计同名用户数量和计算同名用户的平均年龄