您的位置:首页 > 移动开发

mapping 详解2(field datatypes)

2016-05-12 19:52 441 查看

基本类型

1. 字符串

字符串类型被分为两种情况:full-text 和 keywords。

full-text 表示字段内容会被分析,而 keywords 表示字段值只能作为一个精确值查询。

参数:

analyzer
boost
doc_values
fielddata
fields
ignore_above
include_in_all
index
index_options
norms
null_value
position_increment_gap
store
search_analyzer
search_quote_analyzer
similarity
term_vector


2. 数值

数值类型包括: long, integer, short, byte, double, float 。

参数:

coerce
boost
doc_values
ignore_malformed
include_in_all
index
null_value
precision_step
store


3. 日期

JSON 本身并没有日期数据类型,在 ES 中的日期类型可以是:

类似
"2015-01-01"
or
"2015/01/01 12:10:30" 的字符串


long 类型的毫秒级别的时间戳


int 类型的秒级别的时间戳


日期类型默认会被转换为 UTC 并且转换为毫秒级别的时间戳的 long 类型存储。


日期类型如果不指定 format ,将会以默认格式表示。


参数:

boost
[code]doc_values
format
ignore_malformed
include_in_all
index
null_value
precision_step
store
[/code]

4. 布尔

布尔假:
false
,
"false"
,
"off"
,
"no"
,
"0"
,
""
(empty string),
0
,
0.0 。


布尔真:    任何不为假的值。


像 terms aggregation 聚合,是使用 1 和 0 来作为 key 的,key_as_string 则是用字符串 true 和 false


布尔类型的值,在 scripts 中则始终返回 1 或 0


参数:

boost
[code]doc_values
index
null_value
store
[/code]

5. 二进制

二进制类型以 Base64 编码方式接收一个二进制值,二进制类型字段默认不存储,也不可搜索。

参数:
doc_values
store


复杂类型

1. 对象

JSON 格式本身是分层级的——文档可以包含对象,对象还可以包含子对象。不过,在 ES 内部 "对象" 被索引为一个扁平的键值对。

例如:

PUT my_index/my_type/1
{
"region": "US",
"manager": {
"age":     30,
"name": {
"first": "John",
"last":  "Smith"
}
}
}


转换为:

{
"region":             "US",
"manager.age":        30,
"manager.name.first": "John",
"manager.name.last":  "Smith"  //层级结构被以 "." 来表示。
}


2. 数组

数组类型,要求数组元素的数据类型必须一致。

字符串数组: [
"one"
,
"two"
]

数字数组: [
1
,
2
]

数组数组: [
1
, [
2
,
3
]] which is the equivalent of [
1
,
2
,
3
]

对象数组: [
{ "name": "Mary", "age": 12 }
,
{ "name": "John", "age": 10 }
]

数组元素的数据类型,将会由其第一个元素的数据类型决定。

对象数组,在 ES 内部将会被转换为 "多值" 的扁平数据类型。后面将会详解这一点。

例如:

PUT my_index/my_type/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" :  "Smith"
},
{
"first" : "Alice",
"last" :  "White"
}
]
}


转转为:

{
"group" :        "fans",
"user.first" : [ "alice", "john" ],
"user.last" :  [ "smith", "white" ]
}


3. 对象数组

对象数组在 ES 内部,会把所有数组元素(即对象)合并,对象中的每一个字段被索引为一个 "多值" 字段。这将导致每个数组元素(对象)内部的字段关联性丢失,解决的方法是使用 nested 类型

例如:

PUT my_index/my_type/1
{
"region": "US",
"manager": {
"age":     30,
"name": [
{
"first": "John",
"last":  "Smith"
},
{
"first": "Bob",
"last":  "Leo"
}
]
}
}


转换为:

{
"region":             "US",
"manager.age":        30,
"manager.name.first": "John Bob",
"manager.name.last": "Smith Leo"
}
// 如果我们搜索:
"bool": {
"must": [
{ "match": { "manager.name.first": "John" }},   // John Smith
{ "match": { "manager.name.last": "Leo"}}       // Bob Leo
]
}
//这将会导致导致文档被命中,显然,John Smith 、Bob Leo 两组字段它们内在的关联性都丢失了


参数:

dynamic
enabled
include_in_all
properties


4. 嵌套(nested)

嵌套类型是一个特殊对象类型,嵌套类型允许对对象数组的每一个元素(对象)相互独立的进行查询,也即他们不会被合并为一个对象。

嵌套类型的文档可以:

nested
查询来查询

nested
来分析以及
reverse_nested
来聚合

nested sorting 来排序

nested inner hits 来检索或高亮

例如:

PUT my_index/my_type/1
{
"region": "US",
"manager": {
"age":     30,
"name": [
{
"first": "John",
"last":  "Smith"
},
{
"first": "Bob",
"last":  "Leo"
}
]
}
}


转换为:

{
"region":             "US",
"manager.age":        30,
{
"manager.name.first": "John",
"manager.name.last": "Smith"
},
{
"manager.name.first": "Bob",
"manager.name.last": "Leo"
}
}
// 如果我们搜索:
"bool": {
"must": [
{ "match": { "manager.name.first": "John" }},   // John Smith
{ "match": { "manager.name.last": "Leo"}}       // Bob Leo
]
}
//这样的查询将不能命中文档!!!


参数:

dynamic
include_in_all
properties


专有类型

1. IPV4类型

IPV4 数据类型其实质是个 long 类型,不过其能接收一个 IPV4 地址并且将他转换为 long 类型存放。

参数:

boost
doc_values
include_in_all
index
null_value
precision_step
store
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: