一、下載安裝
下載地址: https://www.elastic.co/cn/downloads/elasticsearch (目前最新版本為7.8)
本文以mac版本為例,下載后解壓即可。
終端命令行直接輸入
./elasticsearch-7.8.0/bin/elasticsearch
即可啟動,停止的話直接kill進程。
啟動成功后,可以瀏覽http://localhost:9200/,如果看出類似下面的輸出,就表示ok了
{ "name" : "lpt45125.local", "cluster_name" : "elasticsearch", "cluster_uuid" : "W56eUV3PTemAWBwmEx7CcQ", "version" : { "number" : "7.8.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65", "build_date" : "2020-06-14T19:35:50.234439Z", "build_snapshot" : false, "lucene_version" : "8.5.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
二、安裝瀏覽器插件(可選)
google瀏覽器上建議安裝2個插件:
有了這2個后,可以直觀的在瀏覽器中用可視化方式管理ES。這2個插件的效果如下:
可以看出ES集群的健康狀況,索引基本信息,包括瀏覽索引里的數據。
三、基本操作
3.1 創建索引
ES中的索引類似mysql中的table,是用來存儲數據的基本單位。
PUT http://localhost:9200/cnblogs (注:cnblogs為要創建的索引名,可自行命名)
{ "mappings": { "properties": { "blog_id": { "type": "long" }, "blog_title": { "type": "text" }, "blog_category": { "type": "keyword" }, "blog_content": { "type": "text" } } }, "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 0 } } }
向ES集群的url,put上述json,就會創建一個cnblogs的索引(相當於在mysql中建了一張名為cnblogs的表),只不過ES在“建表”時,還可以指定分片數和副本數(類似於mysql中的分庫分表個數,以及slave從庫個數)。mappings.properties節點的內容,相當於表中的字段定義。ES中的字段類型,可以參考 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
3.2 刪除索引
DELETE http://localhost:9200/cnblogs (注:cnblogs為要刪除的索引名)
用postman之類的,發一條DELETE操作即可,更多索引相關的操作,可參考 https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
3.3 添加單條記錄
注:在index中創建的數據,准確來講,ES中稱為document。
POST http://localhost:9200/cnblogs/_doc
{ "blog_id":10000001, "blog_title":"ES 7.8速成筆記", "blog_content":"這是一篇關於ES的測試內容by 菩提樹下的楊過", "blog_category":"ES" }
注1:上述語句執行后,該自動生成一個id,在返回結果中也能看到(即:下面的_id字段)
{ "_index": "cnblogs", "_type": "_doc", "_id": "RSYlZXMBw3XehDWN1Hbf", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
如果連續POST相同的內容,將生成多條內容一樣的記錄(即:重復記錄)
當然,也可以在POST時,就指定id,比如:
http://localhost:9200/cnblogs/_doc/123 (最后的123即為id)
{ "_index": "cnblogs", "_type": "_doc", "_id": "123", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 6, "_primary_term": 1 }
返回結果也能看到"_id":"123",如果重復提交1次,ES會自動認為是對_id:123的記錄做update,從返回結果也能看出來:
{ "_index": "cnblogs", "_type": "_doc", "_id": "123", "_version": 2, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 7, "_primary_term": 1 }
這時"result":"updated"已經不是再created,而且_version也變成了2。 更多創建文檔的細節,可以參考 https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
3.4 批量添加記錄
從mysql向ES遷移數據時,批量操作很有用,ES中稱為bulk操作,比如我們要向cnblogs,1次添加多條記錄:
POST http://localhost:9200/cnblogs/_bulk
{"index":{"_id":1}} {"blog_title":"第1篇標題","blog_content":"測試內容1"} {"index":{}} {"blog_id":11,"blog_title":"第2篇標題","blog_content":"測試內容2","blog_category":"test"}
注1:基本格式為2行,第1行為元數據定義,即:{"index":{"_id":1}},第2行為緊接下來的操作,即:{"blog_title":"第1篇標題","blog_content":"測試內容1"}
注2:最后要有一個空行(即:\n)
操作完成后,可以看到多了2行數據:
更多bulk操作的細節,可參考https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
3.5 刪除記錄
刪除單條記錄 DELETE http://localhost:9200/cnblogs/_doc/1 (最后的值為_id的值)
當然也可以用剛學到的bulk操作,批量刪除
PUT http://localhost:9200/cnblogs/_bulk
{"delete":{"_id":"mCZQZXMBw3XehDWN9HYJ"} } {"delete":{"_id":"mSZSZXMBw3XehDWNSnZT"} } {"delete":{"_id":"fiZHZXMBw3XehDWNkXYf"} } {"delete":{"_id":"lyZQZXMBw3XehDWN9HYJ"} }
3.6 更新記錄
更新操作跟mysql中的有很大區別,新手容易踩坑,先創建一條記錄,方便后面講解
POST http://localhost:9200/cnblogs/_doc/1
{ "blog_id":10000001, "blog_title":"ES 7.8速成筆記", "blog_content":"這是一篇關於ES的測試內容by 菩提樹下的楊過", "blog_category":"ES" }
完成后,就能看到這條記錄了
接下來,改下json內容,變成:
POST http://localhost:9200/cnblogs/_doc/1
{ "blog_title":"ES 7.8速成筆記(修改后的版本)" }
返回結果:
{ "_index": "cnblogs", "_type": "_doc", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
提示更新完成,但是看記錄,會發現其它字段值全被清掉了!
也就是說,如果我們只想更新某個字段的值,這樣是不行的,必須先從ES中查出舊記錄,然后所有字段值全賦上舊值,再更新指定字段,做全量提交才能達到預期,所以不推薦這種方法。
正確方法:
POST http://localhost:9200/cnblogs/_update/1
{ "doc": { "blog_title": "ES 7.8速成筆記(新標題)" } }
update細節可參考https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html