首先看來創建一個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 類型)