Elasticsearch 是一個(gè)高度可伸縮的開源全文搜索和分析引擎潭辈。它允許您快速昌犹、近乎實(shí)時(shí)地存儲(chǔ)、搜索和分析大量數(shù)據(jù)破镰。它通常用作底層引擎/技術(shù),為具有復(fù)雜搜索特性和需求的應(yīng)用程序提供支持压储。
我目前的理解是鲜漩,在大數(shù)據(jù)量的情況下,關(guān)系型數(shù)據(jù)庫的查詢搜索效率會(huì)達(dá)不到系統(tǒng)的要求集惋,無法做到有效的實(shí)時(shí)查詢和數(shù)據(jù)分析孕似,所以,就需要引入 Elasticsearch 來分擔(dān)這份工作刮刑。
把需要進(jìn)行分析的數(shù)據(jù)存儲(chǔ)到 Elasticsearch 中喉祭,并通過它的各種查詢和聚合更有效率的響應(yīng)和分析數(shù)據(jù)。
如果是安裝 Elasticsearch 最新版本雷绢,看 Installing Elasticsearch 這里就很容易安裝了泛烙,或者是常用的 brew 安裝 。
但是我們目前需要安裝 6.4.3 可能就稍微麻煩一點(diǎn)了翘紊,brew 中沒有這個(gè)版本了蔽氨。
0. 安裝 Java JDK
Elasticsearch 依賴于 jdk,所以必須先安裝好相應(yīng)的 jdk 版本,我們下一步才會(huì)比較順利安裝 Elasticsearch 相關(guān)的鹉究。見說明:Set up Elasticsearch宇立。
因?yàn)榻酉聛硎前惭b Elasticsearch 6.4.3,所以需要先安裝 6.4.3 依賴的 jdk 8自赔,各版本依賴可查看 Elasticsearch and JVM妈嘹。
沒有安裝相應(yīng) jdk 版本,執(zhí)行 sudo dpkg -i elasticsearch-6.4.3.deb
可能會(huì)報(bào)錯(cuò) (Reading database ... 74361 files and directories currently installed.) Preparing to unpack elasticsearch-6.4.3.deb ... dpkg: error processing archive elasticsearch-6.4.3.deb (--install): subprocess new pre-installation script returned error exit status 1 Errors were encountered while processing: elasticsearch-6.4.3.deb
參考:How To Manually Install Oracle Java on a Debian or Ubuntu VPS 這篇文章基本上可以在 ubuntu 上安裝好 jdk 了绍妨。
遇到的不太順暢的地方润脸,就是 Oracle 下載 jdk 需要先登錄并同意后才可以下載,直接通過 wget 下載鏈接的方式不能正確下載到他去,所以需要注意一下毙驯,文章中有一段提示可解決。
Oracle does not allow downloads without accepting their license, therefore we needed to modify the header of our request. Alternatively, you can just download the compressed file using your browser and manually upload it using a SFTP/FTP client.
我的解決方式是先在本地瀏覽器登錄 Oracle 官網(wǎng)孤页,去 下載頁面 選擇并下載要安裝的 jdk 版本尔苦,過程中可以在瀏覽器下載列表中找到帶有登錄參數(shù)的下載鏈接涩馆,再通過 wget 下載到遠(yuǎn)端機(jī)器上行施,如下圖,
wget https://download.oracle.com/otn/java/jdk/8u202-b08/1961070e4c9b4e26a04e7f5a083f551e/jdk-8u202-linux-x64.tar.gz?AuthParam=1593172842_b1fa1511e98ac208d05c3ea2b78d4d9f
接下來跟著參考文章繼續(xù)走(如果不是 root 用戶魂那,需要使用 sudo 執(zhí)行命令)蛾号,解壓 jdk 并安裝即可。
1. 安裝 Elasticsearch
在這一頁可以找到各個(gè)版本的安裝文檔:https://www.elastic.co/guide/en/elastic-stack-get-started/index.html
下面是我的安裝方式涯雅,
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.deb
sudo dpkg -i elasticsearch-6.4.3.deb
sudo /etc/init.d/elasticsearch start
執(zhí)行 curl http://127.0.0.1:9200
鲜结,查看是否運(yùn)行成功。
$ curl http://127.0.0.1:9200
{
"name" : "xxxx",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "xxxxx",
"version" : {
"number" : "6.4.3",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "fe40335",
"build_date" : "2018-10-30T23:17:19.084789Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
2. 安裝 kibana
kibana 是用來可視化 Elasticsearch 的活逆,通過它可以查看存儲(chǔ)的 index 和相關(guān)數(shù)據(jù)精刷。相當(dāng)于數(shù)據(jù)庫客戶端。版本要和 Elasticsearch 一致蔗候。
參考:Kibana 6.4.3
wget [https://artifacts.elastic.co/downloads/kibana/kibana-6.4.3-amd64.deb](https://artifacts.elastic.co/downloads/kibana/kibana-6.4.3-amd64.deb)
sudo dpkg -i kibana-6.4.3-amd64.deb
sudo -i service kibana start
執(zhí)行 curl http://127.0.0.1:5601
怒允,查看是否成功。
$ curl http://127.0.0.1:5601
<script>var hashRoute = '/app/kibana';
var defaultRoute = '/app/kibana';
var hash = window.location.hash;
if (hash.length) {
window.location = hashRoute + hash;
} else {
window.location = defaultRoute;
}</script>
3. 創(chuàng)建 Elasticsearch template
索引模板: 就是把已經(jīng)創(chuàng)建好的某個(gè)索引的參數(shù)設(shè)置(settings)和索引映射(mapping)保存下來作為模板, 在創(chuàng)建新索引時(shí), 指定要使用的模板名, 就可以直接重用已經(jīng)定義好的模板中的設(shè)置和映射.
索引模板锈遥,簡而言之纫事,是一種復(fù)用機(jī)制,就像一些項(xiàng)目的開發(fā)框架如 Laravel 一樣所灸,省去了大量的重復(fù)丽惶,體力勞動(dòng)。當(dāng)新建一個(gè) Elasticsearch 索引時(shí)爬立,自動(dòng)匹配模板钾唬,完成索引的基礎(chǔ)部分搭建。
我們用了 gem 'elasticsearch-ruby'
gem 'act-fluent-logger-rails'
,大致是
r = c.indices.put_template name: 'rails.order', body: {
index_patterns: ["rails.order-*"],
settings: {
index: {
number_of_shards: "1",
auto_expand_replicas: "0-1"
}
},
mappings: {
fluentd: {
dynamic: false,
properties: {
"@timestamp" => { type: "date" },
order_id: { type: "keyword" },
name: {
type: "keyword",
fields: { text: { type: "text" } }
},
user_id: { type: "keyword" },
user_nickname: { type: "keyword" },
user_gender: { type: "keyword" },
user_created_at: { type: "date" }
amount: { type: "scaled_float", scaling_factor: 100 }
}
}
}
}
4. 創(chuàng)建 Elasticsearch index
這個(gè)就相當(dāng)于導(dǎo)入數(shù)據(jù)到 Elasticsearch 中了知纷。
在 Elasticsearch 中每一條數(shù)據(jù)就是一條 index壤圃。
log = Fluent::Logger::FluentLogger.new
Order.all.find_each(batch_size: 1000) do |o|
log.post("rails.order", o.to_es_doc)
end
如何卸載
參考How to Uninstall Elasticsearch on Linux 和 How to uninstall Elasticsearch and Kibana
執(zhí)行:
sudo apt-get --purge autoremove elasticsearch
sudo rm -rf /opt/elasticsearch # (之前的安裝目錄)
sudo apt-get remove --purge kibana
總結(jié)
以上都完成后就準(zhǔn)備好 Elasticsearch 數(shù)據(jù)方面的準(zhǔn)備了。Elasticsearch 查詢部分就要接下來看文檔怎么寫了琅轧。
因?yàn)橛昧?gem elasticsearch-ruby 的關(guān)系伍绳,除了需要看一遍 es 的查詢語法,又需要把 es 的語法再轉(zhuǎn)換為該 gem 的語法乍桂,也遇到些阻礙冲杀,后面把 elasticsearch-ruby 源碼下載下來,再配合 google睹酌、es 官方文檔权谁,總算寫出來了 es 的查詢,最后查到了數(shù)據(jù)并顯示在頁面上憋沿。