1. 新增文件,建立索引
語法格式:
PUT /index/type/id
{
"json資料"
}
複製程式碼
輸入:
PUT /person/chinese/1
{
"id":12345,
"name":"lujiahao",
"age":18
}
複製程式碼
輸出:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
複製程式碼
es會自動建立index和type,不需要提前建立,而且es預設會對document每個field都建立倒排索引,讓其可以被搜尋。
2. 檢索文件
格式:
GET /index/type/id
複製程式碼
輸入:
GET /person/chinese/1
複製程式碼
輸出:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 12345,
"name": "lujiahao",
"age": 18
}
}
複製程式碼
3. 更新文件
3.1 替換方式
格式:
PUT /index/type/id
{
"json資料"
}
複製程式碼
輸入:
PUT /person/chinese/1
{
"name":"lujiahao123"
}
複製程式碼
輸出:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
複製程式碼
查詢:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "lujiahao123"
}
}
複製程式碼
替換方式更新文件時,必須帶上所有的field,才能去進行資訊的修改;如果缺少field就會丟失部分資料。其原理時替換,因此需要全部欄位。不推薦此種方式更新文件。
3.1 更新方式
格式:
POST /index/type/id/_update
{
"doc":{
"json資料"
}
}
複製程式碼
輸入:
POST /person/chinese/1/_update
{
"doc":{
"name":"lujiahao10010"
}
}
複製程式碼
輸出:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
複製程式碼
再次查詢:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 6,
"found": true,
"_source": {
"id": 12345,
"name": "lujiahao10010",
"age": 18
}
}
複製程式碼
4. 刪除文件
格式:
DELETE /index/type/id/_update
{
"doc":{
"json資料"
}
}
複製程式碼
輸入:
DELETE /person/chinese/1
複製程式碼
輸出:
{
"found": true,
"_index": "person",
"_type": "chinese",
"_id": "1",
"_version": 7,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
複製程式碼
再次查詢:
{
"_index": "person",
"_type": "chinese",
"_id": "1",
"found": false
}
複製程式碼
5. 小結
本文所有操作都是在kibana的Dev tools中進行的,相較於Elasticsearch-Heade外掛,kibana中更加方便與美觀(個人觀點),推薦大家使用。
Tips
本文同步發表在公眾號,歡迎大家關注!? 後續筆記歡迎關注獲取第一時間更新!