索引管理_ mapping root object深入剖析

    什么是 root object? 就是某个 type 对应的 mapping json,包括了 properties,metadata(_id,_source,_type),settings(analyzer),其他settings(比如include_in_all) 如下高亮部分,“my_type” 这个一个大 json 就叫做 root object
PUT my_index
{
          
   
 "mappings": {
          
   
   "my_type":{
          
   
     "properties": {
          
   }
   }
 }
}
    properties type:数据类型 index:是否需要分词类型 analyzer:分词器
PUT my_index
{
          
   
 "mappings": {
          
   
   "my_type":{
          
   
     "properties": {
          
   
       "title":{
          
   
         "type": "text",
         "analyzer": "standard",
         "index": true
       }
     }
   }
 }
}
    _source
GET company/employee/1


--运行结果 -----------------------------------

{
          
   
  "_index": "company",
  "_type": "employee",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
          
   
    "address": {
          
   
      "country": "China",
      "province": "beijing",
      "city": "beijing"
    },
    "name": "lily",
    "age": 33,
    "join_date": "2014-05-20"
  }
}

查询出一个文档的时候,响应的数据中的 _source

好处: 查询的时候,直接可以拿到完整的 document,不需要先拿 document id,再发送一次请求拿 document partial update 基于 _source 实现 reindex(零停机重建索引) 时,直接基于 _source 实现,不需要从数据库(或者其他外部存储)查询数据再修改 可以基于 _source 定制返回 field debug query 更容易,因为可以直接看到 _source

1创建索引,并设置禁用 _source

PUT my_index
{
          
   
 "mappings": {
          
   
   "my_type2":{
          
   
     "properties": {
          
   
       "title":{
          
   
         "type": "text",
         "analyzer": "standard",
         "index": true
       },
       "content":{
          
   
         "type": "text",
         "analyzer": "standard"
       },
       "post_date":{
          
   
         "type": "date"
    
       },
       "author_id":{
          
   
         "type": "long"
       }
     },
     "_source": {
          
   
       "enabled": false
     }
   }
 }
}

--运行结果--------------------------------------------------
{
          
   
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "my_index"
}

2插入一条数据

PUT /my_index/my_type2/1
{
          
   
  "title": "first article",
  "content": "this is my second article",
  "post_date": "2017-01-01",
  "author_id": 110
}
------运行结果------
{
          
   
  "_index": "my_index",
  "_type": "my_type2",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
          
   
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

3.查询

GET /my_index/my_type2/1
------运行结果------
-{
          
   
  "_index": "my_index",
  "_type": "my_type2",
  "_id": "1",
  "_version": 1,
  "found": true
}

可以看到返回的 _source已经没有了

    _all 将所有 field 打包在一起,作为一个 _all field,建立索引。没指定任何 field 进行搜索时,就是使用 _all field在搜索

默认开启,可以手动关闭

PUT /my_index/_mapping/my_type3
{
          
   
  "_all": {
          
   "enabled": false}
}

也可以在 field 级别设置 include_in_all field,设置是否要将 field 的值包含在 _all field 中

PUT /my_index/_mapping/my_type4
{
          
   
  "properties": {
          
   
    "my_field": {
          
   
      "type": "text",
      "include_in_all": false
    }
  }
}
    标识性 metadata _index _type _id
经验分享 程序员 微信小程序 职场和发展