Elasticsearch 是目前很熱門的分散式搜尋引擎,為了讓自己有個環境方便研究 ,所以整理了一份可以在自己電腦上快速啟動 Elasticsearch 的 docker-compose.yml
Elasticsearch 本身提供 RESTful API 做為使用介面,如果想要視覺化的介面,需要再加上 Kibana。
這次建立服務會有 Elasticsearch + Kibana
建立 docker-compose.yml
完整檔案與設定說明如下
|
|
設定為單節點模式
1 2
environment: - discovery.type=single-node
關掉 SSL 加密傳輸 (這是為了讓架服務簡單一點)
1 2
environment: - xpack.security.enabled=false
限制記憶體用量
1 2 3 4
deploy: resources: limits: memory: 1GB
關掉 swap
1 2 3 4 5 6
environment: - bootstrap.memory_lock=true ulimits: memlock: soft: -1 hard: -1
因為我想要掛載資料夾保存資料,而 Elasticsearch 預設是使用 elasticsearch
這個 user 跟 uid:gid 1000:0,所以需要先建立資料夾跟設定權限。不然直接啟動時,會遇到寫檔權限錯誤。
|
|
啟動服務
以上動作都準備好之後,就可以啟動服務了
|
|
稍等服務啟動,就可以直接點以下連結看服務有沒有成功
- elasticsearch: http://127.0.0.1:9200/
- kibana: http://127.0.0.1:5601/
實測 Elasticsearch
打開 Kibana 選單,選 Dev Tools,這裡提供一個 Console 介面讓我們可以方便的測試 Elasticsearch。左側輸入 API 請求,執行結果會出現在右側。
以下提供幾個測試範例:
查看 cluster 健康狀態
Request:
1
GET /_cluster/health
Response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
{ "cluster_name": "docker-cluster", "status": "yellow", "timed_out": false, "number_of_nodes": 1, "number_of_data_nodes": 1, "active_primary_shards": 38, "active_shards": 38, "relocating_shards": 0, "initializing_shards": 0, "unassigned_shards": 3, "delayed_unassigned_shards": 0, "number_of_pending_tasks": 0, "number_of_in_flight_fetch": 0, "task_max_waiting_in_queue_millis": 0, "active_shards_percent_as_number": 92.6829268292683 }
新增 document 到 hello index
Request:
1 2 3 4 5
PUT hello/_doc/1 { "message": "hello, world!", "from": "me" }
Response
1 2 3 4 5 6 7 8 9 10 11 12 13
{ "_index": "hello", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
查詢 hello id 為 1 的 document
Request
1
GET hello/_doc/1
Response
1 2 3 4 5 6 7 8 9 10 11 12
{ "_index": "hello", "_id": "1", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "message": "hello, world!", "from": "me" } }
搜尋 hello 的所有 document
Request
1 2 3 4 5 6
GET hello/_search { "query":{ "match_all" : {} } }
Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "hello", "_id": "1", "_score": 1, "_source": { "message": "hello, world!", "from": "me" } } ] } }
刪除 hello id 為 1 的 document
Request
1
DELETE hello/_doc/1
Response
1 2 3 4 5 6 7 8 9 10 11 12 13
{ "_index": "hello", "_id": "1", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
Reference
- Install Elasticsearch with Docker | Elasticsearch Guide [8.11] | Elastic
- Run Elasticsearch locally | Elasticsearch Guide [8.11] | Elastic
- Important Elasticsearch configuration | Elasticsearch Guide [8.11] | Elastic
- elasticsearch/docs/reference/setup/install/docker/docker-compose.yml at 8.11 · elastic/elasticsearch
- Disable swapping | Elasticsearch Guide [8.12] | Elastic
- Elasticsearch fails to start in Docker, when
elasticsearch.yml
is bind mount · Issue #85463 · elastic/elasticsearch