布尔字段类型
编辑布尔字段类型
编辑布尔字段接受 JSON true
和 false
值,但也接受会被解释为 true 或 false 的字符串。
False 值 |
|
True 值 |
|
例如
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "is_published": { "type": "boolean" } } }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", refresh=True, document={ "is_published": "true" }, ) print(resp1) resp2 = client.search( index="my-index-000001", query={ "term": { "is_published": True } }, ) print(resp2)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { is_published: { type: 'boolean' } } } } ) puts response response = client.index( index: 'my-index-000001', id: 1, refresh: true, body: { is_published: 'true' } ) puts response response = client.search( index: 'my-index-000001', body: { query: { term: { is_published: true } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { is_published: { type: "boolean", }, }, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, refresh: "true", document: { is_published: "true", }, }); console.log(response1); const response2 = await client.search({ index: "my-index-000001", query: { term: { is_published: true, }, }, }); console.log(response2);
PUT my-index-000001 { "mappings": { "properties": { "is_published": { "type": "boolean" } } } } POST my-index-000001/_doc/1?refresh { "is_published": "true" } GET my-index-000001/_search { "query": { "term": { "is_published": true } } }
诸如 terms
聚合之类的聚合使用 1
和 0
作为 key
,并使用字符串 "true"
和 "false"
作为 key_as_string
。当在脚本中使用时,布尔字段返回 true
和 false
。
resp = client.index( index="my-index-000001", id="1", refresh=True, document={ "is_published": True }, ) print(resp) resp1 = client.index( index="my-index-000001", id="2", refresh=True, document={ "is_published": False }, ) print(resp1) resp2 = client.search( index="my-index-000001", aggs={ "publish_state": { "terms": { "field": "is_published" } } }, sort=[ "is_published" ], fields=[ { "field": "weight" } ], runtime_mappings={ "weight": { "type": "long", "script": "emit(doc['is_published'].value ? 10 : 0)" } }, ) print(resp2)
response = client.index( index: 'my-index-000001', id: 1, refresh: true, body: { is_published: true } ) puts response response = client.index( index: 'my-index-000001', id: 2, refresh: true, body: { is_published: false } ) puts response response = client.search( index: 'my-index-000001', body: { aggregations: { publish_state: { terms: { field: 'is_published' } } }, sort: [ 'is_published' ], fields: [ { field: 'weight' } ], runtime_mappings: { weight: { type: 'long', script: "emit(doc['is_published'].value ? 10 : 0)" } } } ) puts response
const response = await client.index({ index: "my-index-000001", id: 1, refresh: "true", document: { is_published: true, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 2, refresh: "true", document: { is_published: false, }, }); console.log(response1); const response2 = await client.search({ index: "my-index-000001", aggs: { publish_state: { terms: { field: "is_published", }, }, }, sort: ["is_published"], fields: [ { field: "weight", }, ], runtime_mappings: { weight: { type: "long", script: "emit(doc['is_published'].value ? 10 : 0)", }, }, }); console.log(response2);
POST my-index-000001/_doc/1?refresh { "is_published": true } POST my-index-000001/_doc/2?refresh { "is_published": false } GET my-index-000001/_search { "aggs": { "publish_state": { "terms": { "field": "is_published" } } }, "sort": [ "is_published" ], "fields": [ {"field": "weight"} ], "runtime_mappings": { "weight": { "type": "long", "script": "emit(doc['is_published'].value ? 10 : 0)" } } }
boolean
字段的参数
编辑boolean
字段接受以下参数:
是否应将字段以列式方式存储在磁盘上,以便稍后用于排序、聚合或脚本编写?接受 |
|
该字段是否应可以快速搜索?接受 |
|
默认情况下,尝试将错误的数据类型索引到字段中会引发异常并拒绝整个文档。如果此参数设置为 true,则允许忽略该异常。格式错误的字段不会被索引,但文档中的其他字段会被正常处理。接受 |
|
接受上面列出的任何 true 或 false 值。该值将替换任何显式的 |
|
|
定义在索引时,由 |
|
如果设置了此参数,则该字段将索引由此脚本生成的值,而不是直接从源读取值。如果在输入文档中为此字段设置了值,则该文档将被拒绝并出现错误。脚本的格式与它们的 运行时等效项相同。 |
是否应将字段值存储并从 |
|
有关该字段的元数据。 |
|
|
(可选,布尔值) 将该字段标记为时间序列维度。默认为
维度字段具有以下约束:
|
合成 _source
编辑合成 _source
仅对 TSDB 索引(将 index.mode
设置为 time_series
的索引)普遍可用。对于其他索引,合成 _source
处于技术预览状态。技术预览中的功能可能会在未来的版本中更改或删除。Elastic 将努力解决任何问题,但技术预览中的功能不受官方 GA 功能的支持 SLA 约束。
boolean
字段在其默认配置中支持 合成 _source
。
合成源可以对 boolean
字段值进行排序。例如:
resp = client.indices.create( index="idx", settings={ "index": { "mapping": { "source": { "mode": "synthetic" } } } }, mappings={ "properties": { "bool": { "type": "boolean" } } }, ) print(resp) resp1 = client.index( index="idx", id="1", document={ "bool": [ True, False, True, False ] }, ) print(resp1)
const response = await client.indices.create({ index: "idx", settings: { index: { mapping: { source: { mode: "synthetic", }, }, }, }, mappings: { properties: { bool: { type: "boolean", }, }, }, }); console.log(response); const response1 = await client.index({ index: "idx", id: 1, document: { bool: [true, false, true, false], }, }); console.log(response1);
PUT idx { "settings": { "index": { "mapping": { "source": { "mode": "synthetic" } } } }, "mappings": { "properties": { "bool": { "type": "boolean" } } } } PUT idx/_doc/1 { "bool": [true, false, true, false] }
将变为:
{ "bool": [false, false, true, true] }