Elasticsearch本地環境安裝和經常使用操做

本篇文章首發於個人頭條號Elasticsearch本地環境安裝和經常使用操做,歡迎關注個人頭條號和微信公衆號「大數據技術和人工智能」(微信搜索bigdata_ai_tech)獲取更多幹貨,也歡迎關注個人CSDN博客node


Elasticsearch是一個分佈式、RESTful風格的搜索和數據分析引擎。目前已被各大公司普遍的引入生產使用,也已成爲大數據生態的重要組成部分。本篇簡單介紹一下Elasticsearch的本地安裝和一些經常使用操做。shell

Elasticsearch使用Java構建,不一樣版本的Elasticsearch對Java版本要求略有差別,可參考下圖來選擇Elasticsearch和Java的版本(下圖來自官網Support Matrix JVM)。json

Elasticsearch本地安裝

本地安裝Elasticsearch很是簡單,首先到官網下載Elasticsearch到本地指定目錄,而後解壓,進入到Elasticsearch的解壓目錄下,執行./bin/elasticsearch.\bin\elasticsearch.bat(Windows),能夠加上-d參數讓Elasticsearch在後臺運行。至此,Elasticsearch就安裝好了,能夠經過curl http://localhost:9200或者用瀏覽器打開http://localhost:9200/檢查是否正常啓動,下圖這樣就表示正常啓動了。bootstrap

常見問題

Elasticsearch的安裝很是簡單,一般在安裝過程當中會遇到一些問題,下面這幾個問題是在Ubuntu操做系統安裝時常常遇到的問題。瀏覽器

問題一:

ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解決辦法:
切換到root用戶修改配置/etc/sysctl.conf
添加下面配置並執行命令:bash

vm.max_map_count=655360
sysctl -p

而後,從新啓動elasticsearch,便可啓動成功。微信

問題二:

ERROR: [1] bootstrap checks failed
[1]: max number of threads [1024] for user [elasticsearch] is too low, increase to at least [2048]

解決辦法:
修改/etc/security/limits.d/90-nproc.confapp

* soft nproc 1024 修改爲 * soft nproc 2048

Elasticsearch經常使用操做

問題三:

ERROR: [1] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]

解決辦法:
切換到root用戶,編輯/etc/security/limits.conf添加以下內容(其實切換到root用戶直接執行ulimit -n 65536便可)curl

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

新增一個索引

curl -X PUT 'http://localhost:9200/index_name' -H 'Content-Type: application/json' -d '{
    "settings": {
        ...
    },
    "mappings": {
        "one": {...},
        "two": {...},
        ...
    }
}'

刪除一個索引

curl -X DELETE "http://localhost:9200/index_name"

刪除多個索引

curl -X DELETE "http://localhost:9200/index_name1,index_name2"

刪除全部索引

curl -X DELETE "http://localhost:9200/_all"  
curl -X DELETE "http://localhost:9200/*"

添加一條數據

curl -X PUT 'http://localhost:9200/index_name/type_name/id' -H 'Content-Type: application/json' -d '{
  "title": "The title",
  "text":  "The text ...",
  "date":  "2019/01/01"
}'

刪除單條數據

curl -X DELETE "http://localhost:9200/index_name/type_name/id"

批量刪除多條數據

curl -X POST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' -d '
{"delete":{"_index":"index_name","_type":"main","_id":"1"}}
{"delete":{"_index":"index_name","_type":"main","_id":"2"}}
'

刪除全部數據

curl -X POST "http://localhost:9200/index_name/type_name/_delete_by_query?conflicts=proceed" -H 'Content-Type: application/json' -d '{"query": {"match_all": {}}}'

修改索引setting

curl -X POST 'http://localhost:9200/index_name' -H 'Content-Type: application/json' -d '{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 0,
        "index.mapping.total_fields.limit": 5000
    }
}'

索引重命名

curl -X POST 'http://localhost:9200/_reindex' -H 'Content-Type: application/json' -d '{
  "source": {
    "index": "index_name_old"
  },
  "dest": {
    "index": "index_name_new"
  }
}'

手動遷移分片

curl -X PUT 'http://localhost:9200/_cluster/settings' -H 'Content-Type: application/json' -d'{
    "transient": {
        "cluster.routing.allocation.enable": "none"
    }
}'
curl -X PUT 'http://localhost:9200/_cluster/settings' -H 'Content-Type: application/json' -d'{
    "transient": {
        "cluster.routing.allocation.enable": "all"
    }
}'
curl -X POST 'http://localhost:9200/_cluster/reroute' -H 'Content-Type: application/json' -d  '{
    "commands" : [
        {
            "move" : {
                "index" : "reindex-resharding-test",
                "shard" : 0,
                "from_node" : "192.168.0.101",
                "to_node" : "192.168.0.102"
            }
        }
    ]
}'

查看集羣狀態

curl -X GET 'http://localhost:9200/_cluster/health?pretty'

查看全部索引

curl -X GET 'http://localhost:9200/_cat/indices?v'

查看全部shards

curl -X GET 'http://localhost:9200/_cat/shards'

查看unassigned shards

curl -X GET 'http://localhost:9200/_cat/shards' | grep UNASSIGNED
相關文章
相關標籤/搜索