文章目录
- 1, 动态映射:Dynamic mapping (动态确定字段类型,以第一个值为参照)
- 2, 显示映射:Explicit mapping (预先规定字段类型)
- 3, 使用kibana:可视化查看elasticsearch数据
1, 动态映射:Dynamic mapping (动态确定字段类型,以第一个值为参照)
Elasticsearch 默认不需要(事先建好索引,确定字段名和字段类型,然后才能添加数据),而是可以直接添加数据,并根据数值来动态确定字段类型https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html
- json(-string) int --> long
- json double --> float
- json boolean --> boolean
- json array --> 根据元素类型确定类型
- json object --> 嵌套的对象类型(内部字段类型按对应规则确定)
- json-string date-- > date : 默认格式为 yyyy-MM-dd 或 yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis
示例如下:
# 1,添加一条数据(完成的动作:创建索引名,并根据kv对来确定字段和数值类型,然后添加数据)
curl -X POST "localhost:9200/test_dynamic_mapidx/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"id": 1,
"age": "23",
"name": "name1",
"is_rich_man":false,
"salary": 13210.23,
"birth_year": "1992-01-02",
"company": {"location":"bj", "house":"123123" },
"hobby":["books","runngin","movie"],
"family_member":[{
"name":"lisi",
"role":"son"
},
{
"name":"xiaoli",
"role":"wife",
"age":33
}]
}
'
# 2,查看索引的字段隐射结果
[root@hadoop01 ~]# curl -X GET "192.168.56.1:9200/test_dynamic_mapidx/_mapping?pretty"
{
"test_dynamic_mapidx" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"birth_year" : {
"type" : "date"
},
"company" : {
"properties" : {
"house" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"location" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"family_member" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"role" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"hobby" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "long"
},
"is_rich_man" : {
"type" : "boolean"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"salary" : {
"type" : "float"
}
}
}
}
}
2, 显示映射:Explicit mapping (预先规定字段类型)
设计索引(名称+ 字段类型):https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
# 1,创建索引名
curl -X PUT "localhost:9200/test_idx1?pretty"
# 2,更新已存在索引,添加字段和映射类型: date类型默认是yyyy-MM-dd 或 yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis
# text 类型适合用于 full-text搜索,keyword 类型适合用于排序/聚合: While text fields work well for full-text search, keyword fields are not analyzed and may work better for sorting or aggregations.
curl -X PUT "localhost:9200/test_idx1/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"id": { "type": "long"},
"name": { "type": "text"},
"age": { "type": "long"},
"birth": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"ignore_malformed": false,
"null_value": null
}
}
}
'
# 3,查看字段映射规则
curl -X GET "localhost:9200/test_idx1/_mapping?pretty"
# 4,添加数据
curl -X POST "localhost:9200/test_idx1/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"id": 1,
"name": "name1",
"age": 23,
"birth": "1992-01-02 09:01:10"
}
'
3, 使用kibana:可视化查看elasticsearch数据
kibana下载:https://www.elastic.co/cn/downloads/kibana
- 配置kibana.yml,并启动
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
# Supported languages are the following: English - en , by default , Chinese - zh-CN .
#i18n.locale: "en"
使用kinana查看elasticsearch数据 https://www.elastic.co/guide/en/kibana/current/discover.html