Elasticsearch(四)---文档管理

新建文档:

# curl -XPOST node3:9200/blog/article/1?pretty -d '{
"id":1,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "1",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

如果不在url中指定ID的话,系统会自动生成:

# curl -XPOST node3:9200/blog/article?pretty -d '{
"id":1,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "AW177VG-fklqH1cpWhsC",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

获取文档:

# curl node2:9200/blog/article/1?pretty
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "id" : 1,
    "title" : "Git 简介",
    "posttime" : "2017-05-01",
    "content" : "Git是一款免费、开源的分布式版本控制系统"
  }
}

查找一个不存在的ID:

# curl node1:9200/blog/article/100?pretty
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "100",
  "found" : false
}

可以通过HEAD的提交方式快速查看是否有相关的记录:

# curl --head node2:9200/blog/article/100?pretty
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0

# curl --head node2:9200/blog/article/1?pretty
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0

新建两个文档:

# curl -XPOST node3:9200/blog/article/101?pretty -d '{
"id":101,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "101",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}
# curl -XPOST node3:9200/blog/article/102?pretty -d '{
"id":102,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "102",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

根据id获取多个文档:

# curl node2:9200/_mget?pretty -d '{
"docs":[
{
"_index":"blog",
"_type":"article",
"_id":"1"
},
{
"_index":"blog",
"_type":"article",
"_id":"101"
},
{
"_index":"blog",
"_type":"article",
"_id":"102"
}
]
}'
{
  "docs" : [ {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 1,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  }, {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "101",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 101,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  }, {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "102",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 102,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  } ]
}

同一个索引下的不同类型,可以简写为:

# curl node3:9200/blog/_mget?pretty -d '{
"docs":[
{
"_type":"article",
"_id":"1"
},
{
"_type":"article",
"_id":"102"
}
]
}'
{
  "docs" : [ {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 1,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  }, {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "102",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 102,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  } ]
}

如果类型和索引都相同,则可以写为:

# curl node3:9200/blog/article/_mget?pretty -d '{
"docs":[
{"_id":"1"},
{"_id":"102"}
]
}'
{
  "docs" : [ {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 1,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  }, {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "102",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 102,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  } ]
}

进一步简化:

# curl node3:9200/blog/article/_mget?pretty -d '{
"ids":["101","102"]
}'
{
  "docs" : [ {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "101",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 101,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  }, {
    "_index" : "blog",
    "_type" : "article",
    "_id" : "102",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "id" : 102,
      "title" : "Git 简介",
      "posttime" : "2017-05-01",
      "content" : "Git是一款免费、开源的分布式版本控制系统"
    }
  } ]
}

更新文档

文档更新,在ES内部首先找到这个文档,删除旧文档执行更新,更新后再索引最新文档。

索引文档:

# curl -XPUT node3:9200/test?pretty
{
  "acknowledged" : true
}

curl -XPOST node2:9200/test/mytype/1?pretty -d '
  {
    "id":1,
    "name":"zhangsan",
    "age":25
  }'

curl -XPUT node2:9200/test/mytype/1?pretty -d '
  {
    "id":1,
    "name":"lisi",
    "age":35
  }'

# curl -XPUT node3:9200/test/type1/1?pretty -d '{
"counter":1,
"tags":["red"]
}'
{
  "_index" : "test",
  "_type" : "type1",
  "_id" : "1",
  "_version" : 3,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

30岁老阿姨

支持一下哦!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值