Elasticsearch系列(5)Mapping之元数据字段

2020-09-02  本文已影响0人  正义的杰克船长

1. 前言

每个文档都有与之关联的元数据字段(Metadata fields),比如_index、_type和_id元数据字段。在创建Mapping时,可以自定义其中一些元数据字段的行为。接下来具体介绍一些重要的元数据字段。


元数据字段分类

2. 标识元数据字段

_index

PUT my_index_1/_doc/1
{"text":"Document in index 1"}
PUT my_index_2/_doc/2?refresh=true
{"text":"Document in index 2"}
# term查询、聚合、排序及脚本中使用_index字段
GET my_index_1,my_index_2/_search
{
  "query": {
    "terms": {
      "_index": [
        "my_index_1",
        "my_index_2"
      ]
    }
  },
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index",
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_index": {
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "index_name": {
      "script": {
        "lang": "painless",
        "source": "doc['_index']"
      }
    }
  }
}

_id

PUT my_index_1/_doc/1
{"text":"Document with ID 1"}
PUT my_index_1/_doc/2?refresh=true
{"text":"Document with ID 2"}
# term查询中使用_id字段
GET my_index_1/_search
{"query":{"terms":{"_id":["1","2"]}}}

3. 文档源元数据字段

_source

# 禁用_source字段
PUT my_index_1
{"mappings":{"_source":{"enabled":false}}}
# 创建索引my_index_1,指定_source字段中参数includes/excludes
PUT my_index_1
{
  "mappings": {
    "_source": {
      "includes": [
        "*.count",
        "meta.*"
      ],
      "excludes": [
        "meta.description",
        "meta.other.*"
      ]
    }
  }
}
PUT my_index_1/_doc/1
{
  "requests": {
    "count": 10,
    "foo": "bar" 
  },
  "meta": {
    "name": "Some metric",
    "description": "Some metric description", 
    "other": {
      "foo": "one", 
      "baz": "two" 
    }
  }
}
# 我们可以通过meta.other.foo字段搜索并匹配到数据,
# 即使这个字段内容在_source中被排除。
GET my_index_1/_search
{"query":{"match":{"meta.other.foo":"one"}}}

_size

4. 索引元数据字段

_field_names

# 禁用_field_names字段
PUT my_index_1
{"mappings":{"_field_names":{"enabled":false}}}

_ignored

PUT my_index_1
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "ignore_malformed": true
      
      },
      "number_two": {
        "type": "integer",
        "ignore_malformed": true
      }
    }
  }
}
# 由于字段number_one设置了忽略缺陷格式的数据,保存成功
PUT my_index_1/_doc/1
{"text":"Some text value","number_one":"foo"}
# 由于字段number_two设置了ignore_malformed=false,保存失败
PUT my_index_1/_doc/2
{"text":"Some text value","number_two":"foo"}
# 查询已经被忽略缺陷的数据
GET my_index_1/_search
{"query":{"exists":{"field":"_ignored"}}}

5. 路由元数据字段

_routing

shard_num = hash(_routing) % num_primary_shards
PUT my_index_1 
{
  "settings": {
    "number_of_shards": 2
  }, 
  "mappings": {
    "properties": {
      "title": {
        "type" : "text"
      }
    }
  }  
}
# 文档1使用user1替代id作为routing值
PUT my_index_1/_doc/1?routing=user1&refresh=true 
{"title":"This is a document"}
# 在term query中使用_routing字段搜索
GET my_index_1/_search
{"query":{"terms":{"_routing":["user1"]}}}
PUT my_index_1 
{
  "mappings": {
    "_routing": {
      "required": true 
    }
  }  
}

6. 其他元数据字段

_meta

# 自定义元信息class、version,表示文档所属的class和适用的版本范围
PUT my_index_1
{
  "mappings": {
    "_meta": { 
      "class": "MyApp::User",
      "version": {
        "min": "1.0",
        "max": "1.3"
      }
    }
  }
}
上一篇 下一篇

猜你喜欢

热点阅读