ELK学习轨迹(3)———Elasticsearch的简单使用(上)

搭建完Elasticsearch后,现在开始逐步探索Elasticsearch的使用方法了,这里我会先记录一些简单的命令,下一篇将会对其进行更深入的探索。

Elasticsearch的简单命令


  • 查看elasticsearch的健康状态
curl -GET 127.0.0.1:9200/_cat/health?v  #加v可以显示参数的列名,下同
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1555329542 11:59:02  elasticsearch yellow          1         1     10  10    0    0        1             0                  -                 90.9%
  • 查看elasticsearch集群的健康状态
curl -GET 127.0.0.1:9200/_cluster/health?pretty   #加pretty可以美化json格式的结果,下同
{
  "cluster_name" : "elasticsearch",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 10,
  "active_shards" : 10,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 1,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 90.9090909090909
}
  • 查看节点状态
curl -GET 127.0.0.1:9200/_cat/nodes?v
ip             heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.19.244.188           45          96   5    0.25    0.11     0.07 mdi       *      izuf6bcs2gy0q80ienjn0yz
  • 查看索引状态
curl -GET 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   .kibana_task_manager            d52WbBQNRmK9_NB-ScHDYQ   1   0          2            0     46.3kb         46.3kb
green  open   .monitoring-es-7-2019.04.14     NY_SOwsATN-O-0wQECbB1w   1   0     100855        71917     47.1mb         47.1mb
green  open   .monitoring-kibana-7-2019.04.14 TrQK23IqQpSYKC_Sz9AAgw   1   0       8638            0      2.6mb          2.6mb
green  open   kibana_sample_data_flights      9fQjpTomTniy5QPeembbWg   1   0      13059            0      6.5mb          6.5mb
green  open   .monitoring-kibana-7-2019.04.13 0ZCf0_5sSpmbB1VdqtOaAA   1   0       3370            0        1mb            1mb
yellow open   test_1                          KsgH0IWVTdO_YIYVTjlVOQ   1   1          6            0     28.1kb         28.1kb
green  open   .monitoring-es-7-2019.04.15     AN3Gu6DPTKK4fJ-eh0X0lw   1   0      61779        48444     30.6mb         30.6mb
green  open   .monitoring-kibana-7-2019.04.15 2ec38z9ORxmgB3cNh67rLg   1   0       4397            0      1.5mb          1.5mb
green  open   .monitoring-es-7-2019.04.13     dGLxQnlVRjapWHqP2esY9g   1   0      30338        16840     14.1mb         14.1mb
green  open   .kibana_1                       3A228HM4R96hTgv4rZI9bg   1   0         72            5    150.7kb        150.7kb
  • 新增test_2索引
curl -XPUT 127.0.0.1:9200/test_2?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_2"
}
  • 新增一条记录(类型为sample,ID为1)
curl -XPUT 127.0.0.1:9200/test_2/sample/1?pretty -H 'Content-Type:application/json' -d '{"name":"xiaoming","age":23}'
{
  "_index" : "test_2",
  "_type" : "sample",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
  • 查询索引内的所有记录
 curl -XGET 127.0.0.1:9200/test_2/sample/_search?pretty
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaoming",
          "age" : 23
        }
      }
    ]
  }
}
  • 按条件查询索引内的记录
 curl -XGET 127.0.0.1:9200/test_2/sample/_search?pretty&q=name:xiaoming
 {
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaoming",
          "age" : 23
        }
      }
    ]
  }
}

  • 覆盖记录(若没有记录则新增)
curl -XPOST 127.0.0.1:9200/test_2/sample/1?pretty -H 'Content-Type:application/json' -d '{"name":"xiaohuang","sex":"男","age":24}
{
  "_index" : "test_2",
  "_type" : "sample",
  "_id" : "1",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 17,
  "_primary_term" : 2
}
 curl 127.0.0.1:9200/test_2/_search?pretty
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaohuang",
          "sex" : "男",
          "age" : 24
        }
      }
    ]
  }
}
  • 更新记录
curl -XPOST 127.0.0.1:9200/test_2/sample/1/_update?pretty -H 'Content-Type:application/json' -d '{"doc":{"name":"xiaohong"}}'
{
  "_index" : "test_2",
  "_type" : "sample",
  "_id" : "1",
  "_version" : 8,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 18,
  "_primary_term" : 2
}
curl 127.0.0.1:9200/test_2/_search?pretty
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
       "_source" : {
          "name" : "xiaohong",
          "sex" : "男",
          "age" : 24
        }
      }
    ]
  }
}
  • 批量操作(bulk)
 curl -XPOST 127.0.0.1:9200/test_2/sample/_bulk?pretty -H 'Content-Type:application/json' -d '
{"index":{"_id":"2"}}
{"name":"xiaohei","age":22}
{"index":{"_id":"3"}}
{"name":"xiaohuang","age":23}
{"update":{"_id":"1"}}
{"doc":{"name":"xiaoming"}}
> '
{
  "took" : 29,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 23,
        "_primary_term" : 2,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 24,
        "_primary_term" : 2,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_version" : 9,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 25,
        "_primary_term" : 2,
        "status" : 200
      }
    }
  ]
}
curl 127.0.0.1:9200/test_2/_search?pretty
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaohei",
          "age" : 22
        }
      },
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaohuang",
          "age" : 23
        }
      },
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaoming",
          "sex" : "男",
          "age" : 24
        }
      }
    ]
  }
}
  • 删除记录
curl -XDELETE 127.0.0.1:9200/test_2/sample/3?pretty
{
  "_index" : "test_2",
  "_type" : "sample",
  "_id" : "3",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 32,
  "_primary_term" : 2
}
curl 127.0.0.1:9200/test_2/_search?pretty
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaohei",
          "age" : 22
        }
      },
      {
        "_index" : "test_2",
        "_type" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaoming",
          "sex" : "男",
          "age" : 24
        }
      }
    ]
  }
}

先记录这些简单的语句,后面会慢慢更新。共勉!

你可能感兴趣的:(ELK学习轨迹(3)———Elasticsearch的简单使用(上))