Boolean field type
editBoolean field type
editBoolean fields accept JSON true
and false
values, but can also accept
strings which are interpreted as either true or false:
False values |
|
True values |
|
For example:
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 } } }
Indexing a document with |
|
Searching for documents with a JSON |
Aggregations like the terms
aggregation use 1
and 0
for the key
, and the strings "true"
and
"false"
for the key_as_string
. Boolean fields when used in scripts,
return true
and 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)" } } }
Parameters for boolean
fields
editThe following parameters are accepted by boolean
fields:
Should the field be stored on disk in a column-stride fashion, so that it
can later be used for sorting, aggregations, or scripting? Accepts |
|
Should the field be quickly searchable? Accepts |
|
Trying to index the wrong data type into a field throws an exception by
default, and rejects the whole document. If this parameter is set to true,
it allows the exception to be ignored. The malformed field is not indexed,
but other fields in the document are processed normally. Accepts |
|
Accepts any of the true or false values listed above. The value is
substituted for any explicit |
|
|
Defines what to do if the script defined by the |
|
If this parameter is set, then the field will index values generated by this script, rather than reading the values directly from the source. If a value is set for this field on the input document, then the document will be rejected with an error. Scripts are in the same format as their runtime equivalent. |
Whether the field value should be stored and retrievable separately from
the |
|
Metadata about the field. |
|
|
(Optional, Boolean) Marks the field as a time series dimension. Defaults
to The
Dimension fields have the following constraints:
|
Synthetic _source
editSynthetic _source
is Generally Available only for TSDB indices
(indices that have index.mode
set to time_series
). For other indices
synthetic _source
is in technical preview. Features in technical preview may
be changed or removed in a future release. Elastic will work to fix
any issues, but features in technical preview are not subject to the support SLA
of official GA features.
boolean
fields support synthetic _source
in their
default configuration.
Synthetic source may sort boolean
field values. For example:
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] }
Will become:
{ "bool": [false, false, true, true] }