最近的項目要用到快速全文檢索,經過前期的調研,最后選用Elasticsearch搭建搜索服務器。以前做的項目中沒用過這個搜索引擎,這是第一次使用。
主要是參照 《如何用 Node.js 和 Elasticsearch 構建搜索引擎》這篇文章學習的,這篇文章翻譯得很好,整個過程都走下來了很流暢。
下面記錄一下本人按照這篇文章的學習過程:
1、學習Elasticsearch概述。
了解Elasticsearch是什么?能做什么?可以查一下elasticsearch、lucene等的相關介紹,另外也可以查查資料比較一下其它的搜索引擎sphinx、reds等。
2、學習Elasticsearch術語。
上面參考文章中只列了幾個核心術語,其它更多的術語可以自己查找資料。
3、安裝Elasticsearch並運行。
a、下載Elasticsearch: https://www.elastic.co/downloads/elasticsearch
本人下載的v5.3.0版本zip包,下載后解壓到本地mac系統的/usr/local/elasticsearch目錄下.
這個版本需要java1.8以上(包含)支持,這個可以在命令行用java -version查看,達不到要求的就自己升級去。
b、啟動Elasticsearch,出現下面類似的界面就說明啟動成功了。默認9200是http訪問端口,是9300是各節點間彼此通信的端口。
>cd /usr/local/elasticsearch >bin/elasticsearch [2017-03-31T16:32:40,477][INFO ][o.e.n.Node ] [] initializing ... [2017-03-31T16:32:40,571][INFO ][o.e.e.NodeEnvironment ] [iXZpFuL] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [57.7gb], net total_space [232.6gb], spins? [unknown], types [hfs] [2017-03-31T16:32:40,571][INFO ][o.e.e.NodeEnvironment ] [iXZpFuL] heap size [1.9gb], compressed ordinary object pointers [true] [2017-03-31T16:32:40,590][INFO ][o.e.n.Node ] node name [iXZpFuL] derived from node ID [iXZpFuL9TUa2_I43LJUQDg]; set [node.name] to override [2017-03-31T16:32:40,590][INFO ][o.e.n.Node ] version[5.3.0], pid[5772], build[3adb13b/2017-03-23T03:31:50.652Z], OS[Mac OS X/10.12/x86_64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_121/25.121-b13] [2017-03-31T16:32:41,642][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [aggs-matrix-stats] [2017-03-31T16:32:41,642][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [ingest-common] [2017-03-31T16:32:41,642][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [lang-expression] [2017-03-31T16:32:41,642][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [lang-groovy] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [lang-mustache] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [lang-painless] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [percolator] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [reindex] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [transport-netty3] [2017-03-31T16:32:41,643][INFO ][o.e.p.PluginsService ] [iXZpFuL] loaded module [transport-netty4] [2017-03-31T16:32:41,644][INFO ][o.e.p.PluginsService ] [iXZpFuL] no plugins loaded [2017-03-31T16:32:43,969][INFO ][o.e.n.Node ] initialized [2017-03-31T16:32:43,969][INFO ][o.e.n.Node ] [iXZpFuL] starting ... [2017-03-31T16:32:44,209][INFO ][o.e.t.TransportService ] [iXZpFuL] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300} [2017-03-31T16:32:47,392][INFO ][o.e.c.s.ClusterService ] [iXZpFuL] new_master {iXZpFuL}{iXZpFuL9TUa2_I43LJUQDg}{2FmpVTkEQwKUy9eHEv26qg}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined) [2017-03-31T16:32:47,412][INFO ][o.e.h.n.Netty4HttpServerTransport] [iXZpFuL] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200} [2017-03-31T16:32:47,415][INFO ][o.e.n.Node ] [iXZpFuL] started [2017-03-31T16:32:47,553][INFO ][o.e.g.GatewayService ] [iXZpFuL] recovered [1] indices into cluster_state [2017-03-31T16:32:47,806][INFO ][o.e.c.r.a.AllocationService] [iXZpFuL] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[library][2]] ...]).
c、在瀏覽器中打開http://localhost:9200/ ,將會顯示一些關於你運行的實例的基本信息。
{ "name" : "iXZpFuL", "cluster_name" : "elasticsearch", "cluster_uuid" : "inoWXI3_QsGfG8xG4YHOAA", "version" : { "number" : "5.3.0", "build_hash" : "3adb13b", "build_date" : "2017-03-23T03:31:50.652Z", "build_snapshot" : false, "lucene_version" : "6.4.1" }, "tagline" : "You Know, for Search" }
4、圖形化用戶界面。
按照文章中提供的選擇,我安裝了谷歌瀏覽器的擴展組件ElasticSearch Toolbox.界面如下。
第一個select加載可選索引,第二個select加載可選類型。因為還沒有導入任何數據,暫時沒得選。
其實另外一個插件也挺好,地址:https://github.com/mobz/elasticsearch-head 感興趣的同學可以下載下來試試。
5、基於Node的elasticsearch客戶端。
elasticsearch為Node.js提供一個官方模塊,稱為elasticsearch。可以通過這個模塊編碼與elasticsearch通訊。
我直接下載的示例代碼運行。示例要求node版本0.11.0及以上。
>git clone https://github.com/sitepoint-editors/node-elasticsearch-tutorial.git #下載示例 >cd node-elasticsearch-tutorial #切換目錄 >npm install #安裝依賴
示例中的文件解釋可以看到 https://github.com/sitepoint-editors/node-elasticsearch-tutorial 上去看。
我們目前只需要知道:
1、data.json是測試數據文件,1000條數據。
2、index.js文件的例子就是導入data.json文件中的數據到elasticsearch。使用node index.js運行。
$ node index.js 1000 items parsed from data file Successfully indexed 1000 out of 1000 items
3、indices.js用於檢查索引信息的准確性。
4、其它的elasticsearch測試文件都是檢索數據的例子。有各種寫法,以后慢慢研究就可以了。
先弄個簡單查詢的在先前安裝的ElasticSearch Toolbox上試試。
由於導入了數據。現在可以在工具中選擇索引library,類型article.然后直接點擊查詢可以看到如下結果:
上面結果是查詢所有的,我們可以點擊QUERY DEITOR編輯查詢條件,比如查詢作者名為Rowe的數據,條件如下:
{ "query": { "match": { "authors.firstname": "Rowe" } }, "from": 0, "size": 10 }
可以查到一條記錄。
關於Elasticsearch數據檢索知識下一篇文章再說。