ElasticSearch的安裝、使用、踩坑


最近博客寫的少了。

本篇介紹在安裝ElasticSearch和head插件的過程中遇到的小問題,和一些日常使用的操作(簡單搜索語法、分片管理)。

ElasticSearch

它是一個實時分布式搜索和分析引擎。它讓你以前!所!未!有!的速度處理大數據成為可能。廢話略過,詳情這里

安裝

因為elasticsearch是Java寫的,所以先要保證Java運行環境。

1.先來安裝ElasticSearch

下載:

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.*.*.zip
unzip elasticsearch-5.*.*.zip
或者
https://www.elastic.co/downloads/elasticsearch,直接下載並解壓

運行elasticsearch,(這里下載的是5.0.1版本,5.*以后的版本對head插件的支持有所改變,后面說明):

➜  elasticsearch-5.0.1 ./bin/elasticsearch

相關執行命令:

➜  elasticsearch-5.0.1 ./bin/elasticsearch --help
starts elasticsearch

Option                Description
------                -----------
-E <KeyValuePair>     Configure a setting
-V, --version         Prints elasticsearch version information and exits
-d, --daemonize       Starts Elasticsearch in the background
-h, --help            show help
-p, --pidfile <Path>  Creates a pid file in the specified path on start
-q, --quiet           Turns off standard ouput/error streams logging in console
-s, --silent          show minimal output
-v, --verbose         show verbose output

* 注意,elasticsearch沒有關閉服務命令,只能通過kill -9。

修改相關配置文件(配置文件詳情):

➜  elasticsearch-5.0.1 vim config/elasticsearch.yml
cluster.name: zhengbin-application
http.port: 9200
#以上兩項可改可不改

現在可以瀏覽器或者curl方式訪問http://127.0.0.1:9200這個地址:

You Know, for Search~

2.再來安裝ElasticSearch-head

網上很多博客的安裝方式都是:

./bin/plugin install mobz/elasticsearch-head

但是5.*版本以上就不再支持這種方式,詳情可以看elasticsearch-head官方README

下面按照這個README來:

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
grunt server
open http://localhost:9100/

由於我用的是OSX,npm、grunt本身沒有,下面安裝這兩個命令:

➜  elasticsearch-head git:(master) brew install node
➜  elasticsearch-head git:(master) npm install
➜  elasticsearch-head git:(master) npm install grunt
➜  elasticsearch-head git:(master) npm install -g grunt-cli
#驗證一下:
➜  elasticsearch-head git:(master) grunt --version
grunt-cli v1.2.0
grunt v1.0.1

好了,准備就緒,在執行grunt server之前,需要修改elasticsearch.yml文件:

#增加對head插件的支持
http.cors.enabled: true
http.cors.allow-origin: "*"

打開瀏覽器,或直接命令:

➜  elasticsearch-head git:(master) grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
➜  elasticsearch-5.0.1 open http://127.0.0.1:9100

ES的日常使用

這里寫的很清楚,不再啰嗦。

ES分片的管理

啥是分片?這里

但是當我在線上環境創建索引后,發現分片並沒有被自動分配到主、從節點上。

就像下圖所示,索引test1有5個主分片,每個主分片對應一個副本分片,現在主分片都在創建索引的時候自動分配到了TJ3GeVV這個節點,但是還有5個副本分片沒有利用,也就顯示Unassigned狀態。

如何在創建索引后動態分配分片:

http://10.4.245.71:9200/_cluster/reroute/
{
  "commands": [
    {
      "allocate": {
        "index": "wm_godview_disorderstatus",
        "shard": 1,
        "node": "waimai-dev233",
        "allow_primary": true
      }
    }
  ]
}

通過上面的方式可以分配未分配的分片。

reroute還有移動-‘move’,取消-‘cancel’參數可選:

"commands" : [ {
        "move" : 
            {
              "index" : "test", "shard" : 0, 
              "from_node" : "node1", "to_node" : "node2"
            }
        },
       "cancel" : 
            {
              "index" : "test", "shard" : 0, "node" : "node1"
            }
        },
        {
          "allocate" : {
              "index" : "test", "shard" : 1, "node" : "node3"
          }
        }
    ]

ES新建模板

  通過head插件:

 

相關鏈接

手動控制分片分布


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM