elasticsearch2.3.1單機和叢集安裝2合1及elasticsearch的使用
特點: 1.倒排索引(也就是分詞後做索引) 2.安裝過程非常簡單,解壓縮就能用 3.沒有明確主節點,搭建叢集增加節點即可(叢集最好3個或3個以上節點) 4.查詢非常快速 安裝過程如下: 說明: 1建議不要使用註冊使用者root,如果用root會啟動不起來(除非進行配置) 2如果是搭建叢集先同步時間 date -s "2018-09-30 13:57:00"同時傳送到所有節點上 安裝步驟 安裝jdk1.7.0_55版本以上 下載elasticsearch2.3.1 (https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.1/elasticsearch-2.3.1.tar.gz
useradd bigdata
為使用者新增密碼
echo 123456 | passwd --stdin bigdata
將bigdata新增到sudoers
echo "bigdata ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/bigdata
chmod 0440 /etc/sudoers.d/bigdata
#解決sudo: sorry, you must have a tty to run sudo問題,在/etc/sudoer註釋掉 Default requiretty 一行
sudo sed -i 's/Defaults requiretty/Defaults:bigdata !requiretty/' /etc/sudoers
#建立一個bigdata目錄
mkdir /{bigdata,data}
#給相應的目錄新增許可權
chown -R bigdata:bigdata /{bigdata,data}
切換到bigdata進行安裝
su bigdata
解壓縮(叢集需要各個節點都解壓縮)
tar zxf elasticsearch-2.3.1.tar.gz -C /bigdata/
建立日誌資料夾
mkdir /bigdata/elasticsearch-2.3.1/logs
修改配置檔案
vi /bigdata/elasticsearch-2.3.1/config/elasticsearch.yml
修改cluster.name:bigdata(說明cluster.name相同的會建立組播,形成叢集) 修改node.name:node-1(說明如果是叢集部署node.name必須不能相同) node.rack(說明這個是設定機架,可以不用設定) path.data:/data(設定資料儲存路徑) path.logs:/bigdata/elasticsearch-2.3.1/logs(日誌儲存路徑) network.host:192.168.1.103(這臺機器的ip地址,如果修改了hosts寫主機名也可以,不建議寫localhost) http.port:9200(這個不需要修改,使用預設即可) discovery.zen.ping.unicast.hosts:“192.168.1.103” discovery.zen.minimum_master_nodes: 1 (使用1個master節點,幾個master設定幾個) 叢集狀態下設定完成後需要將/bigdata/elasticsearch-2.3.1檔案傳送到其他裝置上,後修改node.name:node-1為其他 啟動elasticsearch
/bigdata/elasticsearch-2.3.1/bin/elasticsearch -d
{
"name" : "node-1",
"cluster_name" : "bigdata",
"version" : {
"number" : "2.3.1",
"build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
"build_timestamp" : "2016-04-04T12:25:05Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
-----------------------------elasticsearch的使用----------------------------------- RESTful介面URL的格式: http://localhost:9200///[] 其中index、type是必須提供的。 id是可選的,不提供es會自動生成。 index、type將資訊進行分層,利於管理。 index可以理解為資料庫;type理解為資料表;id相當於資料庫表中記錄的主鍵,是唯一的。
#向store索引中新增一些書籍(新增用put,查詢get,刪除delete,修改update),下面的命令在centos上面輸入就行 curl -XPUT ‘http://192.168.1.103:9200/store/books/1’ -d ‘{ “title”: “Elasticsearch: The Definitive Guide”, “name” : { “first” : “Zachary”, “last” : “Tong” }, “publish_date”:“2015-02-06”, “price”:“49.99” }’ #通過瀏覽器查詢 http://192.168.1.103:9200/store/books/1 #在linux中通過curl的方式查詢
curl -XGET 'http://192.168.1.103:9200/store/books/1'
#es安裝外掛下載es外掛 /bigdata/elasticsearch-2.3.1/bin/plugin install mobz/elasticsearch-head
[[email protected] es]$ bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...........DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /export/servers/es/plugins/head
#本地方式安裝head外掛 ./plugin install file:///home/bigdata/elasticsearch-head-master.zip
或者通過 _update API的方式單獨更新你想要更新的
最簡單filter查詢
SELECT * FROM books WHERE price = 35.99
filtered 查詢價格是35.99的
curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{ “query” : { “filtered” : { “query” : { “match_all” : {} }, “filter” : { “term” : { “price” : 35.99 } } } } }’
#指定多個值 curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{ “query” : { “filtered” : { “filter” : { “terms” : { “price” : [35.99, 99.99] } } } } }’
SELECT * FROM books WHERE publish_date = “2015-02-06”
curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{ “query” : { “filtered” : { “filter” : { “term” : { “publish_date” : “2015-02-06” } } } } }’
bool過濾查詢,可以做組合過濾查詢
SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND (publish_date != “2016-02-06”)
類似的,Elasticsearch也有 and, or, not這樣的組合條件的查詢方式
格式如下:
{
“bool” : {
“must” : [],
“should” : [],
“must_not” : [],
}
}
must: 條件必須滿足,相當於 and
should: 條件可以滿足也可以不滿足,相當於 or
must_not: 條件不需要滿足,相當於 not
curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{ “query” : { “filtered” : { “filter” : { “bool” : { “should” : [ { “term” : {“price” : 35.99}}, { “term” : {“price” : 99.99}} ], “must_not” : { “term” : {“publish_date” : “2016-02-06”} } } } } } }’
巢狀查詢
SELECT * FROM books WHERE price = 35.99 OR ( publish_date = “2016-02-06” AND price = 99.99 )
curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{ “query” : { “filtered” : { “filter” : { “bool” : { “should” : [ { “term” : {“price” : 35.99}}, { “bool” : { “must” : [ {“term” : {“publish_date” : “2016-02-06”}}, {“term” : {“price” : 99.99}} ] }} ] } } } } }’
range範圍過濾
SELECT * FROM books WHERE price >= 20 AND price < 100
gt : > 大於
lt : < 小於
gte : >= 大於等於
lte : <= 小於等於
curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{ “query” : { “filtered” : { “filter” : { “range” : { “price” : { “gt” : 20.0, “lt” : 100 } } } } } }’
另外一種 and, or, not查詢
沒有bool, 直接使用and , or , not
注意: 不帶bool的這種查詢不能利用快取
查詢價格既是35.99,publish_date又為"2015-02-06"的結果
curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{ “query”: { “filtered”: { “filter”: { “and”: [ { “term”: { “price”:59.99 } }, { “term”: { “publish_date”:“2015-02-06” } } ] }, “query”: { “match_all”: {} } } } }’
#es安裝外掛下載es外掛 /bigdata/elasticsearch-2.3.1/bin/plugin install elasticsearch/marvel/latest #訪問head管理頁面 http://172.16.0.14:9200/_plugin/marvel