enabled
编辑enabled
编辑
Elasticsearch 尝试索引您提供的所有字段,但有时您只想存储字段而不对其进行索引。例如,假设您将 Elasticsearch 用作 Web 会话存储。您可能希望索引会话 ID 和最后更新时间,但不需要查询或对会话数据本身运行聚合。
enabled
设置(只能应用于顶级映射定义和 object
字段)会导致 Elasticsearch 完全跳过对字段内容的解析。JSON 仍然可以从 _source
字段中检索,但它不可搜索,也不以任何其他方式存储。
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { user_id: { type: 'keyword' }, last_updated: { type: 'date' }, session_data: { type: 'object', enabled: false } } } } ) puts response response = client.index( index: 'my-index-000001', id: 'session_1', body: { user_id: 'kimchy', session_data: { arbitrary_object: { some_array: [ 'foo', 'bar', { baz: 2 } ] } }, last_updated: '2015-12-06T18:20:22' } ) puts response response = client.index( index: 'my-index-000001', id: 'session_2', body: { user_id: 'jpountz', session_data: 'none', last_updated: '2015-12-06T18:22:13' } ) puts response
PUT my-index-000001 { "mappings": { "properties": { "user_id": { "type": "keyword" }, "last_updated": { "type": "date" }, "session_data": { "type": "object", "enabled": false } } } } PUT my-index-000001/_doc/session_1 { "user_id": "kimchy", "session_data": { "arbitrary_object": { "some_array": [ "foo", "bar", { "baz": 2 } ] } }, "last_updated": "2015-12-06T18:20:22" } PUT my-index-000001/_doc/session_2 { "user_id": "jpountz", "session_data": "none", "last_updated": "2015-12-06T18:22:13" }
整个映射也可以禁用,在这种情况下,文档将存储在 _source
字段中,这意味着它可以检索,但其内容不会以任何方式索引。
response = client.indices.create( index: 'my-index-000001', body: { mappings: { enabled: false } } ) puts response response = client.index( index: 'my-index-000001', id: 'session_1', body: { user_id: 'kimchy', session_data: { arbitrary_object: { some_array: [ 'foo', 'bar', { baz: 2 } ] } }, last_updated: '2015-12-06T18:20:22' } ) puts response response = client.get( index: 'my-index-000001', id: 'session_1' ) puts response response = client.indices.get_mapping( index: 'my-index-000001' ) puts response
PUT my-index-000001 { "mappings": { "enabled": false } } PUT my-index-000001/_doc/session_1 { "user_id": "kimchy", "session_data": { "arbitrary_object": { "some_array": [ "foo", "bar", { "baz": 2 } ] } }, "last_updated": "2015-12-06T18:20:22" } GET my-index-000001/_doc/session_1 GET my-index-000001/_mapping
无法更新现有字段和顶级映射定义的 enabled
设置。
请注意,由于 Elasticsearch 完全跳过对字段内容的解析,因此可以将非对象数据添加到禁用的字段中。
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { session_data: { type: 'object', enabled: false } } } } ) puts response response = client.index( index: 'my-index-000001', id: 'session_1', body: { session_data: 'foo bar' } ) puts response