二、elasticsearch索引基本操作
一、相关概念
1.1 结构类型
索引(index):相对应于数据库
类型(type): 相对应于数据库中的表
文档(document): 相对应于数据库中的表的行
属性(field): 相对应于数据库中的表中行的列
结构如下:
二,常用的restful语法(es7)
1.索引的操作
查询所有索引
http://ip:9200/_cat/indices
新增索引文档,即可自动增加一个索引,索引的字段类型会自动猜测
#put或post 索引名/类型名/id
put http://ip:9200/customer2/external2/1
#body json
{ "name":"John Doe1", "age":20 }
返回结果:
{ "_index": "customer2", "_type": "external2", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
#put /索引名
put http://ip:9200/customer3
#body json
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "keyword"
}
}
}
}
返回结果:
{ "acknowledged": true, "shards_acknowledged": true, "index": "customer3" }
查询单个索引
get /索引名
get http://ip:9200/customer2
返回结果:
{ "customer2": { "aliases": {}, "mappings": { "properties": { "age": { "type": "long" }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "settings": { "index": { "creation_date": "1610502090644", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "0DPrhl5tRuaUZX-N4oplbQ", "version": { "created": "7060299" }, "provided_name": "customer2" } } } }
修改索引:可新增新的字段映射,对于已经存在的字段映射,我们不能更新。更新必须创建新的索引,进行数据迁移。
新增新的字段映射
#put /索引名/_mapping
put http://ip:9200/customer3/_mapping
#body json
{
"properties": {
"city": {
"type": "keyword",
"index": false
}
}
}
返回结果
{ "acknowledged": true }
修改字段映射,先创建新的索引
数据迁移
POST _reindex [固定写法] { "source":{ "index":"twitter"//旧索引 }, "dest":{ "index":"new_twitters"//新索引 } }
由此,不能改变字段名
删除索引
#delete /索引名
delete http://ip:9200/customer4
返回结果
{ "acknowledged": true }