支持的字段类型
> 文档中心 > 文档中心 > INFINI Easysearch > 功能手册 > 字段类型 > 支持的字段类型

字段类型 #

您可以在创建映射时为字段指定数据类型。以下表格列出了 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)。
Objectobject: 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 也必须是字符串。{: .note}

示例 #

创建一个映射,将 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
          ]
        }
      }
    ]
  }
}

扁平化类型(Flattened) #

在 Easysearch 中,在索引文档之前,不必指定映射,如果没有指定映射,Easysearch 将使用动态映射(Dynamic Mapping)来自动映射文档中的每个字段及其子字段。

动态地映射所有新的子字段可能会迅速导致“映射爆炸”(Mapping Explosion),即不断增长的字段数量可能会降低集群的性能。

扁平化类型(Flattened)字段类型通过将整个 JSON 对象视为字符串来解决这个问题。JSON 对象内的子字段可通过标准的点路径表示法(Dot Path Notation)进行访问,但它们不会被索引以进行快速查找。

最大字段值长度是 2^24 - 1。

Flattened 字段类型提供以下好处:

  • 高效读取:获取性能与 keyword 字段类似。
  • 内存效率:在不为所有子字段建立索引的情况下,将整个复杂的 JSON 对象存储在一个字段中,从而减少索引中的字段数量。
  • 空间效率:flattened 类型中的子字段不会创建倒排索引(inverted index),从而节省空间。
  • 迁移兼容性:您可以将数据从支持类似平面类型的系统迁移到 Easysearch。

当字段及其子字段主要用于读取而不用作搜索条件时,将字段映射为 flattened 类型是适用的,因为子字段没有被索引。flattened 类型对于具有大量字段的对象或当您事先不知道键(keys)时很有用。

flattened 类型支持使用或不使用点路径表示法的精确匹配查询。要查看支持的查询类型的完整列表,请参见 支持的查询语句。

Flattened 不支持:

  • 针对特定类型的解析,对不同数据类型进行专门定制的解析操作。
  • 数值操作,例如数值比较或数值排序。
  • 文本分析。
  • 高亮显示。
  • 使用点表示法对子字段进行聚合。
  • 按子字段进行过滤。

支持的查询 #

  • Term
  • Terms
  • Terms set
  • Prefix
  • Range
  • Match
  • Multi-match
  • Query string
  • Simple query string
  • Exists

限制 #

  • Flattened不支持开放参数(例如 normalizer, docValues, ignoreAbove, nullValue, similarity, depthlimit)。
  • 用于检索子字段值的Painless脚本和通配符查询不受支持。

如何使用 #

以下示例说明了如何将字段映射为 flattened 类型,如何在带有 flattened 类型字段的文档中进行索引,以及如何在这些文档中搜索 flattened 字段的叶子值。

首先,创建mapping

PUT /test-index/
{
  "mappings": {
    "properties": {
      "issue": {
        "type": "flattened"
      }
    }
  }
}

索引2个文档

PUT /test-index/_doc/1
{
  "issue": {
    "number": "123456",
    "labels": {
      "version": "2.1",
      "backport": [
        "2.0",
        "1.3"
      ],
      "category": {
        "type": "API",
        "level": "enhancement"
      }
    }
  }
}

PUT /test-index/_doc/2
{
  "issue": {
    "number": "123457",
    "labels": {
      "version": "2.2",
      "category": {
        "type": "API",
        "level": "bug"
      }
    }
  }
}

搜索 flattened 字段,即使不知道字段名称,也可以在整个平面对象中搜索叶值。例如,以下请求搜索所有标记为 Bug 的问题:

GET /test-index/_search
{
  "query": {
    "match": {"issue": "bug"}
  }
}

或者,如果您知道要搜索的子字段名称,请以点表示法提供字段的路径:

GET /test-index/_search
{
  "query": {
    "match": {"issue.labels.category.level": "bug"}
  }
}

在这两种情况下,答复都是相同的,并包含文档 2:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1.0303539,
    "hits": [
      {
        "_index": "test-index",
        "_id": "2",
        "_score": 1.0303539,
        "_source": {
          "issue": {
            "number": "123457",
            "labels": {
              "version": "2.2",
              "category": {
                "type": "API",
                "level": "bug"
              }
            }
          }
        }
      }
    ]
  }
}

使用前缀查询,您可以搜索以 2. 开头的版本的所有问题

GET /test-index/_search
{
  "query": {
    "prefix": {"issue.labels.version": "2."}
  }
}

使用范围查询,您可以搜索 2.0–2.1 版本的所有问题:

GET /test-index/_search
{
  "query": {
    "range": {
      "issue": {
        "gte": "2.0",
        "lte": "2.1"
      }
    }
  }
}

将子字段定义为 flattened 类型 #

您可以将JSON对象的子字段定义为 flattened 类型。例如,使用以下查询将 issue.labels 定义为 flattened 类型:

PUT /test-index/
{
  "mappings": {
    "properties": {
      "issue": {
        "properties": {
          "number": {
            "type": "double"
          },
          "labels": {
            "type": "flattened"
          }
        }
      }
    }
  }
}

因为 issue.number 不是 flattened 的一部分,所以可以使用它来聚合和排序文档。