enabled(开启字段)
Elasticsearch尝试着索引所有你提供的字段,但是有时候你只想要存储这个字段而不需要索引.例如,假设你正在使用 Elasticsearch作为 web session 的存储.你可能想要索引 session ID 和最后一次更新时间,但不需要在 session 数据本身上查询或运行聚合。
enabled设置只可以应用于映射类型和 object 字段,导致 Elasticsearch 完全跳过字段内容的解析.这个 JSON仍然可以从 _source 字段中检索,但是不能以任何其他方式搜索或存储:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"session": {
"properties": {
"user_id": {
"type": "keyword"
},
"last_updated": {
"type": "date"
},
"session_data": { # 1
"enabled": false
}
}
}
}
}
'
curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
{
"user_id": "kimchy",
"session_data": { # 2
"arbitrary_object": {
"some_array": [ "foo", "bar", { "baz": 2 } ]
}
},
"last_updated": "2015-12-06T18:20:22"
}
'
curl -XPUT 'localhost:9200/my_index/session/session_2?pretty' -H 'Content-Type: application/json' -d'
{
"user_id": "jpountz",
"session_data": "none", # 3
"last_updated": "2015-12-06T18:22:13"
}
'
1
session_data 被禁用
2
任何数据都可以传递给 session_data 字段,因为它将完全被忽略
3
session_data 会忽略不是JSON对象的值.
整个映射类型也可以被禁用,在这种情况下, document 存储在_source字段中.这意味着它可以被检索,但是它的内容没有以任何方式被索引:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"session": { # 1
"enabled": false
}
}
}
'
curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
{
"user_id": "kimchy",
"session_data": {
"arbitrary_object": {
"some_array": [ "foo", "bar", { "baz": 2 } ]
}
},
"last_updated": "2015-12-06T18:20:22"
}
'
curl -XGET 'localhost:9200/my_index/session/session_1?pretty' # 2
curl -XGET 'localhost:9200/my_index/_mapping?pretty' # 3
1
完整的 session 映射类型被禁用
2
document可以被检索
3
检查映射确定没有字段被添加
建议:enabled 设置允许在同一索引中相同名称的字段有不同设置。 可以使用PUT mapping API在现有字段上更新已经存在字段的值。
Last updated
Was this helpful?