索引创建好之后,就可以插入数据,在Elasticsearch中,这样的数据叫document。

官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

Create Document(创建文档)

创建文档的命令:

curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国国歌","date":"2021-10-15T13:12:00", "counts":1}'

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

Get Document(获取文档)

通过上面返回的_id获取文档的命令:

curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_doc/njKfHIQB8xpN1Xtdv241?pretty

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "key" : "中华人民共和国国歌",
    "date" : "2021-10-15T13:12:00",
    "counts" : 1
  }
}

Delete Document(删除文档)

通过_id删除文档:

curl --cacert http_ca.crt -u elastic -XDELETE https://localhost:9200/test_record/_doc/njKfHIQB8xpN1Xtdv241?pretty

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

删除之后再使用上面的Get命令就获取不到了,会返回下面的内容:

{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "found" : false
}

0

本文为原创文章,转载请注明出处,欢迎访问作者网站(和而不同)

发表评论

error: Content is protected !!