elasticsearch 5.x 系列之三 mapping 映射的时候的各个字段的设置

时间:2023-03-09 15:38:13
elasticsearch 5.x 系列之三 mapping 映射的时候的各个字段的设置

首先看来创建一个mapping 来show show:

curl -XPUT "master:9200/zebra_info?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards":5,
"number_of_replicas":1
},
"mappings": {
"zebra_info": {
"properties": {
"name" : {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fielddata": true, "fields": {"raw": {"type":"keyword"}}},
"firstly_classification": {"type": "keyword"},
"secondary_classification": {"type": "keyword"},
"type_name": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fielddata": true,"fields": {"raw": {"type":"keyword"}}},
"province": {"type": "keyword"},
"city": {"type": "keyword"},
"citycode": {"type": "keyword"},
"district": {"type": "keyword"},
"adcode": {"type": "keyword"},
"township": {"type": "text"},
"business_circle": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fielddata": true,"fields": {"raw": {"type":"keyword"}}},
"formatted_address": {"type": "text"},
"location": {"type": "geo_point"},
"extensions": {
"type": "nested",
"properties": {
"avg_price": {"type": "double"},
"shops": {"type":"integer"},
"good_comments": {"type":"byte"},
"lvl": {"type":"byte"},
"other_type": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fielddata": true, "fields": {"raw": {"type":"keyword"}}},
"numbers": {"type": "integer"}
}
}
}
}
}
}
'

二、show 看过了,来看一下elasticsearch 支持的数据类型。


1,text:
当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合(termsAggregation除外)。
如果要聚合,请设置成keyword 参照上面的索引,设置一个fielddata。 聚合或者排序的时候用name.raw 进行排序。
"name" : {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fielddata": true, "fields": {"raw": {"type":"keyword"}}} 2,keyword:
keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。 3,数据类型 范围
long -2^63至2^63-1
integer -2^31至2^31-1
short -32,768至32768
byte -128至127
double 64位双精度IEEE 754浮点类型
float 32位单精度IEEE 754浮点类型
half_float 16位半精度IEEE 754浮点类型
scaled_float 缩放类型的的浮点数(比如价格只需要精确到分,price为57.34的字段缩放因子为100,存起来就是5734)相当于可以定义精确度
用法如下:
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"number_of_bytes": {
"type": "integer"
},
"time_in_seconds": {
"type": "float"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
}
} 4,object 类型 或者说是嵌套类型。定义参见文章标题给出的索引
PUT my_index/my_type/1
{
"region": "US",
"manager": {
"age": 30,
"name": {
"first": "John",
"last": "Smith"
}
}
} 5, 日期类型
支持的格式如下:
日期格式的字符串:e.g. “2015-01-01” or “2015/01/01 12:10:30”.
long类型的毫秒数( milliseconds-since-the-epoch)
integer的秒数(seconds-since-the-epoch)
举栗子如下:
PUT my_index/my_type/1
{ "date": "2015-01-01" }
PUT my_index/my_type/2
{ "date": "2015-01-01T12:10:30Z" }
PUT my_index/my_type/3
{ "date": 1420070400001 } 6,Array类型
ELasticsearch没有专用的数组类型,默认情况下任何字段都可以包含一个或者多个值,但是一个数组中的值要是同一种类型。例如:
字符数组: [ “one”, “two” ]
整型数组:[1,3]
嵌套数组:[1,[2,3]],等价于[1,2,3]
对象数组:[ { “name”: “Mary”, “age”: 12 }, { “name”: “John”, “age”: 10 }]
注意事项:
动态添加数据时,数组的第一个值的类型决定整个数组的类型
混合数组类型是不支持的,比如:[1,”abc”]
数组可以包含null值,空数组[ ]会被当做missing field对待。 7,geo 类型,可以是点,线,或者面(区域)
地理位置信息类型用于存储地理位置信息的经纬度。 8,其他不常用的类型。
range 类型
integer_range -2^31至2^31-1
float_range 32-bit IEEE 754
long_range -2^63至2^63-1
double_range 64-bit IEEE 754
date_range 64位整数,毫秒计时 ip 类型,binary 类型,token_count 类型, nested类型类型(特殊的object 类型)