Linux 系统下操作 ElasticSearch 集群,有很多操作命令,现在将常用的命令分类总结如下。

  ElasticSearch 集群可以包含多个索引(indices),每一个索引可以包含多个类型(types),每一个类型包含多个文档(documents),每个文档包含多个字段(Fields)。

  在 ElasticSearch 6.x 版本中已经只允许一个索引下只有一个 type,声明多个 type 已经标记为过期,但是仍可以使用。7.0 之后的版本已完全移除 type 的概念。

  如果将 ElasticSearch 和 MySQL 数据库类比的话,ES 的索引 index 相当于 MySQL 的数据库 database,type 相当于 table,document 相当于表的记录,field 相当于 column。

一、集群操作命令
  1. 查看集群状态
[estestuser@vm-10-20-42-19 ~]$ curl -u elastic http://localhost:9200/_cat/health?v
Enter host password for user 'elastic':
epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1614674552 08:42:32  my-application green           3         3      4   2    0    0        0             0                  -                100.0%

   green 表明集群是健康状态。

  1. 修改集群访问密码
    在集群的一个节点修改后,会自动同步到其他节点。将 elastic 用户的密码更新为 “new_password” 的命令如下。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" --user elastic -XPUT 'http://localhost:9200/_xpack/security/user/elastic/_password?pretty' -d '{"password":"new_password"}'
Enter host password for user 'elastic':
{ }
  1. 查看集群中的节点状态
[estestuser@vm-10-20-42-19 eslogs]$ curl -u elastic http://localhost:9200
Enter host password for user 'elastic':
{
  "name" : "node-2",
  "cluster_name" : "my-application",
  "cluster_uuid" : "l-kTWdoxRCuTIm6x8ekG7w",
  "version" : {
    "number" : "7.1.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "7a013de",
    "build_date" : "2019-05-23T14:04:00.380842Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

  说明节点 2 是存活的。

二、索引操作命令
  1. 查看索引列表
[estestuser@vm-10-20-42-19 ~]$ curl -u elastic http://localhost:9200/_cat/indices?v
Enter host password for user 'elastic':
health status index       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security-7 7py8w_piQMqXbzlQuJgCSg   1   1          6            0     39.7kb         19.8kb
green  open   testmapping G4oOQ4HdRMW01useLwDW7w   1   1          0            0       566b           283b

  说明共有 .security-7 和 testmapping 两个索引。

  1. 删除索引
    将索引 testmapping 删除的命令如下。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" -u elastic -XDELETE http://localhost:9200/testmapping
Enter host password for user 'elastic':
{"acknowledged":true}
  1. 创建索引
    ElasticSearch 索引名字须为小写。创建名为 testIndex 的索引会报错,如下所示。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" -u elastic -XPUT http://localhost:9200/testIndex
Enter host password for user 'elastic':
{"error":{"root_cause":[{"type":"invalid_index_name_exception","reason":"Invalid index name [testIndex], must be lowercase","index_uuid":"_na_","index":"testIndex"}],"type":"invalid_index_name_exception","reason":"Invalid index name [testIndex], must be lowercase","index_uuid":"_na_","index":"testIndex"},"status":400} 

  修改索引名为小写,创建索引语句如下,提示创建成功。

[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" -u elastic -XPUT http://localhost:9200/testindex
Enter host password for user 'elastic':
{"acknowledged":true,"shards_acknowledged":true,"index":"testindex"}
  1. 查看索引结构
    创建完索引后,查询索引 testindex 包含哪些 field 的命令如下所示。
[estestuser@vm-10-20-42-19 ~]$ curl -XGET -u elastic http://localhost:9200/testindex/_mapping?pretty
Enter host password for user 'elastic':
{
  "testindex" : {
    "mappings" : { }
  }
}

  说明索引 testindex 目前没有任何 filed。

  若查询一个不存在的索引(假定索引名为 testindex)结构,则会报错,如下所示。

[estestuser@vm-10-20-42-19 ~]$ curl -XGET -u elastic http://localhost:9200/testindex/_mapping?pretty
Enter host password for user 'elastic':
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [testindex]",
        "index_uuid" : "_na_",
        "resource.type" : "index_or_alias",
        "resource.id" : "testindex",
        "index" : "testindex"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [testindex]",
    "index_uuid" : "_na_",
    "resource.type" : "index_or_alias",
    "resource.id" : "testindex",
    "index" : "testindex"
  },
  "status" : 404
}
  1. 给索引设置 field
    给索引 testindex 设置 2 个 filed,名字分别为 mchnt_id 和 mchnt_name,语句如下所示。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" -u elastic -XPUT 'http://localhost:9200/testindex/_mapping?pretty' -d '{"properties":{"mchnt_id" : {"type" : "keyword","index" : false},"mchnt_name" : {"type" : "text"}}}'
Enter host password for user 'elastic':
{
  "acknowledged" : true
}
  1. 给索引新增字段
    给索引 testindex 新增一个 field,名字为 amount,语句如下所示。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type: application/json" -u elastic -XPOST 'http://localhost:9200/testindex/_mapping?pretty' -d '{"properties": {"amount":{"type":"integer"}}}'
Enter host password for user 'elastic':
{
  "acknowledged" : true
}
三、文档操作命令
  1. 添加文档
    给索引 testindex 添加一条文档,文档 ID 为 1,语句如下。
[estestuser@vm-10-20-42-19 ~]$ curl -u elastic -H "Content-Type: application/json" -XPOST 'http://localhost:9200/testindex/_doc/1?pretty' -d '{"mchnt_id": "1","mchnt_name": "鹿角巷"}'
Enter host password for user 'elastic':
{
  "_index" : "testindex",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
  1. 删除文档
    将 testindex 索引下,文档 ID 为 3 的文档删除,命令如下。
[estestuser@vm-10-20-42-9 ~]$ curl -u elastic -H "Content-Type: application/json" -XDELETE 'http://localhost:9200/testindex/_doc/3?pretty'
Enter host password for user 'elastic':
{
  "_index" : "testindex",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}
  1. 统计索引 testindex 下的文档数
[estestuser@vm-10-20-42-19 ~]$ curl -s --user elastic -XGET 'http://localhost:9200/_cat/indices/testindex?v' | awk -F ' ' {'print $7'} | grep -v docs.count
Enter host password for user 'elastic':
0
  1. 查看索引 testindex 下的全部数据
[estestuser@vm-10-20-42-19 ~]$ curl -u elastic -XGET 'http://localhost:9200/testindex/_search?pretty=true'
Enter host password for user 'elastic':
{
  "took" : 533,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "1",
          "mchnt_name" : "鹿角巷"
        }
      },
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "2",
          "mchnt_name" : "鹿角巷东方路"
        }
      },
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "3",
          "mchnt_name" : "鹿角巷民雷路"
        }
      }
    ]
  }
}
  1. 搜索 testindex 索引下的全部数据
[estestuser@vm-10-20-42-19 ~]$ curl --user elastic -XGET 'http://localhost:9200/testindex/_search?pretty' -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'
Enter host password for user 'elastic':
{
  "took" : 54,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

  上述结果为 0 条数据。

[estestuser@vm-10-20-42-19 ~]$ curl --user elastic -XGET 'http://localhost:9200/testindex/_search?pretty' -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'
Enter host password for user 'elastic':
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "1",
          "mchnt_name" : "鹿角巷"
        }
      },
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "2",
          "mchnt_name" : "鹿角巷东方路"
        }
      },
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "mchnt_id" : "3",
          "mchnt_name" : "鹿角巷民雷路"
        }
      }
    ]
  }
}

  插入几条数据后,再次搜索全部数据,发现有 3 条。

  1. 根据文档 ID 精准获取索引数据
    精准查询 testindex 索引下的文档 ID 为 20200634 的数据,命令如下。
[estestuser@vm-10-20-42-19 ~]$ curl -H "Content-Type:application/json" -u elastic -XGET http://localhost:9200/testindex/_doc/20200634?pretty
Enter host password for user 'elastic':
{
  "_index" : "testindex",
  "_type" : "_doc",
  "_id" : "20200634",
  "_version" : 3,
  "_seq_no" : 44704,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "mchnt_id" : "20200634",
    "mchnt_name" : "85度C成都凯德店"
  }
}
备注:

  若 ElasticSearch 集群添加了用户安全认证功能,curl 命令中的 “-u elastic” 代表以 elastic 用户访问 ElasticSearch 集群,也可以修改为 “- -user elastic”,命令回车后需要输入密码。

  若集群未添加用户安全认证,“-u elastic” 或 “- -user elastic” 可以缺省,回车后也无需输入密码。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐