Bootstrap

Elasticsearch document 的 _source 元数据

文字内容整理自B站中华石杉的 Elasticsearch 顶尖高手系列课程核心知识篇

_source 元数据

put /test_index/test_type/1
{
  "test_field1": "test field1",
  "test_field2": "test field2"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "16",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

get /test_index/test_type/1

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "16",
  "_version" : 1,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field1" : "test field1",
    "test_field2" : "test field2"
  }
}

_source元数据:就是说,我们在创建一个document的时候,使用的那个放在request body 中的json串,默认情况下,在 get 的时候,会原封不动的给我们返回回来。

定制返回结果

定制返回的结果,指定_source中,返回哪些field

GET test_index/test_type/16?_source=test_field1

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "16",
  "_version" : 1,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field1" : "test field1"
  }
}

document 的全量替换

语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容

document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容

es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document

document 的强制创建

创建文档与全量替换的语法是一样的,有时我们只是想新建文档,不想替换文档,如果强制进行创建呢?

PUT /index/type/id?op_type=create
PUT /index/type/id/_create

document 的删除

DELETE /index/type/id

不会理解物理删除,只会将其标记为deleted,当 Elasticsearch 中数据越来越多的时候,Elasticsearch 会自动在后台将标记为 deleted 的 document 自动删除(物理),以释放空间。

我有一点好奇,就是已经被标记为 deleted 的 document 是否还能够被访问,甚至恢复?