1.ElasticSearch API 和操作索引
Rest风格的API,即http请求接口
1.1 基本概念
对比关系
索引集(indexs)–Databases数据库
类型(type)—Table数据库表
文档(Document)—Row行
字段(Field)—Columns列
一个索引库里面只有一种类型的文档,对应数据库中一种类型的数据
索引集(indexs):逻辑上的完整索引
分片(shard):数据拆分后的各个部分
副本(replica):每个分片的复制
1.2 创建索引
Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求
创建索引的请求格式
- 请求方式:PUT
- 请求路径:/索引名称
- 请求参数:json格式
insomnia请求:
{
"settings":{
"number_of_shards":3, //分片数量
"number_of_replicas":2 //副本数量
}
}
返回结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "heima"
}
表示创建索引成功
kibana请求:
PUT /heima2
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
返回结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "heima2"
}
使用kibana查询索引
请求:
GET heima
返回结果:
{
"heima": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1589011560927",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "cuHizgA1Tiaq9sfgO_lYEw",
"version": {
"created": "6020499"
},
"provided_name": "heima"
}
}
}
}
删除hema2
请求:
DELETE heima2
返回结果:
{
"acknowledged": true
}
在次查询heima2
请求:
GET heima2
返回结果:
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "heima2",
"index_uuid": "_na_",
"index": "heima2"
}
],
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "heima2",
"index_uuid": "_na_",
"index": "heima2"
},
"status": 404
}
1.3.创建映射字段
请