(1) marvel 遠程安裝方式: bin/plugin -i elasticsearch/marvel/latesthtml
本地安裝方式: wget https://download.elasticsearch.org/elasticsearch/marvel/marvel-latest.zip bin/plugin -i marvel -u file:/home/elasticsearch-1.5.1/marvel-latest.zip 在啓動後,能夠經過如下方式查看elasticsearch運行狀況 http://xxx.xxx.xxx.xxx:8765/_plugin/marvel/java
(2) elasticsearch service [很是喜歡] https://github.com/elastic/elasticsearch-servicewrapper 將service文件放置在elasticsearch bin 目錄下 mv elasticsearch-servicewrapper-master/service/ bin/ 配置bin/service/elasticsearch.conf vim bin/service/elasticsearch.conf 按需做以下修改 set.default.ES_HOME=/home/elasticsearch-1.5.1 #替換爲實際的elasticsearch路徑 wrapper.java.command=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java #替換爲實際的java二進制文件路徑git
(3). ElasticHQ [很是喜歡] http://www.elastichq.org/ bin/plugin -i royrusso/elasticsearch-HQ -u file:/home/elasticsearch-1.5.1/royrusso-elasticsearch-HQ-603ae9e.zipgithub
在啓動後,能夠經過如下方式查看elasticsearch運行狀況 http://xxx.xxx.xxx.xxx:8765/_plugin/HQ/算法
(4) elasticsearch-head [比較喜歡] https://github.com/mobz/elasticsearch-head bin/plugin -i mobz/elasticsearch-head -u file:/home/elasticsarch-1.5.1/elasticsearch-head-master.zip數據庫
在啓動後,能夠經過如下方式查看elasticsearch運行狀況 http://xxx.xxx.xxx.xxxx:8765/_plugin/headapache
第四步:啓動elasticsearch bin/service/elasticsearch start|stop|console|install|removejson
start 在後臺運行elasticsearch stop 中止elasticsearch console 在前臺運行elasticsearch install elasticsearch自啓動 remove elasticsearch取消自啓動vim
2、基本操做app
首先咱們批量導入示例數據——莎士比亞全集 (參照http://kibana.logstash.es/content/v3/10-minute-walk-through.html kibana 3指南10分鐘入門 wget http://www.elasticsearch.org/guide/en/kibana/3.0/snippets/shakespeare.json
curl -XPUT http://localhost:8765/_bulk --data-binary @shakespeare.json more shakespeare.json 察看存儲內容 {"index":{"_index":"shakespeare","_type":"act","_id":0}} {"line_id":1,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"} {"index":{"_index":"shakespeare","_type":"scene","_id":1}}
接下來咱們來經過與熟悉的關係數據庫來對比elasticsearch的數據組成 (1)數據組成:元數據+實際數據 至關於察看數據庫的模式定義 http localhost:8765/shakespeare/ 返回 { "shakespeare": { "mappings": { "act": { "properties": { "line_id": { "type": "long" }, "line_number": { "type": "string" }, "play_name": { "type": "string" }, "speaker": { "type": "string" }, "speech_number": { "type": "long" }, "text_entry": { "type": "string" } } }, "line": { "properties": { "line_id": { "type": "long" }, "line_number": { "type": "string" }, "play_name": { "type": "string" }, "speaker": { "type": "string" }, "speech_number": { "type": "long" }, "text_entry": { "type": "string" } } }, "scene": { "properties": { "line_id": { "type": "long" }, "line_number": { "type": "string" }, "play_name": { "type": "string" }, "speaker": { "type": "string" }, "speech_number": { "type": "long" }, "text_entry": { "type": "string" } } } }, "settings": { "index": { "creation_date": "1429691321987", "number_of_replicas": "1", "number_of_shards": "5", "uuid": "rrCmsKKcSDyLSpLFVnQnbg", "version": { "created": "1040299" } } } } }
咱們用熟悉的關係數據庫來進行對比,映射關係以下 elasticsearch RDBS indices 索引 databases數據庫 types 類型 tables表 documents文檔 rows行 fields 字段 columns列
示例中,索引名爲shakespeare(等同於數據庫名爲shakespeare) 類型有3個:act, line, scene (等同於表名爲act, line, scene) 字段組成(等同於表的結構) 字段名 字段類型 line_id long line_number string play_name string speaker string speech_number long text_entry string
(2)簡單檢索 示例1:經過index+type+文檔_id來察看內容 格式:host:port/index_name/type_name/_id http localhost:8108/shakespeare/line/2
結果以下: { "_id": "2", "_index": "shakespeare", "_source": { "line_id": 3, "line_number": "", "play_name": "Henry IV", "speaker": "", "speech_number": "", "text_entry": "Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others" }, "_type": "line", "_version": 1, "found": true } elasticsearch的數據由兩部分組成:文檔元數據(例如_id)與文檔數據 名字 說明 _index 相似RDBS的「數據庫」概念 _type 相似RDBS的「表」概念 _id 文檔的惟一編號 _source 字段裏的內容爲文檔數據(真實存儲的數據),咱們可使用以下方法只讀取實際數據 http localhost:8108/shakespeare/line/2/_source 結果以下:
{ "line_id": 3, "line_number": "", "play_name": "Henry IV", "speaker": "", "speech_number": "", "text_entry": "Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others" }
示例2:指定字段field進行搜索,例如搜索play_name字段爲Romeo and Juliet http localhost:8108/shakespeare/_search?q=play_name:"Romeo and Juliet" 結果以下(截取部分): { "_shards": { "failed": 0, "successful": 5, "total": 5 }, "hits": { "hits": [ { "_id": "86748", "_index": "shakespeare", "_score": 3.3792284, "_source": { "line_id": 86749, "line_number": "", "play_name": "Romeo and Juliet", "speaker": "JULIET", "speech_number": 19, "text_entry": "Exeunt" }, "_type": "line" },
(3)複雜搜索 Elasticsearch支持豐富而靈活的查詢語言——Query DSL。 在學習以前,咱們能夠先熟悉一下Lucene查詢語法(其實和使用google搜索引擎區別不大)
支持AND,OR,NOT 查詢語句"apache AND lucene"的意思是匹配含apache且含lucene的文檔。 查詢表達式"apache OR lucene"可以匹配包含「apache」的文檔,也能匹配包含"lucene"的文檔,還能匹配同時包含這兩個Term的文檔。 查詢表達式「lucene NOT elasticsearch」就只能匹配包含lucene可是不含elasticsearch的文檔
支持+, -符號 例如:但願搜索到包含關鍵詞lucene,可是不含關鍵詞elasticsearch的文檔,能夠用以下的查詢表達式:"+lucene -elasticsearch"。
支持指定字段名進行搜索(相似RDBS按列名搜索) 例如:查詢title域中包含關鍵詞elasticsearch的文檔,查詢表達式以下:title:elasticsearch
支持通配符 ? (匹配單個字符)
支持~整數符號 一個~符號,後面緊跟一個整數,~後面的整數表示短語中可接收的最大的詞編輯距離(短語中替換一個詞,添加一個詞,刪除一個詞) "writer~2"可以搜索到含writer和writers的文檔。 title:"mastering elasticsearch"~2可以搜匹配title域中含"mastering elasticsearch"的文檔與包含"mastering book elasticsearch"的文檔
支持^符號進行加權boost設置 一個^符號後面接一個浮點數表示權重。若是權重小於1,就會下降關鍵詞的重要程度。同理,若是權重大於1就會增長關鍵詞的重要程度。默認的加權值爲1
支持區間搜索 price:[10.00 TO 15.00查詢price域的值在10.00到15.00之間的全部文檔。 price:[10.00 TO 15.00}查詢price域中價格在10.00(10.00要可以被搜索到)到15.00(15.00不能被搜索到)之間的文檔
特殊字符需轉義 +, -, &&, || , ! , (,) , { } , [ ] , ^, " , ~, *, ?, : , , /
更多,Lucene原理 (打分算法,TF-IDF算法必定會在搜索中出境)
咱們能夠看到elasticsearch支持豐富的數據查詢方式,結果展現方式(按什麼方式來排序結果,使用什麼圖形來展現統計結果) (1)關鍵詞查詢term (2)短語查詢phrase (3)區間range (4)布爾Boolean (5)模糊fuzzy (6)跨度span (7)通配符wildcard (8)地理位置spatial (9) 統計aggregation ——這個功能很是很是贊,好比說生成各類統計圖表 (10)prospective search
搜索語句支持經過URI提交(上面的例子演示的_search?q= 注意,使用這種方式的要遵循url編碼,官方參考) ,也支持經過request body提交,簡直就是HTTP RESTFULL最佳實踐,官方參考
咱們用熟悉的SQL語句來對比 實例1: curl -XPOST 'http://localhost:8108/shakespeare/line/_search?pretty' -d ' { "query":{ "match_all": {} }, "sort": {"line_id": {"order": "desc" }}, "size": 1, "from": 10 }' 等同於 use shakespeare; select * from line order by line_id desc limit 10,1 實例2: curl -XPOST 'http://localhost:8108/shakespeare/line/_search?pretty' -d ' { "query":{ "bool":{ "must":[ {"match_phrase": {"text_entry":"question"}},
{"match_phrase": {"text_entry":"not to be"}} ] } } }' 結果
"took" : 253, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 4.0433946, "hits" : [ { "_index" : "shakespeare", "_type" : "line", "_id" : "34229", "_score" : 4.0433946, "_source":{"line_id":34230,"play_name":"Hamlet","speech_number":19,"line_number":"3.1.64","speaker":"HAMLET","text_entry":"To be, or not to be: that is the question:"} }, { "_index" : "shakespeare", "_type" : "line", "_id" : "1397", "_score" : 4.0004296, "_source":{"line_id":1398,"play_name":"Henry IV","speech_number":152,"line_number":"2.4.392","speaker":"FALSTAFF","text_entry":"blackberries? a question not to be asked. Shall"} } ] } } 等同於 use shakespeare; select * from line where text_entry like "%question%" and text_entry like "%not to be%"
Search APIs Match Query APIs