ES的最重要的特性之一就是尽量方便的让我们可以直接开始explore your data。在index一个doc时,不需要先去create一个索引、定义索引的mapping。可以直接索引一个文档。
//索引这个文档不需要任何前置工作,ES自动帮你做了PUT data/_doc/1{ "count": 5 }
自动检测和增加新field被叫做dynamic mapping(动态mapping,可变的,可动态更新的)。并且dynamic mapping是可以根据自己的目的通过dynamic field mapping和dynamic templates自定义的。
dynamic field mapping
默认情况下,一个文档中新出现的field会被es添加到type mapping中。这个行为是可以通过dynaic参数被disable掉的。
json数据的类型和ES field类型的映射表如下:
| JSON data type | Elasticsearch data type |
|---|---|
null |
No field is added. |
true or false |
[boolean](https://www.elastic.co/guide/en/elasticsearch/reference/current/boolean.html) field |
| floating point number | [float](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html) field |
| integer | [long](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html) field |
| object | [object](https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html) field |
| array | Depends on the first non-null value in the array. |
| string | Either a [date](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html) field (if the value passes date detection), a [double](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html) or [long](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html) field (if the value passes numeric detection) or a [text](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html) field, with a [keyword](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) sub-field. |
只有上述这些类型会被动态添加。其他类型需要使用dynamic_template显式地进行映射。
date detection
es会自动检查输入的string是否满足默认的日期格式,如果满足的话就会被映射成为date类型。日期格式的默认格式是dynamic_date_formats,其值为[ ["strict_date_optional_time"](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#strict-date-time),"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"].默认开,可以关闭。
PUT my-index-000001
{
"mappings": {
"date_detection": false
}
}
PUT my-index-000001/_doc/1
{
"create": "2015/09/02"
}
PUT my-index-000001
{
"mappings": {
//自定义日期格式
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
PUT my-index-000001/_doc/1
{
"create_date": "09/25/2015"
}
numeric detection
json格式的int,float是可以被自动映射的,但是有的时候int和float是以字符串形式给出的,像这些string类型的数字需要显式地mapping。
PUT my-index-000001
{
"mappings": {
"numeric_detection": true
}
}
PUT my-index-000001/_doc/1
{
//开启了numeric detection可以自动对字符串类型的数值类型作mapping
"my_float": "1.0",
"my_integer": "1"
}
dynamic templates
这个一般用不到就不看了,以后再说
