布尔字段类型

编辑

布尔字段接受 JSON truefalse 值,但也接受会被解释为 true 或 false 的字符串。

False 值

false, "false", "" (空字符串)

True 值

true, "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 
    }
  }
}

索引一个带有 "true" 的文档,它会被解释为 true

搜索具有 JSON true 的文档。

诸如 terms 聚合之类的聚合使用 10 作为 key,并使用字符串 "true""false" 作为 key_as_string。当在脚本中使用时,布尔字段返回 truefalse

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 字段接受以下参数:

doc_values

是否应将字段以列式方式存储在磁盘上,以便稍后用于排序、聚合或脚本编写?接受 true(默认)或 false

index

该字段是否应可以快速搜索?接受 true(默认)和 false。仅启用 doc_values 的字段仍然可以使用基于词项或范围的查询进行查询,尽管速度较慢。

ignore_malformed

默认情况下,尝试将错误的数据类型索引到字段中会引发异常并拒绝整个文档。如果此参数设置为 true,则允许忽略该异常。格式错误的字段不会被索引,但文档中的其他字段会被正常处理。接受 truefalse。请注意,如果使用了 script 参数,则不能设置此参数。

null_value

接受上面列出的任何 true 或 false 值。该值将替换任何显式的 null 值。默认为 null,这意味着该字段被视为缺失。请注意,如果使用了 script 参数,则不能设置此参数。

on_script_error

定义在索引时,由 script 参数定义的脚本抛出错误时该如何处理。接受 fail (默认),这将导致整个文档被拒绝,以及 continue,它将在文档的 _ignored 元数据字段中注册该字段并继续索引。只有在设置了 script 字段时,才能设置此参数。

script

如果设置了此参数,则该字段将索引由此脚本生成的值,而不是直接从源读取值。如果在输入文档中为此字段设置了值,则该文档将被拒绝并出现错误。脚本的格式与它们的 运行时等效项相同。

store

是否应将字段值存储并从 _source 字段单独检索。接受 truefalse (默认)。

meta

有关该字段的元数据。

time_series_dimension

(可选,布尔值)

将该字段标记为时间序列维度。默认为 false

index.mapping.dimension_fields.limit 索引设置限制索引中维度的数量。

维度字段具有以下约束:

  • doc_valuesindex 映射参数必须为 true

合成 _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]
}