Elasticsearch 入门教程

查看_cat 接口有哪些选项

curl  http://127.0.0.1:9200/_cat/

输出如下内容

=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
/_cat/component_templates/_cat/transforms
/_cat/transforms/{transform_id}
/_cat/ml/anomaly_detectors
/_cat/ml/anomaly_detectors/{job_id}
/_cat/ml/trained_models
/_cat/ml/trained_models/{model_id}
/_cat/ml/datafeeds
/_cat/ml/datafeeds/{datafeed_id}
/_cat/ml/data_frame/analytics
/_cat/ml/data_frame/analytics/{id} 

查看集群的健康状态

curl  http://127.0.0.1:9200/_cat/health
1679039470 07:51:10 my-es green 3 3 24 11 0 0 0 0 - 100.0%

curl http://127.0.0.1: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 1679039570 07:52:50 my-es green 3 3 24 11 0 0 0 0 - 100.0%

 

状态说明:

Green - everything is good (cluster is fully functional),即最佳状态

Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即数据和集群可用,但是集群的备份有的是坏的

Red - some data is not available for whatever reason (cluster is partially functional),即数据和集群都不可用

查看集群的节点

 curl  http://127.0.0.1:9200/_cat/nodes?v
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role   master name
10.2.2.10            8          94   1    0.01    0.25     0.43 cdfhilmrstw -      node-2
10.2.2.10           31          94   1    0.01    0.25     0.43 cdfhilmrstw -      node-3
10.2.2.10           22          94   1    0.01    0.25     0.43 cdfhilmrstw *      node-1

 

查看索引

curl  http://127.0.0.1:9200/_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   students u0fLcFxSQaqkTDV2qoW6Jg   2   2          0            0      1.3kb           450b
green  open   index    PzoWg3TjS52SZG5btfRDhw   1   1          4            0     10.5kb          5.2kb  

创建索引

创建一个名为twitter的索引,设置索引的分片数为3,副本数为2。注意:在ES中创建一个索引类似于在数据库中建立一个数据库(ES6.0之后类似于创建一个表)

 

 

 创建mapping映射

 注意:在ES中创建一个mapping映射类似于在数据库中定义表结构,即表里面有哪些字段、字段是什么类型、字段的默认值等;也类似于solr里面的模式schema的定义

 

 查看索引信息

 GET /customer

 查看索引的settings

 GET /customer/_settings

 查看索引的mapping

 GET /customer/_mapping

  删除索引

  DELETE /customer 

  判断索引是否存在    

  HEAD /customer   HTTP status code 表示结果 404 不存在 , 200 存在

  修改索引的settings信息

  索引的设置信息分为静态信息和动态信息两部分。静态信息不可更改,如索引的分片数。动态信息可以修改。 REST 访问端点: /_settings 更新所有索引的。 {index}/_settings 更新一个或多个索引的      settings。

  修改索引备份数

PUT /customer/_settings
{
  "number_of_replicas": 2
}

设置默认值,用null

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : null
    }
}

设置索引的读写

index.blocks.read_only:设为true,则索引以及索引的元数据只可读
index.blocks.read_only_allow_delete:设为true,只读时允许删除。
index.blocks.read:设为true,则不可读。
index.blocks.write:设为true,则不可写。
index.blocks.metadata:设为true,则索引元数据不可读写。

索引模板

在创建索引时,为每个索引写定义信息可能是一件繁琐的事情,ES提供了索引模板功能,让你可以定义一个索引模板,模板中定义好settings、mapping、以及一个模式定义来匹配创建的索引。 注意:模板只在索引创建时被参考,修改模板不会影响已创建的索引

创建模板   创建名字为template_1的模板,匹配名称为te* 或 bar*的索引

PUT /_template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  }
}

查看索引模板

GET /_template/template_1
GET /_template/temp* 
GET /_template/template_1,template_2
GET /_template

删除模板

DELETE /_template/template_1

打开/关闭索引

POST /my_index/_close
POST /my_index/_open

说明: 关闭的索引不能进行读写操作,几乎不占集群开销。 关闭的索引可以打开,打开走的是正常的恢复流程。

收缩索引

索引的分片数是不可更改的,如要减少分片数可以通过收缩方式收缩为一个新的索引。新索引的分片数必须是原分片数的因子值,如原分片数是8,则新索引的分片数可以为4、2、1 。

什么时候需要收缩索引呢?

最初创建索引的时候分片数设置得太大,后面发现用不了那么多分片,这个时候就需要收缩了

收缩的流程:

  • 先把所有主分片都转移到一台主机上;
  • 在这台主机上创建一个新索引,分片数较小,其他设置和原索引一致;
  • 把原索引的所有分片,复制(或硬链接)到新索引的目录下;
  • 对新索引进行打开操作恢复分片数据;
  • (可选)重新把新索引的分片均衡到其他节点上。

 

参考地址 https://cloud.tencent.com/developer/article/1547867

 

posted @ 2023-03-17 15:59  羊脂玉净瓶  阅读(41)  评论(0)    收藏  举报