本篇內(nèi)容介紹了“Centos7安裝ElasticSearch入門實例分析”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了思禮免費建站歡迎大家使用!
1.下載elasticsearch 6.4.1
2.解壓壓縮包
[root@localhost elasticsearch]# tar -zxvf elasticsearch-6.4.1.tar.gz
3.啟動elasticsearch
[root@localhost bin]# ./elasticsearch
以后臺方式啟動
[root@localhost bin]# ./elasticsearch -d
tips:
[root@localhost bin]# ./elasticsearch [2018-09-19t19:46:09,817][warn ][o.e.b.elasticsearchuncaughtexceptionhandler] [] uncaught exception in thread [main] org.elasticsearch.bootstrap.startupexception: java.lang.runtimeexception: can not run elasticsearch as root at org.elasticsearch.bootstrap.elasticsearch.init(elasticsearch.java:140) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.elasticsearch.execute(elasticsearch.java:127) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.cli.environmentawarecommand.execute(environmentawarecommand.java:86) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.cli.command.mainwithouterrorhandling(command.java:124) ~[elasticsearch-cli-6.4.1.jar:6.4.1] at org.elasticsearch.cli.command.main(command.java:90) ~[elasticsearch-cli-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.elasticsearch.main(elasticsearch.java:93) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.elasticsearch.main(elasticsearch.java:86) ~[elasticsearch-6.4.1.jar:6.4.1] caused by: java.lang.runtimeexception: can not run elasticsearch as root at org.elasticsearch.bootstrap.bootstrap.initializenatives(bootstrap.java:104) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.bootstrap.setup(bootstrap.java:171) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.bootstrap.init(bootstrap.java:326) ~[elasticsearch-6.4.1.jar:6.4.1] at org.elasticsearch.bootstrap.elasticsearch.init(elasticsearch.java:136) ~[elasticsearch-6.4.1.jar:6.4.1]
elasticsearch 不能以root用戶角色啟動,因此需要將安裝目錄授權(quán)給其他用戶,用其他用戶來啟動
啟動成功后,驗證,打開新的終端,執(zhí)行如下命令:
[root@localhost ~]# curl 'http://localhost:9200/?pretty' { "name" : "o5bavye", "cluster_name" : "elasticsearch", "cluster_uuid" : "rw1yjlzksgodxkuvgixmxg", "version" : { "number" : "6.4.1", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "e36acdb", "build_date" : "2018-09-13t22:18:07.696808z", "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" } [root@localhost ~]#
返回信息則表示安裝成功!
4.安裝kibana
sense 是一個 kibana 應(yīng)用 它提供交互式的控制臺,通過你的瀏覽器直接向 elasticsearch 提交請求。 這本書的在線版本包含有一個 view in sense 的鏈接,里面有許多代碼示例。當點擊的時候,它會打開一個代碼示例的sense控制臺。 你不必安裝 sense,但是它允許你在本地的 elasticsearch 集群上測試示例代碼,從而使本書更具有交互性。
下載kibana
kibana是一個為 elasticsearch 提供的數(shù)據(jù)分析的 web 接口??墒褂盟鼘θ罩具M行高效的搜索、可視化、分析等各種操作
下載完成解壓kibana
[root@localhost elasticsearch]# tar -zxvf kibana-6.4.1-linux-x86_64.tar.gz
修改 配置config目錄下的kibana.yml 文件,配置elasticsearch地址和kibana地址信息
server.host: "192.168.92.50" # kibana 服務(wù)器地址 elasticsearch.url: "http://192.168.92.50:9200" # es 地址
啟動 kibana
[root@localhost bin]# ./kibana
安裝kibana本機訪問:http://localhost:5601/
選擇dev tools菜單,即可實現(xiàn)可視化請求
5.安裝logstash
下載logstash
下載完成解壓后,config目錄下配置日志收集日志配置文件 logstash.conf
# sample logstash configuration for creating a simple # beats -> logstash -> elasticsearch pipeline. input { tcp { mode => "server" host => "192.168.92.50" port => 4560 codec => json_lines } } output { elasticsearch { hosts => "192.168.92.50:9200" index => "springboot-logstash-%{+yyyy.mm.dd}" } }
配置成功后啟動logstatsh
[root@localhost bin]# ./logstash -f ../config/logstash.conf
es 一些基礎(chǔ)知識:
索引(名詞):
如前所述,一個 索引 類似于傳統(tǒng)關(guān)系數(shù)據(jù)庫中的一個 數(shù)據(jù)庫 ,是一個存儲關(guān)系型文檔的地方。 索引 (index) 的復(fù)數(shù)詞為 indices 或 indexes 。
索引(動詞):
索引一個文檔 就是存儲一個文檔到一個 索引 (名詞)中以便它可以被檢索和查詢到。這非常類似于 sql 語句中的 insert 關(guān)鍵詞,除了文檔已存在時新文檔會替換舊文檔情況之外。
倒排索引:
關(guān)系型數(shù)據(jù)庫通過增加一個 索引 比如一個 b樹(b-tree)索引 到指定的列上,以便提升數(shù)據(jù)檢索速度。elasticsearch 和 lucene 使用了一個叫做 倒排索引 的結(jié)構(gòu)來達到相同的目的。
put /megacorp/employee/1 { "first_name" : "john", "last_name" : "smith", "age" : 25, "about" : "i love to go rock climbing", "interests": [ "sports", "music" ] }
返回結(jié)果:
#! deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template { "_index": "megacorp", "_type": "employee", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
路徑 /megacorp/employee/1 包含了三部分的信息:
megacorp 索引名稱
employee 類型名稱
1 特定雇員的id
放置第二個雇員信息:
{ "_index": "megacorp", "_type": "employee", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
返回結(jié)果:
{ "_index": "megacorp", "_type": "employee", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
放置第三個雇員信息
{ "_index": "megacorp", "_type": "employee", "_id": "3", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
5.檢索文檔
檢索到單個雇員的數(shù)據(jù)
get /megacorp/employee/1
返回結(jié)果:
{ "_index": "megacorp", "_type": "employee", "_id": "1", "_version": 1, "found": true, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } }
6.輕量搜索
一個 get 是相當簡單的,可以直接得到指定的文檔。 現(xiàn)在嘗試點兒稍微高級的功能,比如一個簡單的搜索!
第一個嘗試的幾乎是最簡單的搜索了。我們使用下列請求來搜索所有雇員:
get /megacorp/employee/_search
返回結(jié)果:
{ "took": 31, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 1, "_source": { "first_name": "jane", "last_name": "smith", "age": 32, "about": "i like to collect rock albums", "interests": [ "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 1, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "3", "_score": 1, "_source": { "first_name": "douglas", "last_name": "fir", "age": 35, "about": "i like to build cabinets", "interests": [ "forestry" ] } } ] } }
通過姓名模糊匹配來獲得結(jié)果
get /megacorp/employee/_search?q=last_name:smith
返回結(jié)果:
{ "took": 414, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.2876821, "_source": { "first_name": "jane", "last_name": "smith", "age": 32, "about": "i like to collect rock albums", "interests": [ "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.2876821, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } } ] } }
7.使用查詢表達式搜索
領(lǐng)域特定語言 (dsl), 指定了使用一個 json 請求
get /megacorp/employee/_search { "query" : { "match" : { "last_name" : "smith" } } }
返回結(jié)果:
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.2876821, "_source": { "first_name": "jane", "last_name": "smith", "age": 32, "about": "i like to collect rock albums", "interests": [ "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.2876821, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } } ] } }
8.更復(fù)雜的搜索
搜索姓氏為 smith 的雇員,但這次我們只需要年齡大于 30 的,使用過濾器 filter ,它支持高效地執(zhí)行一個結(jié)構(gòu)化查詢
get /megacorp/employee/_search { "query" : { "bool": { "must": { "match" : { "last_name" : "smith" } }, "filter": { "range" : { "age" : { "gt" : 30 } } } } } }
其中:range 過濾器 , 它能找到年齡大于 30 的文檔,其中 gt 表示_大于(_great than)
返回結(jié)果:
{ "took": 44, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.2876821, "_source": { "first_name": "jane", "last_name": "smith", "age": 32, "about": "i like to collect rock albums", "interests": [ "music" ] } } ] } }
9.全文搜索
搜索下所有喜歡攀巖(rock climbing)的雇員
get /megacorp/employee/_search { "query" : { "match" : { "about" : "rock climbing" } } }
返回結(jié)果:
{ "took": 17, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.5753642, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.5753642, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.2876821, "_source": { "first_name": "jane", "last_name": "smith", "age": 32, "about": "i like to collect rock albums", "interests": [ "music" ] } } ] } }
10.全文搜索
找出一個屬性中的獨立單詞是沒有問題的,但有時候想要精確匹配一系列單詞或者短語 。 比如, 我們想執(zhí)行這樣一個查詢,僅匹配同時包含 “rock” 和 “climbing” ,并且 二者以短語 “rock climbing” 的形式緊挨著的雇員記錄。
get /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } } }
返回結(jié)果:
{ "took": 142, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.5753642, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] } } ] } }
11.高亮搜索
許多應(yīng)用都傾向于在每個搜索結(jié)果中 高亮 部分文本片段,以便讓用戶知道為何該文檔符合查詢條件。在 elasticsearch 中檢索出高亮片段也很容易。
增加參數(shù): highlight
get /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } }
返回結(jié)果:
{ "took": 250, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.5753642, "_source": { "first_name": "john", "last_name": "smith", "age": 25, "about": "i love to go rock climbing", "interests": [ "sports", "music" ] }, "highlight": { "about": [ "i love to go <em>rock</em> <em>climbing</em>" ] } } ] } }
其中高亮模塊為highlight屬性
12.分析
elasticsearch 有一個功能叫聚合(aggregations),允許我們基于數(shù)據(jù)生成一些精細的分析結(jié)果。聚合與 sql 中的 group by 類似但更強大。
舉個例子,挖掘出雇員中最受歡迎的興趣愛好:
get /megacorp/employee/_search { "aggs": { "all_interests": { "terms": { "field": "interests" } } } }
返回結(jié)果:
{ ... "hits": { ... }, "aggregations": { "all_interests": { "buckets": [ { "key": "music", "doc_count": 2 }, { "key": "forestry", "doc_count": 1 }, { "key": "sports", "doc_count": 1 } ] } } }
“Centos7安裝ElasticSearch入門實例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
當前題目:Centos7安裝ElasticSearch入門實例分析
文章轉(zhuǎn)載:http://jinyejixie.com/article14/jdogge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、企業(yè)網(wǎng)站制作、品牌網(wǎng)站制作、網(wǎng)站營銷、做網(wǎng)站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)