大資料Elasticsearch之Elasticsearch基本操作
阿新 • • 發佈:2019-12-31
文章目錄
1.索引的基本操作
1.1新建 Index
1.2刪除 Index
1.3新增記錄
1.4檢視記錄
1.5刪除記錄
1.6更新記錄
2.資料查詢
2.1返回所有記錄
2.2全文搜尋
1. 索引的基本操作
1.1 新建 Index
可以直接向 Elastic 伺服器發出 PUT 請求
新建一個名叫 weather 的 Index
$ curl -X PUT 'localhost:9200/weather'
複製程式碼
伺服器返回一個 JSON 物件,裡面的 acknowledged 欄位表示操作成功。
{ "acknowledged":true,"shards_acknowledged":true }
1.2 刪除 Index
發 DELETE 請求刪除即可
$ curl -X DELETE 'localhost:9200/weather'
複製程式碼
1.3 新增記錄
PUT請求 指定 id 新增記錄,id 為字串即可。
$ curl -X PUT 'localhost:9200/accounts/1' -d ' { "user": "張三","title": "工程師","desc": "資料庫管理" }'
複製程式碼
POST請求則會自動生成隨機字串 id
1.4 檢視記錄
向/Index/Type/Id發出 GET 請求,引數 pretty=true 表示以易讀的格式返回
$ curl 'localhost:9200/accounts/1?pretty=true'
複製程式碼
1.5 刪除記錄
發出 DELETE 請求
$ curl -X DELETE 'localhost:9200/accounts/1'
複製程式碼
1.6 更新記錄
使用 PUT 請求,重新傳送一次資料,返回的json資料會相應地發生變化:Id 沒變,版本(version)從1變成2,操作型別(result)從created變成updated,created欄位變成false
2. 資料查詢
2.1 返回所有記錄
GET 請求 /Index/Type/_search
返回的資料中 預設按 _score 相關度進行排序
2.2 全文搜尋
獨特的查詢語法,要求 GET 請求帶有資料體,預設返回10條資料,可通過 size 欄位改變,可通過 from 欄位指定其實位置
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "軟體" }},"from": 1,"size": 20 }'
複製程式碼