Elasticsearch数据类型


随着版本的改动,小部分数据类型也有细微的改动,就比如string类型新版本就不再支持。我本地的版本是7.9.3文档版本是7.X

1 常见类型

1.1 binary - 二进制型

二进制值编码为Base64字符串, 不以默认的方式存储, 不能被搜索. 有2个设置项:

  • doc_values:该字段是否需要存储到磁盘上, 方便以后用来排序、聚合或脚本查询. 接受truefalse(默认);
  • store: 该字段的值是否要和_source分开存储、检索, 意思是除了_source中, 是否要单独再存储一份. 接受truefalse(默认).

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "blob": {
        "type": "binary"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

1.2 boolean - 布尔类型

字面意思,布尔字段接受JSONtruefalse值,但也可以接受解释为true或false的字符串:

假值 false"false"""(空字符串)
真值 true"true"

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "is_published": {
        "type": "boolean"
      }
    }
  }
}

POST my-index-000001/_doc/1
{
  "is_published": "true" 
}

1.3 keyword - 关键字类型

在Elasticsearch 5.4 版本开始, keyword取代了不需要分词的string.

关键字包括以下几种:

  • keyword,用于结构化内容,例如ID,电子邮件地址,主机名,状态代码,邮政编码或标签。
  • constant_keyword 对于始终包含相同值的关键字字段。
  • wildcard,可针对类似grep的通配符查询优化日志行和类似的关键字值。

当一个字段需要按照精确值进行过滤、排序、聚合等操作时, 就应该使用关键字类型.

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "tags": {
        "type":  "keyword"
      }
    }
  }
}

1.4 number - 数字类型

数字类型包括以下9种:

long 有符号的64位整数,最小值为,最大值为。 -2⁶³-2⁶³-1
integer 有符号的32位整数,最小值为,最大值为。 ̶-2³¹-2³¹-1
short 有符号的16位整数,最小值为-32,768,最大值为32,767
byte 带符号的8位整数,最小值为-128,最大值为127
double 一个双精度64位IEEE 754浮点数,限制为有限值。
float 单精度32位IEEE 754浮点数,限制为有限值。
half_float 半精度16位IEEE 754浮点数,限制为有限值。
scaled_float 以a为后缀的浮点数long,由固定double比例因子缩放。
unsigned_long 一个无符号的64位整数,最小值为0,最大值为。 2⁶⁴-1

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "number_of_bytes": {
        "type": "integer"
      },
      "time_in_seconds": {
        "type": "float"
      },
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

1.5 date - 日期类型

1.5.1 date
  • 包含格式化日期的字符串,例如"2015-01-01""2015/01/01 12:10:30"
  • 从纪元以来 代表毫秒的长整数。
  • 代表秒后的整数。

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "date": {
          // 该date字段使用默认值format。
        "type": "date" 
      }
    }
  }
}

// 本文档使用简单的日期。
PUT my-index-000001/_doc/1
{ "date": "2015-01-01" } 

// Solr中默认使用的日期格式。
PUT my-index-000001/_doc/2
{ "date": "2015-01-01T12:10:30Z" } 

// 时间的毫秒值。
PUT my-index-000001/_doc/3
{ "date": 1420070400001 } 
1.5.2 date_nanos

此数据类型是日期数据类型的补充。 但是,两者之间有重要区别。 现有的日期数据类型以毫秒为单位存储日期。 date_nanos数据类型以纳秒为单位存储日期,这限制了它的日期范围从大约1970到2262,因为日期仍然自从纪元以来以长表示纳秒的形式存储。

纳秒级的查询在内部转换为对此长表示形式的范围查询,并且聚合和存储的字段的结果将转换为字符串,具体取决于与该字段关联的日期格式。

1.5.3 日期格式化

多个格式使用双竖线||分隔, 每个格式都会被依次尝试, 直到找到匹配的.
第一个格式用于将时间毫秒值转换为对应格式的字符串.

使用示例:

PUT my-index-000001?include_type_name=true
{
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
        }
      }
    }
  }
}

1.6 alias - 别名

一个alias映射为索引中的一个字段定义的替代名称。别名可以代替搜索请求中的目标字段,也可以使用其他其他API(例如field features)使用

使用示例:

PUT trips
{
  "mappings": {
    "properties": {
      "distance": {
        "type": "long"
      },
      "route_length_miles": {
        "type": "alias",
        // 目标字段的路径。请注意,这必须是完整路径,包括任何父对象(例如object1.object2.field)。
        "path": "distance" 
      },
      "transit_mode": {
        "type": "keyword"
      }
    }
  }
}

GET _search
{
  "query": {
    "range" : {
      "route_length_miles" : {
        "gte" : 39
      }
    }
  }
}

2 对象和关系类型

2.1 object - JSON对象

JSON文档本质上是分层的:文档可能包含内部对象,而内部对象又可能包含内部对象本身:

使用示例:

PUT my-index-000001/_doc/1
{ 
  "region": "US",
  "manager": { 
    "age":     30,
    "name": { 
      "first": "John",
      "last":  "Smith"
    }
  }
}

存储方式:

{
  "region":             "US",
  "manager.age":        30,
  "manager.name.first": "John",
  "manager.name.last":  "Smith"
}

文档的映射结构类似为:

PUT my-index-000001
{
  "mappings": {
    "properties": { 
      "region": {
        "type": "keyword"
      },
      "manager": { 
        "properties": {
          "age":  { "type": "integer" },
          "name": { 
            "properties": {
              "first": { "type": "text" },
              "last":  { "type": "text" }
            }
          }
        }
      }
    }
  }
}

2.2 flattened - 整个JSON对象作为单个字段值(7.3新功能)

默认情况下,对象中的每个子字段都需要分别进行映射和索引。如果事先不知道子字段的名称或类型,则将动态映射它们。

flattened 数据类型提供了一种替代方法,其中将整个对象映射为单个字段。对于给定的对象,flatten 类型映射将解析出其 leaf 值并将它们作为关键字索引到一个字段中。然后可以通过简单的查询和汇总来搜索对象的内容。

此数据类型对于索引具有大量或未知数量的唯一键的对象很有用。仅为整个 JSON 对象创建一个字段映射,这可以帮助防止由于大量不同的字段映射而导致映射爆炸

另一方面,flatten的对象字段在搜索功能方面存在折衷。仅允许基本查询,不支持数字范围查询或突出显示(highlighting)。

在使用 flattened 数据类型时,必须注意的是:

flattened 的映射类型不应用于索引所有文档内容,因为它将所有值都视为关键字,并且不提供完整的搜索功能。 在大多数情况下,默认方法(每个子字段在映射中都有其自己相对应的项)有效。

使用示例:

PUT bug_reports
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      // 该字段可以用来保存一个对象
      "labels": {
        "type": "flattened"
      }
    }
  }
}

POST bug_reports/_doc/1
{
  "title": "Results are not sorted correctly.",
  "labels": {
    "priority": "urgent",
    "release": ["v1.2.5", "v1.3.0"],
    "timestamp": {
      "created": 1541458026,
      "closed": 1541457010
    }
  }
}

可通过如下方式查询:

POST bug_reports/_search
{
  "query": {
    "term": {"labels": "urgent"}
  }
}

// 结果
"hits" : {
    "total" : {
        "value" : 1,
        "relation" : "eq"
    },
    "max_score" : 0.42763555,
    "hits" : [
        {
            "_index" : "bug_reports",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.42763555,
            "_source" : {
                "title" : "Results are not sorted correctly.",
                "labels" : {
                    "priority" : "urgent",
                    "release" : [
                        "v1.2.5",
                        "v1.3.0"
                    ],
                    "timestamp" : {
                        "created" : 1541458026,
                        "closed" : 1541457010
                    }
                }
            }
        }
    ]
}

// 要查询flattened对象中的特定键,请使用“."来表示POST bug_reports/_search
{
  "query": {
    "term": {"labels.release": "v1.3.0"}
  }
}

// 结果
"hits" : {
    "total" : {
        "value" : 1,
        "relation" : "eq"
    },
    "max_score" : 0.42763555,
    "hits" : [
        {
            "_index" : "bug_reports",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.42763555,
            "_source" : {
                "title" : "Results are not sorted correctly.",
                "labels" : {
                    "priority" : "urgent",
                    "release" : [
                        "v1.2.5",
                        "v1.3.0"
                    ],
                    "timestamp" : {
                        "created" : 1541458026,
                        "closed" : 1541457010
                    }
                }
            }
        }
    ]
}

查询时,不可能使用通配符来引用字段关键字,例如 {“ term”:{“ labels.time *”:1541457010}}。 请注意,所有查询(包括范围)都将值视为字符串关键字。 拼合的字段不支持突出显示(highlighting)。

可以对 flattened 的对象字段进行排序,以及执行简单的关键字样式聚合(例如terms aggregation)。 与查询一样,对数字没有特殊支持-将 JSON 对象中的所有值都视为关键字。 排序时,这意味着按字典顺序对值进行比较。

2.3 nested - 嵌套类型

嵌套类型是对象数据类型的一个特例, 可以让array类型的对象被独立索引和搜索.

对象数组的存储方式:

PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

// 内部存储结构
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

可以看出, user.first和user.last会被平铺为多值字段, 这样一来, John和Snow之间的关联性就丢失了.

在查询时, 可能出现John White的结果.

如果需要对以最对象进行索引, 且保留数组中每个对象的独立性, 就应该使用嵌套数据类型.

—— 嵌套对象实质是将每个对象分离出来, 作为隐藏文档进行索引.

使用示例:

// 添加数据
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}

// 添加数据
PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

// 检索
GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "Smith" }} 
          ]
        }
      }
    }
  }
}

// 检索
GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "White" }} 
          ]
        }
      },
      "inner_hits": { 
        "highlight": {
          "fields": {
            "user.first": {}
          }
        }
      }
    }
  }
}

2.4 join - 联接字段类型(6.X版本功能)

join数据类型是创建相同的索引文件中的父/子关系的特殊领域。本relations节定义了文档中的一组可能的关系,每个关系都是父名称和子名称。父/子关系可以定义如下:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_id": {
        "type": "keyword"
      },
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}

要使用连接索引文档,必须在中提供关系的名称和文档的可选父级source。例如,以下示例parentquestion上下文中创建两个文档:

PUT my-index-000001/_doc/1?refresh
{
  "my_id": "1",
  "text": "This is a question",
  "my_join_field": {
    "name": "question" 
  }
}

PUT my-index-000001/_doc/2?refresh
{
  "my_id": "2",
  "text": "This is another question",
  "my_join_field": {
    "name": "question"
  }
}

在为父级文档建立索引时,可以选择仅指定关系的名称作为快捷方式,而不必将其封装在普通的对象符号中:

这里采用refresh来强制进行索引

PUT my-index-000001/_doc/1?refresh
{
  "my_id": "1",
  "text": "This is a question",
  "my_join_field": "question" 
}

PUT my-index-000001/_doc/2?refresh
{
  "my_id": "2",
  "text": "This is another question",
  "my_join_field": "question"
}

在为子级建立索引时,必须在中添加关系的名称以及文档的父级ID _source

例如,以下示例显示了如何为两个child文档建立索引:

PUT my-index-000001/_doc/3?routing=1&refresh 
{
  "my_id": "3",
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}

PUT my-index-000001/_doc/4?routing=1&refresh
{
  "my_id": "4",
  "text": "This is another answer",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}

其它使用看文档吧…地址

3 结构化数据类型

3.1 范围

范围字段类型表示上限和下限之间的连续值范围。例如,范围可以表示十月中的任何日期0到9之间的任何整数。他们正在使用的运营商所定义 gtgte为下限,和ltlte用于上的约束。它们可用于查询,并且对聚合的支持有限。唯一受支持的聚合是 直方图基数

支持以下范围类型:

integer_range 一个带符号的32位整数范围,最小值为,最大值为。 ̶-2³¹- 2³¹-1
float_range 一系列单精度32位IEEE 754浮点值。
long_range 一系列带符号的64位整数,最小值为,最大值为。 -2⁶³-2⁶³-1
double_range 一系列双精度64位IEEE 754浮点值。
date_range date值 的范围。日期范围通过format映射参数支持各种日期格式。无论使用哪种格式,日期值都将解析为无符号的64位整数,该整数表示自UTC的Unix纪元以来的毫秒数。不支持包含now 日期数学表达式的值 。
ip_range 支持IPv4IPv6(或混合)地址的一系列ip值。

使用示例:

PUT range_index
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "expected_attendees": {
        "type": "integer_range"
      },
      "time_frame": {
        "type": "date_range", 
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

PUT range_index/_doc/1?refresh
{
  "expected_attendees" : { 
    "gte" : 10,
    "lt" : 20
  },
  "time_frame" : {
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

以下是在名为“ expected_attendees”的字段上进行术语查询的示例integer_range。12是该范围内的值,因此它将匹配。

GET range_index/_search
{
  "query" : {
    "term" : {
      "expected_attendees" : {
        "value": 12
      }
    }
  }
}

// 结果
{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lt" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}

以下是date_rangedate_range名为“ time_frame”的字段上进行查询的示例。

GET range_index/_search
{
  "query" : {
    "range" : {
      "time_frame" : { 
        "gte" : "2015-10-31",
        "lte" : "2015-11-01",
        "relation" : "within" 
      }
    }
  }
}

// 结果
{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lt" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}

ip范围

PUT range_index/_mapping
{
  "properties": {
    "ip_allowlist": {
      "type": "ip_range"
    }
  }
}

PUT range_index/_doc/2
{
  "ip_allowlist" : "192.168.0.0/16"
}

3.2 ip

一个ip字段可以索引/存储任一的IPv4IPv6的地址。

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "ip_addr": {
        "type": "ip"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "ip_addr": "192.168.1.1"
}

GET my-index-000001/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.0.0/16"
    }
  }
}

3.3 version - 软件版本

version字段类型是一个专门keyword领域处理软件版本值,并为他们支持专业优先级规则。优先级是按照语义版本控制概述的规则定义的 ,例如,这意味着主要版本,次要版本和补丁版本部分按数字排序(即“ 2.1.0” <“ 2.4.1” <“ 2.11.2”)并预先发布版本在发布版本之前进行排序(即“ 1.0.0-alpha” <“ 1.0.0”)。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_version": {
        "type": "version"
      }
    }
  }
}

该字段提供与常规关键字字段相同的搜索功能。例如,可以使用matchterm查询搜索精确匹配,并支持前缀和通配符搜索。主要好处是range查询将遵循Semver排序,因此range“ 1.0.0”和“ 1.5.0”之间的查询将包括“ 1.2.3”的版本,但不包括“ 1.11.2”的版本。请注意,当使用常规keyword字段进行索引(其中顺序是字母顺序)时,这将有所不同。

预期软件版本将遵循 语义版本控制规则架构和优先级规则,但值得注意的例外是允许使用的主要版本标识符多于或少于三个(即“ 1.2”或“ 1.2.3.4”属于有效版本,而它们不属于严格的Semver规则)。在Semver定义下无效的版本字符串(例如“ 1.2.alpha.4”)仍可以作为精确匹配进行索引和检索,但是它们将以规则的字母顺序出现任何有效的版本之后。空字符串“”被认为是无效的,并且在所有有效版本之后但在其他无效版本之前进行排序。

4 汇总数据类型

4.1 aggregate_metric_double - 汇总指标类型

存储度量聚合的预聚合数值。一个aggregate_metric_double字段是包含以下数据的子字段的一个或多个的对象:minmaxsum,和 value_count

aggregate_metric_double字段上运行某些度量标准聚合时,该聚合将使用相关子字段的值。例如,一个 字段min上的聚合 aggregate_metric_double返回所有min 子字段的最小值。

参数值:

etrics

(必需,字符串数组)要存储的度量标准子字段的数组。每个值都对应一个 度量聚合。有效值是 minmaxsum,和 value_count。您必须至少指定一个值。

default_metric

(必需,字符串)默认度量标准子字段,用于不使用子字段的查询,脚本和聚合。必须是metrics数组中的值。

用途

  • 一个min聚集返回所有的最小值min子字段。
  • 一个max聚集返回所有的最大值max子字段。
  • 一个sum聚集返回所有的值的总和sum子字段。
  • 一个value_count 聚集返回所有的值的总和value_count子字段。
  • 一个avg聚集。没有 avg子字段;avg使用sumvalue_count指标计算汇总结果。要运行avg聚合,该字段必须同时包含sumvalue_countmetric子字段。

使用示例:

PUT my-index
{
  "mappings": {
    "properties": {
      "my-agg-metric-field": {
        "type": "aggregate_metric_double",
        "metrics": [ "min", "max", "sum", "value_count" ],
        "default_metric": "max"
      }
    }
  }
}

PUT stats-index/_doc/1
{
  "agg_metric": {
    "min": -302.50,
    "max": 702.30,
    "sum": 200.0,
    "value_count": 25
  }
}

PUT stats-index/_doc/2
{
  "agg_metric": {
    "min": -93.00,
    "max": 1702.30,
    "sum": 300.00,
    "value_count": 25
  }
}

// 检索
POST stats-index/_search?size=0
{
  "aggs": {
    "metric_min": { "min": { "field": "agg_metric" } },
    "metric_max": { "max": { "field": "agg_metric" } },
    "metric_value_count": { "value_count": { "field": "agg_metric" } },
    "metric_sum": { "sum": { "field": "agg_metric" } },
    "metric_avg": { "avg": { "field": "agg_metric" } }
  }
}

// 结果
{
...
  "aggregations": {
    "metric_min": {
      "value": -302.5
    },
    "metric_max": {
      "value": 1702.3
    },
    "metric_value_count": {
      "value": 50
    },
    "metric_sum": {
      "value": 500.0
    },
    "metric_avg": {
      "value": 10.0
    }
  }
}

GET stats-index/_search
{
  "query": {
    "term": {
      "agg_metric": {
        "value": 702.30
      }
    }
  }
}

// 结果
{
  ...
    "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "stats-index",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "agg_metric": {
            "min": -302.5,
            "max": 702.3,
            "sum": 200.0,
            "value_count": 25
          }
        }
      }
    ]
  }
}

4.2 histogram - 直方图类型

用于存储表示直方图的预聚合数值数据的字段。该数据是使用两个成对的数组定义的:

  • valuesdouble数字,代表直方图的桶。这些值必须按升序提供。
  • 相应countsinteger数字数组,表示每个存储桶中有多少个值。这些数字必须为正或零。

因为values数组中的元素与数组中相同位置的元素相对应,所以count这两个数组必须具有相同的长度。

用途

histogram字段主要用于聚合。为了使聚合更易于访问,histogram字段数据存储为二进制doc值,但未编制索引。它的大小(以字节为单位)最大为 13 * numValues,其中numValues为提供的数组的长度。

由于未对数据建立索引,因此只能将histogram字段用于以下聚合和查询:

使用示例:

  • my_histogramhistogram用于存储百分位数据的字段
  • my_textkeyword用于存储直方图标题的字段
PUT my-index-000001
{
  "mappings" : {
    "properties" : {
      "my_histogram" : {
        "type" : "histogram"
      },
      "my_text" : {
        "type" : "keyword"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "my_text" : "histogram_1",
  "my_histogram" : {
      "values" : [0.1, 0.2, 0.3, 0.4, 0.5], 
      "counts" : [3, 7, 23, 12, 6] 
   }
}

PUT my-index-000001/_doc/2
{
  "my_text" : "histogram_2",
  "my_histogram" : {
      "values" : [0.1, 0.25, 0.35, 0.4, 0.45, 0.5], 
      "counts" : [8, 17, 8, 7, 6, 2] 
   }
}

5 文字搜索类型

5.1 text - 文本类型

从Elasticsearch 5.4 版本开始, text取代了需要分词的string.

text的内容会被分词, 可以设置是否需要存储: "index": "true|false".
text类型的字段不能用于排序, 也很少用于聚合.

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "full_name": {
        "type":  "text"
      }
    }
  }
}

5.2 token_count - 计数数据类型

token_count类型用于统计字符串中的单词数量.

本质上是一个整数型字段, 接受并分析字符串值, 然后索引字符串中单词的个数.

使用示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "name": { 
        "type": "text",
        "fields": {
          "length": { 
            "type":     "token_count",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1
{ "name": "John Smith" }

PUT my-index-000001/_doc/2
{ "name": "Rachel Alice Williams" }

GET my-index-000001/_search
{
  "query": {
    "term": {
      "name.length": 3 
    }
  }
}

6 空间数据类型

6.1 geo_point - 地理点类型

地理点类型用于存储地理位置的经纬度对, 可用于:

  • 查找一定范围内的地理点;
  • 通过地理位置或相对某个中心点的距离聚合文档;
  • 将距离整合到文档的相关性评分中;
  • 通过距离对文档进行排序.
// 添加映射
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

// 存储经纬度
PUT my-index-000001/_doc/1
{
  "text": "Geo-point as an object",
  "location": { 
    // 纬度: latitude
    "lat": 41.12,
    // 经度: longitude
    "lon": -71.34
  }
}

// 经纬度字符串 纬度,经度
PUT my-index-000001/_doc/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" 
}

// 表示为geohash的地理位置
PUT my-index-000001/_doc/3
{
  "text": "Geo-point as a geohash",
  "location": "drm3btev3e86" 
}

// ["经度, 纬度"] 数组地理点参数
PUT my-index-000001/_doc/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] 
}

// 地理点,表示为格式为“ POINT(lon lat)”的知名文本POINT
PUT my-index-000001/_doc/5
{
  "text": "Geo-point as a WKT POINT primitive",
  "location" : "POINT (-71.34 41.12)" 
}

// 查询示例
GET my-index-000001/_search
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        // 地理盒子模型的上-左边
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        // 地理盒子模型的下-右边
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}

6.2 geo_shape - 地理形状类型

文档

6.3 point - 二位坐标类型

point数据类型有利于和搜索任意索引x, y落在在二维平面坐标系对。

您可以使用shape Query使用此类型查询文档 。

可以通过四种方式指定一个点,如下所示:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "point"
      }
    }
  }
}

// xy坐标
PUT my-index-000001/_doc/1
{
  "text": "Point as an object",
  "location": { 
    "x": 41.12,
    "y": -71.34
  }
}

// xy坐标字符串 "x,y"
PUT my-index-000001/_doc/2
{
  "text": "Point as a string",
  "location": "41.12,-71.34" 
}

// xy数组 [ x, y]
PUT my-index-000001/_doc/4
{
  "text": "Point as an array",
  "location": [41.12, -71.34] 
}

// 格式为“ POINT(x y)”的点
PUT my-index-000001/_doc/5
{
  "text": "Point as a WKT POINT primitive",
  "location" : "POINT (41.12 -71.34)" 
}

6.4 shape - 形状类型

shape数据类型有助于索引和搜索任意x,y直角坐标形状,例如矩形和多边形。 它可用于索引和查询其坐标属于二维平面坐标系的几何。

文档

7 数组类型

ES中没有专门的数组类型, 直接使用[]定义即可;

数组中所有的值必须是同一种数据类型, 不支持混合数据类型的数组:

  1. 字符串数组: [ "one""two"];
  2. 整数数组:[ 12];
  3. 由数组组成的数组:[ 1[ 23]], 等价于[ 123];
  4. 对象数组: [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }]

注意:

  • 动态添加数据时, 数组中第一个值的类型决定整个数组的类型;
  • 不支持混合数组类型, 比如[1, “abc”];
  • 数组可以包含null值, 空数组[]会被当做missing field —— 没有值的字段.

文章作者: Cody_
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Cody_ !
评论
 上一篇
Security前后端分离多种认证方式 Security前后端分离多种认证方式
Security在现阶段的开发中使用频率非常高,用于权限认证;项目前后端分离的认证,前端登录也不仅仅是单一的账号密码登录,常常会有验证码登录以及各种第三方登录等等,下面我们就一一讲解Security的实现方式。 源码地址:戳我查看 1. 认
2021-01-03
下一篇 
Mysql同时使用分页和排序数据重复丢失问题 Mysql同时使用分页和排序数据重复丢失问题
记录一个很常见的MySQL问题,同时使用limit和order by可能造成数据查询重复和数据丢失问题。我这里测试的MySQL版本号是5.7.20 先来看看表结构: id(int) name(varchar) sort(int)
2020-12-17
  目录