Elasticsearch REST API
1.集群
1.1.查看集群健康值
REST API
GET _cat/health?v
使用curl请求,其他API请求方式相同
curl http://127.0.0.1:9200/_cat/health?v
有用户认证的请求方式
curl -u dd http://127.0.0.1:9200/_cat/health?v
1.2.查看集群节点信息(包含ES版本号)
GET _nodes/settings
1.3.查看节点负载
GET _cat/nodes?v
1.4.查看线程池
GET _cat/thread_pool?v
可以用h参数查看指定内容
GET _cat/nodes?v&h=name,ram.percent,ram.max,heap.current,heap.max,disk.used,disk.total
列名 | 简写 | 说明 |
---|---|---|
ip | i | ip地址。 |
heap.percent | hp,heapPercent | 堆使用百分比。 |
ram.percent | rp,ramPercent | 机器内存使用百分比。 |
cpu | cpu | 最近的cpu使用率。 |
load_1m | l | 最近一分钟平均负载。 |
load_5m | l | 最近五分钟平均负载。 |
load_15m | l | 最近十五分钟平均负载。 |
node.role | r,role,nodeRole | 节点角色,m:主节点,d:数据节点,i:提取节点,-:协调节点。 |
master | m | *:当前为主节点,-:当前不是主节点。 |
name | n | 节点名。 |
id | nodeId | 唯一的节点id。 |
pid | p | 进程id。 |
port | po | 绑定的传输端口。 |
http_address | http | 绑定的http地址。 |
version | v | es版本。 |
flavor | f | es发布包类型。 |
type | t | es发布包文件类型。 |
build | b | es构建哈希值。 |
jdk | j | jdk版本。 |
disk.total | dt,diskTotal | 磁盘总量。 |
disk.used | du,diskUsed | 已使用磁盘。 |
disk.avail | d,da,disk,diskAvail | 可用磁盘量。 |
disk.used_percent | dup,diskUsedPercent | 磁盘使用百分比。 |
heap.current | hc,heapCurrent | 已使用堆大小。 |
heap.max | hm,heapMax | 配置的最大堆大小。 |
ram.current | rc,ramCurrent | 机器已使用内存。 |
ram.max | rm,ramMax | 机器总内存。 |
file_desc.current | fdc,fileDescriptorCurrent | 使用的文件描述符数量。 |
file_desc.max | fdm,fileDescriptorMax | 文件描述符最大数量。 |
file_desc.percent | fdp,fileDescriptorPercent | 使用的文件描述符百分比。 |
uptime | u | 节点正常运行时间。 |
completion.size | cs,completionSize | 自动补全占用内存大小。 |
fielddata.memory_size | fm,fielddataMemory | fielddata占用内存大小。 |
fielddata.evictions | fe,fielddataEvictions | 被回收词根的个数。 |
query_cache.memory_size | qcm,queryCacheMemory | 查询缓存占用内存大小。 |
query_cache.evictions | qce,queryCacheEvictions | 被回收的查询缓存总数。 |
request_cache.memory_size | rcm,requestCacheMemory | 请求缓存占用内存大小。 |
request_cache.evictions | rce,requestCacheEvictions | 被回收的请求缓存总数。 |
request_cache.hit_count | rchc,requestCacheHitCount | 请求缓存命中数。 |
request_cache.miss_count | rcmc,requestCacheMissCount | 请求缓存未命中数。 |
flush.total | ft,flushTotal | 磁盘刷新次数。 |
flush.total_time | ftt,flushTotalTime | 磁盘刷新总用时。 |
get.current | gc,getCurrent | 当前正在执行的get操作的数量。 |
get.time | gti,getTime | get操作总耗时。 |
get.total | gto,getTotal | get操作总次数。 |
get.exists_time | geti,getExistsTime | 命中的get操作总耗时。 |
get.exists_total | geto,getExistsTotal | get操作命中的总次数。 |
get.missing_time | gmti,getMissingTime | 未命中的get操作总耗时。 |
get.missing_total | gmto,getMissingTotal | get操作未命中的总次数。 |
indexing.delete_current | idc,indexingDeleteCurrent | 当前正在执行的删除操作的数量。 |
indexing.delete_time | idti,indexingDeleteTime | 删除操作总耗时。 |
indexing.delete_total | idto,indexingDeleteTotal | 删除操作总次数。 |
indexing.index_current | iic,indexingIndexCurrent | 当前正在执行的索引操作的数量。 |
indexing.index_time | iiti,indexingIndexTime | 索引操作总耗时。 |
indexing.index_total | iito,indexingIndexTotal | 索引操作总次数。 |
indexing.index_failed | iif,indexingIndexFailed | 索引操作失败的次数。 |
merges.current | mc,mergesCurrent | 当前正在执行的合并操作的数量。 |
merges.current_docs | mcd,mergesCurrentDocs | 正在合并的文档数。 |
merges.current_size | mcs,mergesCurrentSize | 正在合并的文档大小。 |
merges.total | mt,mergesTotal | 合并操作总次数。 |
merges.total_docs | mtd,mergesTotalDocs | 合并操作的文档总数。 |
merges.total_size | mts,mergesTotalSize | 合并操作的文档总大小。 |
merges.total_time | mtt,mergesTotalTime | 合并操作的总耗时。 |
refresh.total | rto,refreshTotal | 索引刷新总次数。 |
refresh.time | rti,refreshTime | 索引刷新总耗时。 |
refresh.external_total | rto,refreshTotal | 外部刷新总次数。 |
refresh.external_time | rti,refreshTime | 外部刷线总耗时。 |
refresh.listeners | rli,refreshListeners | 挂起的刷新监听器数量。 |
script.compilations | scrcc,scriptCompilations | 编译脚本数。 |
script.cache_evictions | scrce,scriptCacheEvictions | 被回收的脚本缓存数。 |
script.compilation_limit_triggered | scrclt,scriptCacheCompilationLimitTriggered | 触发脚本编译限制的数量。 |
search.fetch_current | sfc,searchFetchCurrent | 当前处于fetch阶段的操作的数量。 |
search.fetch_time | sfti,searchFetchTime | fetch阶段总耗时。 |
search.fetch_total | sfto,searchFetchTotal | fetch操作总次数。 |
search.open_contexts | so,searchOpenContexts | 打开的查询上下文数量。 |
search.query_current | sqc,searchQueryCurrent | 当前处于query阶段的操作的数量。 |
search.query_time | sqti,searchQueryTime | query阶段总耗时。 |
search.query_total | sqto,searchQueryTotal | query操作总次数。 |
search.scroll_current | scc,searchScrollCurrent | 当前正在执行的scroll操作的数量。 |
search.scroll_time | scti,searchScrollTime | scroll操作的总耗时。 |
search.scroll_total | scto,searchScrollTotal | scroll操作的总次数。 |
segments.count | sc,segmentsCount | 索引当前的段数。 |
segments.memory | sm,segmentsMemory | 段使用内存。 |
segments.index_writer_memory | siwm,segmentsIndexWriterMemory | IndexWriter使用的内存。 |
segments.version_map_memory | svmm,segmentsVersionMapMemory | 版本映射使用的内存。 |
segments.fixed_bitset_memory | sfbm,fixedBitsetMemory | 用于嵌套对象字段类型和_parent字段中引用类型的类型过滤器的固定位集使用的内存。 |
suggest.current | suc,suggestCurrent | 当前正在执行的suggest操作的数量。 |
suggest.time | suti,suggestTime | suggest操作的总耗时。 |
suggest.total | suto,suggestTotal | suggest操作的总次数。 |
1.5.查看节点统计信息
GET _nodes/stats
1.6.查看当前执行的任务
GET _cat/tasks?v
1.7.查看活跃线程
GET _nodes/hot_threads?type=cpu&interval=10s
2.查看所有索引
GET _cat/indices?v
- v:显示列名
按文档数量排序
GET _cat/indices?s=docs.count
按索引大小排序
curl http://localhost:9200/_cat/indices?bytes=b | sort -rnk9 |grep -v ?v
3.查看索引结构(mapping)
GET /user/_mapping?pretty
- pretty:返回结果的格式美观易读
4.创建索引
索引名规则:
- 索引名不能包含以下字符:
\ / * ? " < > | 空格 , #
- 7.0以前可以包含冒号,7.0之后不支持
- 不能以
- _ +
开头 - 不能是
.
或..
- 最大长度为255字节
4.1.只创建索引
REST APT
PUT myIndex
使用curl请求
curl -X PUT http://127.0.0.1:9200/user
4.2.创建索引时指定mapping
PUT myIndex
{
"mappings": {
"user": {
"properties": {
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" }
}
}
}
}
4.3.Elasticsearch 7.x创建索引
Elasticsearch 7.x去除了type,API仍然兼容老的方式,但需要增加include_type_name
参数,否则会报错,include_type_name
或include_type_name=true
都可以,不加参数默认为false。
PUT myIndex?include_type_name
{
"mappings": {
"user": {
"properties": {
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" }
}
}
}
}
不包含type方式(推荐),由于8.x在API层面也去除了type,不兼容老的方式,所以建议使用该方式创建索引。
PUT myIndex
{
"mappings": {
"properties": {
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" }
}
}
}
4.4.创建索引时手动指定索引配置
PUT no_replicas
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
5.创建mapping
mapping可以在创建时指定,也可以在索引创建之后指定。
PUT /myIndex/myType/_mapping
{
"properties": {
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
6.字段类型
6.1.keyword
keyword类型不会对值进行分词,该类型的字段只用来排序、聚合,如邮箱地址、手机号、邮编等。keyword类型的字段只能被精确搜索,不能模糊搜索。
6.2.text
text类型的字段,用来存放文本类数据。该类型字段在被索引之前,整个字符串会被分词器分成多个关键词,搜索时,可以对关键词进行搜素。text类型字段不能排序,也很少用来聚合。可以通过analyzer
属性来指定字段的分词器,如ik分词器。
"mappings":{
"my_type":{
"properties":{
"title": {
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
7.聚合查询
查询字段中各值的数量
POST myIndex/_search?pretty
{
"size":0,
"aggs":{
"group_by_source":{
"terms":{
"field":"fieldName"
}
}
}
}
8.Ingest pipelines
8.1.将已有索引通过pipelines处理并刷新数据
POST myIndex/_update_by_query?pipeline=my-pipeline
9.搜索
9.1.搜索指定的type(V7以下版本)
ES V7以下版本存在type,V7以上版本,都固定为_doc,但兼容老的API,type处可填_doc(和不填一样效果)
GET /myIndex/myType/_search
10.打开关闭索引
打开索引
POST /myIndex/_open
关闭索引
POST /myIndex/_close