上節對ES的安裝進行了描述, 這節來具體看下如何對ES進行操做html
點擊此處查看ES的安裝數據庫
ES是基於RESTful風格方式來進行操做的, 有關RESTful相關的知識,本人對RESTful只停留在用上, 這裏推薦你們查看 大神之做api
下面來看咱們對ES的實際操做bash
接下來咱們操做ES, 在實際操做中對涉及到的概念進行描述restful
這裏咱們以個人小說項目爲例app
建立索引庫elasticsearch
PUT http://sanq1.com.cn:9200/books
複製代碼
輸出:ide
{
"acknowledged": true,
"shards_acknowledged": true
}
複製代碼
索引是含有相同屬性的文檔集合, 例如咱們的小說信息就會存在這個索引庫裏面post
這裏索引庫至關於在關係型數據庫中的庫
,ui
在實際操做中, 若是該索引庫不存在, API會自動建立該索引以及該特定JSON對象的基礎映射。
若是須要取消, 能夠在elasticsearch.yml
配置文件中進行設置
action.auto_create_index:false
index.mapper.dynamic:false
複製代碼
保存數據
POST http://sanq1.com.cn:9200/books/books/1
{
"id": "1",
"booksName": "鬥破蒼穹",
"author": "天蠶土豆"
}
複製代碼
輸出:
{
"_index": "books",
"_type": "books",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
複製代碼
在這裏存在一個叫類型
的概念, 這裏至關於在關係型數據庫中的 表
只有有這個表了, 數據纔可以存儲到ES中
JSON中key是ES中的文檔
文檔是能夠被索引存儲的基本數據單位 至關於表中的 表
中的字段
修改數據
PUT http://sanq1.com.cn:9200/books/books/1
{
"id": "1",
"booksName": "鬥破蒼穹111",
"author": "天蠶土豆111"
}
複製代碼
輸出:
{
"_index": "books",
"_type": "books",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
複製代碼
查詢當前數據
GET http://sanq1.com.cn:9200/books/books/1
複製代碼
輸出:
{
"_index": "books",
"_type": "books",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"id": "1",
"booksName": "鬥破蒼穹111",
"author": "天蠶土豆111"
}
}
複製代碼
若是咱們想顯示指定的字段, 咱們能夠這樣來查詢
GET http://sanq1.com.cn:9200/books/books/1_source=booksName&pretty
複製代碼
輸出:
{
"_index": "books",
"_type": "books",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"booksName": "鬥破蒼穹"
}
}
複製代碼
查詢數據並分頁
GET http://sanq1.com.cn:9200/books/books/_search
{
"query": {
"match": {
"author": "辰"
}
},
"from": 1, //從那條數據開始
"size": 2, //返回的條數
"sort": [
{"id.keyword": {"order": "desc"}}
]
}
複製代碼
輸出:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "books",
"_type": "books",
"_id": "3",
"_score": 0.51623213,
"_source": {
"id": "3",
"booksName": "完美世界",
"author": "辰東"
}
},
...
]
}
}
複製代碼
本人在排序的時候遇到了如下問題, 因此使用 id.keyword
直接使用text字段問題: Fielddata is disabled on text fields by default. Set fielddata=true on [id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.
刪除數據
DELETE http://sanq1.com.cn:9200/books/books/1
複製代碼
輸出:
{
"found": true,
"_index": "books",
"_type": "books",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
複製代碼
刪除索引庫
DELETE http://sanq1.com.cn:9200/books
複製代碼
輸出:
{
"acknowledged": true
}
複製代碼
這些就是簡單的基本操做, 你們能夠親自嘗試嘗試,
也推薦你們去ES的官網查看更多的操做