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

162

被折叠的 条评论
为什么被折叠?



