5.elasticsearch 映射元字段

本文深入探讨了Elasticsearch中的映射元字段,包括它们的定义、作用及配置方式。详细介绍了_index、_type、_id等描述文档本身的元字段,以及_source、_size等源文档元字段的功能。此外,还解析了_field_names、_ignored等索引元字段的作用,以及_routing元字段在文档路由中的应用。

elasticsearch 映射元字段

elasticsearch 映射用来定义一个文档以及其包含的的字段如何被存储和索引

一个映射包含元字段和字段列表

元字段

描述文档本身的字段

_index、_type、_id、_source、_size、_field_names、_ignored、_routing、_meta

文档属性字段
属性名作用
_index文档所属索引
_type文档类型
_id文档ID
源文档元字段
属性名作用
_source文档原始json字符串
_size_source字段大小
_source
禁用_source (慎用),一般情况下不要禁用,
PUT tweets
{
  "mappings": {
    "_source": {
      "enabled": false
    }
  }
}
_source包含与排除字段
PUT logs
{
  "mappings": {
    "_source": {
      "includes": [
        "*.count",
        "meta.*"
      ],
      "excludes": [
        "meta.description",
        "meta.other.*"
      ]
    }
  }
}

PUT logs/_doc/1
{
  "requests": {
    "count": 10,
    "foo": "bar" 
  },
  "meta": {
    "name": "Some metric",
    "description": "Some metric description", 
    "other": {
      "foo": "one", 
      "baz": "two" 
    }
  }
}

GET logs/_search
{
  "query": {
    "match": {
      "meta.other.foo": "one" 
    }
  }
}
_size

_size:默认不支持,需要安装mapper-size插件

sudo bin/elasticsearch-plugin install mapper-size
索引元字段
属性名作用
_field_names所有非空字段的名字,这个字段常用于exists查询
_ignoredignore_malformed开启时忽略错误字段
_field_names
DELETE my_index

PUT my_index/_doc/1
{
  "title": "This is a document"
}
 
PUT my_index/_doc/2?refresh=true
{
  "title": "This is another document",
  "body": "This document has a body"
}
 
GET my_index/_search
{
  "query": {
    "exists": {
      "field": "body"
    }
  }
}
_ignored
PUT my_index
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "ignore_malformed": true	//忽略错误
      },
      "number_two": {
        "type": "integer"
      }
    }
  }
}

PUT my_index/_doc/1					//可以插入文档,number_one字段为空
{
  "text":       "Some text value",
  "number_one": "foo" 
}

PUT my_index/_doc/2					//无法插入文档
{
  "text":       "Some text value",
  "number_two": "foo" 
}

{
  ......
  "hits" : {
   .......
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_ignored" : [		//忽略number_one错误字段,文档可以插入
          "number_one"
        ],
        "_source" : {
          "text" : "Some text value",
          "number_one" : "foo"
        }
      }
    ]
  }
}

路由元字段
_routing

_routing:将文档路由到特定的分片上的路由值

计算公式:shared = hash(routing) % number_of_primary_shards,默认_routing值是文档的ID

PUT my_index/_doc/1?routing=user1&refresh=true 
{
  "title": "This is a document"
}

GET my_index/_doc/1?routing=user1
query中使用_routing
GET my_index/_search
{
  "query": {
    "terms": {
      "_routing": [ "user1" ] 
    }
  }
}
自定义元字段
_meta
PUT my_index
{
  "mappings": {
    "_meta": { 
      "class": "MyApp::User",
      "version": {
        "min": "1.0",
        "max": "1.3"
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值