字段类型 #
您可以在创建映射时为字段指定数据类型。以下表格列出了 Easysearch 支持的所有数据字段类型。
类别 | 字段类型和描述 |
---|---|
别名 | alias : 现有字段的附加名称。 |
二进制 | binary : 以 Base64 编码的二进制值。 |
数值 | 数值类型 (byte , double , float , half_float , integer , long , unsigned_long , scaled_float , short )。 |
布尔 | boolean : 布尔值。 |
日期 | date : 以毫秒存储的日期。date_nanos : 以纳秒存储的日期。 |
IP 地址 | ip : IPv4 或 IPv6 格式的 IP 地址。 |
范围 | 一组值 (integer_range , long_range , double_range , float_range , date_range , ip_range )。 |
Object | object : JSON 对象。nested : 用于需要独立索引为单独文档的数组中的对象。flattened : 作为字符串处理的 JSON 对象。join : 在同一索引中的文档之间建立父子关系。 |
字符串 | keyword : 包含不经过分析的字符串。text : 包含经过分析的字符串。token_count : 存储字符串中经过分析的标记数。 |
自动完成 | completion : 通过自动完成建议器提供自动完成功能。search_as_you_type : 使用前缀和中缀完成提供搜索即时输入功能。 |
地理 | geo_point : 地理点。geo_shape : 地理形状。 |
Rank | 增加或减少文档的相关性分数 (rank_feature , rank_features )。 |
k-NN 向量 | 包括 knn_dense_float_vector , knn_sparse_bool_vector 。 |
过滤器 | percolator : 指定将此字段视为查询。 |
Arrays #
在 Easysearch 中没有专门的数组字段类型。不过您可以将一组值传递到任何字段中。数组中的所有值必须具有相同的字段类型。
PUT testindex1/_doc/1
{
"number": 1
}
PUT testindex1/_doc/2
{
"number": [1, 2, 3]
}
Multifields #
Multifields 用于以不同方式索引相同的字段。通常,字符串被映射为 text 用于全文查询,而 keyword 用于精确值查询。
可以使用 fields 参数创建 Multifields。例如,您可以将书籍的 title 映射为 text 类型,并保留一个名为 title.raw 的子字段,其类型为 keyword。
PUT books
{
"mappings" : {
"properties" : {
"title" : {
"type" : "text",
"fields" : {
"raw" : {
"type" : "keyword"
}
}
}
}
}
}
Null value #
将字段的值设置为 null、空数组或包含 null 值的数组会使该字段等同于空字段。因此,您无法搜索具有该字段中的 null 值的文档。
要使字段可搜索 null 值,可以在索引的映射中指定其 null_value 参数。然后,传递给该字段的所有 null 值都将替换为指定的 null_value。
null_value 参数必须与字段的类型相同。例如,如果您的字段是字符串,那么该字段的 null_value 也必须是字符串。
示例 #
创建一个映射,将 emergency_phone 字段中的 null 值替换为字符串 “NONE”:
PUT testindex
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"emergency_phone": {
"type": "keyword",
"null_value": "NONE"
}
}
}
}
将三个文档索引到 testindex。文档 1 和文档 3 的 emergency_phone 字段包含 null,而文档 2 的 emergency_phone 字段具有一个空数组:
PUT testindex/_doc/1
{
"name": "Akua Mansa",
"emergency_phone": null
}
PUT testindex/_doc/2
{
"name": "Diego Ramirez",
"emergency_phone" : []
}
PUT testindex/_doc/3
{
"name": "Jane Doe",
"emergency_phone": [null, null]
}
搜索没有紧急电话的人:
GET testindex/_search
{
"query": {
"term": {
"emergency_phone": "NONE"
}
}
}
响应包含文档 1 和文档 3,但不包含文档 2,因为只有明确的 null 值才会被替换为字符串 “NONE”:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.18232156,
"hits": [
{
"_index": "testindex",
"_type": "_doc",
"_id": "1",
"_score": 0.18232156,
"_source": {
"name": "Akua Mansa",
"emergency_phone": null
}
},
{
"_index": "testindex",
"_type": "_doc",
"_id": "3",
"_score": 0.18232156,
"_source": {
"name": "Jane Doe",
"emergency_phone": [null, null]
}
}
]
}
}