文档
新增文档
新增文档DSL语法如下:
POST /索引库名/_doc/文档id
{
"字段1":"值1",
"字段2":"值2",
"字段3":{
"子属性1":"值3",
"子属性2":"值4",
}
// ...略
}
不加id则会随机生成一个,不便于查询,所以尽可能加入id
实例
POST /es_test/_doc/1
{
"info":"测试es",
"email":"xxx@163.com",
"name":{
"firstName":"三",
"lastName":"张"
},
"age":16
}
执行结果
{
"_index" : "es_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询文档
语法GET /文档库名/_doc/文档id
实例GET /es_test/_doc/1
执行结果
{
"_index" : "es_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"info" : "测试es",
"email" : "xxx@163.com",
"name" : {
"firstName" : "三",
"lastName" : "张"
},
"age" : 16
}
}
删除文档
语法DELETE /文档库名/_doc/文档id
实例DELETE /es_test/_doc/1
执行结果
{
"_index" : "es_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
对文档进行写操作,其version自增1
二次删除文档,出现not_found
{
"_index" : "es_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
修改文档
修改文档有两种方式
方式一:全量修改,会删除旧文档,添加新文档
语法
PUT /索引库名/_doc/文档id
{
"字段1":"值1",
"字段2":"值2",
// ... 略
}
若文档id不存在,则新增;存在,则修改
方式二:增量修改,修改指定字段值
语法文章来源:https://www.toymoban.com/news/detail-507023.html
POST /索引库名/_update/文档id
{
"doc":{
"字段名":"新的值"
}
}
Reference
黑马程序员文章来源地址https://www.toymoban.com/news/detail-507023.html
到了这里,关于Elasticsearch:document的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!