这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作
索引的初始化操作
创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量
代码
PUT http://192.168.2.218:9200/library/
{
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
}
}
结果
#执行成功返回
{
"acknowledged": true
}
创建索引
代码
-----索引名称
| -----type名称
| | ----文档ID
| | |
| | |
| | |
| | |
| | |
| | |
PUT /library/books/2
{
"title":"Elasticsearch:the definitive guide",
"name":{
"first":"zachary",
"last":"tong",
},
"publish_date":"2017-02-19",
"price":"49.99"
}
结果
{
"_index": "library",
"_type": "books",
"_id": "1",
"_version": 1,
"created": true
}
查看索引
代码
GET /library/books/1
结果
{
"_index": "library",
"_type": "books",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"title": "Elasticsearch:the definitive guide",
"name": {
"first": "zachary",
"last": "tong"
},
"publish_date": "2017-02-19",
"price": "49.99"
}
}
修改/更新索引
代码
POST /library/
把原来的值直接改为新的值就是更新,比如"price":"49.99"的49.99改为59.99,就是"price":"59.99"
删除索引
代码
DELETE /library/books/1
结果
{
"found": true,
"_index": "library",
"_type": "books",
"_id": "1",
"_version": 2
}
Elasticsearch自带的字段
_index | 代表索引名 |
_type | 代表类型 |
_id | 代表文档id,如果插入文档的时候没有设置id的话,那么es会自动生成一个唯一id |
_score | 这个不是文档自带的,而是进行搜索的时候返回的,代表这个文档和搜索的相关匹配分值(elasticsearch给文档打分,分值越高就越靠前,分值越高越有价值) |
_source | 储存原始文本及分类好的字段(文档的具体内容) |
_version | 代表这个文档的版本 |
解释
这里的索引,类型,文档,字段的概念很多文章都做一个关系型数据的对比。
我现在有一个user表,这个user表有个type字段,0/1代表是男还是女,这个表的每条数据就代表一个人,它拥有名称,电话等属性。
对应于es,表就相当于索引,男女的字段相当于type,每条数据就是一个document,名称电话等属性就是一个字段