萬丈高樓平地起html
ES提供了多種操做數據的方式,其中較爲常見的方式就是RESTful風格的API。程序員
簡單的體驗數據庫
利用Postman發起HTTP請求(固然也能夠在命令行中使用curl命令)。json
建立一個名叫demo
的索引:api
PUT http://localhost:9200/demo
app
ES響應:curl
{ "acknowledged": true, "shards_acknowledged": true, "index": "demo" }
在建立索引時,可指定主分片和分片副本的數量:elasticsearch
PUT http://localhost:9200/demo
ide
{ "settings":{ "number_of_shards":1, "number_of_replicas":1 } }
ES響應:ui
{ "acknowledged": true, "shards_acknowledged": true, "index": "demo" }
GET http://localhost:9200/demo
ES響應:
{ "demo": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1561110747038", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "kjPqDUt6TMyywg1P7qgccw", "version": { "created": "5060499" }, "provided_name": "demo" } } } }
查詢ES中索引狀況:
GET http://localhost:9200/_cat/indices?v
ES響應:
health | status | index | uuid | pri | rep | docs.count | docs.deleted | store.size | pri.store.size |
---|---|---|---|---|---|---|---|---|---|
yellow | open | demo | wqkto5CCTpWNdP3HGpLfxA | 5 | 1 | 0 | 0 | 810b | 810b |
yellow | open | .kibana | pwKW9hJyRkO7_pE0MNE05g | 1 | 1 | 1 | 0 | 3.2kb | 3.2kb |
能夠看到當前ES中一共有2個索引,一個是咱們剛建立的demo
,另外一個是kibana建立的索引.kibana
。表格中有一些信息表明了索引的一些狀態。
health:健康狀態,red表示不是全部的主分片均可用,即部分主分片可用。yellow表示主分片可用備分片不可用,經常是單機ES的健康狀態,greens表示全部的主分片和備分片均可用。(官方對集羣健康狀態的說明,https://www.elastic.co/guide/en/elasticsearch/guide/master/cluster-health.html)
status:索引狀態,open表示打開可對索引中的文檔數據進行讀寫,close表示關閉此時索引佔用的內存會被釋放,可是此時索引不可進行讀寫操做。
index:索引
uuid:索引標識
pri:索引的主分片數量
rep:索引的分片副本數量,1表示有一個分片副本(有多少主分片就有多少備分片,此處表示5個備分片)。
docs.count:文檔數量
docs.deleted:被刪除的文檔數量
store.size:索引大小
pri.store.size:主分片佔用的大小
刪除demo
索引,刪除索引等同於刪庫跑路,請謹慎操做。
DELETE http://localhost:9200/demo
ES響應:
{ "acknowledged": true }
在前面基本術語中咱們提到類型Type相似關係型數據庫中的表,映射Mapping定義表結構。建立類型Type時須要配合映射Mapping。
建立索引demo
的類型爲example_type
,包含兩個字段:created
類型爲date,message
類型爲keyword:
方式一:
PUT http://localhost:9200/demo/_mapping/example_type { "properties":{ "created":{ "type":"date" }, "message":{ "type":"keyword" } } }
此時再次執行查詢索引的操做,已經能夠發現類型Type被建立了,遺憾的是,若是類型Type(或者映射Mapping)一旦定義,就不能刪除,只能修改,爲了保證本教程順利進行方式二建立類型,因此此處執行DELETE http://localhost:9200/demo
刪除索引。刪除索引後不要再建立索引,下面的這種方式是在建立索引的同時建立Type並定義Mapping
方式二:
PUT http://localhost:9200/demo { "mappings":{ "example_type":{ "properties":{ "created":{ "type":"date" }, "message":{ "type":"keyword" } } } } }
此時執行GET http://localhost:9200/demo
,能夠看到咱們在ES中建立了第一個索引以及建立的表結構,接下來插入就是數據(即文檔)。
系統定義_id
POST http://localhost:9200/demo/example_type { "created":1561135459000, "message":"test1" }
ES響應:
{ "_index": "demo", "_type": "example_type", "_id": "AWt67Ql_Tf0FgxupYlBX", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
ElasticSearch的核心功能——搜索。
POST http://localhost:9200/demo/example_type/_search?pretty
ES響應:
{ "took": 183, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "demo", "_type": "example_type", "_id": "AWt67Ql_Tf0FgxupYlBX", "_score": 1, "_source": { "created": 1561135459000, "message": "test1" } } ] } }
關於文檔的查詢是ElasticSearch的核心,後面的章節會詳細介紹一些基本的簡單查詢和更爲高級的複雜查詢,此處僅做爲對插入數據的驗證,不作過多展開。
根據文檔_id
修改
POST http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX/_update { "doc":{ "message":"updated" } }
ES響應:
{ "_index": "demo", "_type": "example_type", "_id": "AWt67Ql_Tf0FgxupYlBX", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } }
刪除_id
爲AWt67Ql_Tf0FgxupYlBX的文檔
DELETE http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX
ES的響應:
{ "found": true, "_index": "demo", "_type": "example_type", "_id": "AWt67Ql_Tf0FgxupYlBX", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 } }
關注公衆號:CoderBuff,回覆「es」獲取《ElasticSearch6.x實戰教程》完整版PDF。