修改index.js

const { Client } = require("@elastic/elasticsearch");
const client = new Client({ node: "http://localhost:9200" });

client.create({
    index: 'megacorp',  // 索引
    id: 1,              // id
    type: 'employee',   // 类型
    body: {             // 数据
        "first_name" : "John",
        "last_name" :  "Smith",
        "age" :        25,
        "about" :      "I love to go rock climbing",
        "interests": [ "sports", "music" ]
    }
}, function(error, data) {
  if (error) {
    console.error("client create error", error);
  } else {
    console.log("client create success ", data);
  }
});

创建一个index和type,id为1,存储的内容为body中的内容,执行node index.js后,返回结果如下:
屏幕快照 2019-06-28 下午5.42.55.png

从返回结果上看,应该是成功了,但是statusCode是201,我们不得不疑惑到底插入es了么?
下面我们在终端使用命令行查询:

curl 'localhost:9200/_cat/indices?v'

这个命令是查询当前es中的所有的index,结果如下:
屏幕快照 2019-06-28 下午5.44.53.png

我们看到,megacorp索引确实已经插入成功了,但是这个索引的健康状态是yellow,这个又是什么意思呢?

YELLOW

原因
yellow表示所有主分片可用,但不是所有副本分片都可用,最常见的情景是单节点时,由于es默认是有1个副本,主分片和副本不能在同一个节点上,所以副本就是未分配unassigned

处理
过滤查看所有未分配索引的方式, curl -s "http://localhost:9200/_cat/shards" | grep UNASSIGNED结果如下,第一列表示索引名,第二列表示分片编号,第三列p是主分片,r是副本:

megacorp                     2 p STARTED        0    261b 127.0.0.1 A6SkUYd
megacorp                     2 r UNASSIGNED                         
megacorp                     1 p STARTED        0    261b 127.0.0.1 A6SkUYd
megacorp                     1 r UNASSIGNED                         
megacorp                     3 p STARTED        1   5.6kb 127.0.0.1 A6SkUYd
megacorp                     3 r UNASSIGNED                         
megacorp                     4 p STARTED        0    261b 127.0.0.1 A6SkUYd
megacorp                     4 r UNASSIGNED                         
megacorp                     0 p STARTED        0    261b 127.0.0.1 A6SkUYd
megacorp                     0 r UNASSIGNED                         
.kibana_1                    0 p STARTED       49 130.3kb 127.0.0.1 A6SkUYd
kibana_sample_data_logs      0 p STARTED    14005  11.2mb 127.0.0.1 A6SkUYd
myindex                      2 p STARTED        0    261b 127.0.0.1 A6SkUYd
myindex                      2 r UNASSIGNED                         
myindex                      1 p STARTED        0    261b 127.0.0.1 A6SkUYd
myindex                      1 r UNASSIGNED                         
myindex                      4 p STARTED        0    261b 127.0.0.1 A6SkUYd
myindex                      4 r UNASSIGNED                         
myindex                      3 p STARTED        1   4.8kb 127.0.0.1 A6SkUYd
myindex                      3 r UNASSIGNED                         
myindex                      0 p STARTED        0    261b 127.0.0.1 A6SkUYd
myindex                      0 r UNASSIGNED                         
kibana_sample_data_flights   0 p STARTED    13059   6.3mb 127.0.0.1 A6SkUYd
.kibana_2                    0 p STARTED       50 102.3kb 127.0.0.1 A6SkUYd
articles_index               2 p STARTED        2  11.4kb 127.0.0.1 A6SkUYd
articles_index               2 r UNASSIGNED                         
articles_index               1 p STARTED        1   5.7kb 127.0.0.1 A6SkUYd
articles_index               1 r UNASSIGNED                         
articles_index               3 p STARTED        2    12kb 127.0.0.1 A6SkUYd
articles_index               3 r UNASSIGNED                         
articles_index               4 p STARTED        1   6.3kb 127.0.0.1 A6SkUYd
articles_index               4 r UNASSIGNED                         
articles_index               0 p STARTED        2   7.7kb 127.0.0.1 A6SkUYd
articles_index               0 r UNASSIGNED                         
kibana_sample_data_ecommerce 0 p STARTED     4675   4.6mb 127.0.0.1 A6SkUYd

如何查看索引中的内容

命令行中执行:

curl -XGET 'localhost:9200/megacorp/_search'

结果能看到:
屏幕快照 2019-06-28 下午5.44.53.png