# 03_Elastic Stack 从入门到实践(三)-- 2
03_Elastic Stack 从入门到实践(三)-- 2
三、Elasticsearch核心讲解之映射
1、Elasticsearch映射
1)Elasticsearc创建的索引以及插入数据,都是由Elasticsearch进行自动判断类型,有些时候我们是需要进行明确字段类型的,否则,自动判断的类型和实际需求是不相符的。
2)Elasticsearc自动判断的规则如下:
JSON type | Field type |
---|---|
Boolean: true or false | “boolean” |
Whole number: 123 | “long” |
Floating point:123.45 | “double” |
String,valid date:“2014-09-15” | “date” |
String:“foo bar” | “string” |
3)Elasticsearch中支持的类型如下:
类型 | 表示的数据类型 |
---|---|
String | string,text,keyword |
Whole number | byte , short,integer,long |
Floating point | float, double |
Boolean | boolean |
Date | date |
4)string类型在ElasticSearch旧版本中使用较多,从ElasticSearch 5.x开始不再支持string,由text和keyword类型替代。
5)text类型,当一个字段是要被全文具索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索弓"以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合。
6)keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。
2、Elasticsearch创建明确类型的索引:
# 打开 Postman 软件,选择PUT请求,
# 地址栏输入:http://192.168.43.216:9200/dzs168
# 请求体为 JSON 数据类型,请求内容为以下:
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "0"
}
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"mail": {
"type": "keyword"
},
"hobby": {
"type": "text"
}
}
}
}
}
# 响应数据
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "dzs168"
}
3、Elasticsearch查看映射
# 打开 Postman 软件,选择GET请求,
# 地址栏输入:http://192.168.43.216:9200/dzs168/_mapping
# 请求体为空
# 响应数据
{
"dzs168": {
"mappings": {
"person": {
"properties": {
"age": {
"type": "integer"
},
"hobby": {
"type": "text"
},
"mail": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
}
}
4、Elasticsearch 向新创建的索引 dzs168 插入数据。
# 打开 Postman 软件,选择POST请求,
# 地址栏输入:http://192.168.43.216:9200/dzs168/_bulk
# 请求体为 JSON 数据类型,请求内容为以下:
{
"index": {
"_index": "dzs168", "_type": "person" } }
{
"name": "张三", "age": 20, "mail": "111@qq.com", "hobby": "看电影、听音乐"}
{
"index": {
"_index": "dzs1