ubuntu 16 安裝Elasticsearch

1. 下載

cd /opt
sudo wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.5/elasticsearch-2.3.5.tar.gz

2.解壓

sudo tar zxvf elasticsearch-2.3.5.tar.gz

3.啟動

cd elasticsearch
bin/elasticsearch -d
修改配置:
# ---------------------------------- Cluster --------------------
#
# Use a descriptive name for your cluster:
#
 cluster.name: my-application
#
# ------------------------------------ Node -------------------
#
# Use a descriptive name for the node:
#
 node.name: node-1

4.訪問

curl -i 'http://localhost:9200/?pretty'

{
    "name" : "Jekyll",
    "cluster_name" : "elasticsearch",
    "version" : {
        "number" : "2.3.5",
        "build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
        "build_timestamp" : "2016-07-27T10:36:52Z",
        "build_snapshot" : false,
        "lucene_version" : "5.5.0"
    },
    "tagline" : "You Know, for Search"
}

5. 安裝head插件

bin/plugin install mobz/elasticsearch-head
http://localhost:9200/_plugin/head/
Paste_Image.png

6. 安裝并啟動kibana插件

sudo wget  https://download.elastic.co/kibana/kibana/kibana-4.5.4-linux-x64.tar.gz
sudo tar zxvf kibana-4.5.4-linux-x64.tar.gz
cd kibana-4.5.4-linux-x64
bin/kibana &

7. 安裝監控marvel

sudo wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.3.5/license-2.3.5.zip
sudo wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/marvel-agent/2.3.5/marvel-agent-2.3.5.zip
sudo wget https://download.elasticsearch.org/elasticsearch/marvel/marvel-2.3.5.tar.gz

bin/plugin install file:///opt/license-2.3.5.zip
bin/plugin install file:///opt/marvel-agent-2.3.5.zip
bin/kibana plugin --install marvel --url file:///opt/marvel-2.3.5.tar.gz

curl http://localhost:5601/
Paste_Image.png
Paste_Image.png
官網插件安裝地址
https://www.elastic.co/guide/en/marvel/2.3/installing-marvel.html     
Screenshot.png

8.健康檢查

curl 'localhost:9200/_cat/health?v'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 
1471493855 12:17:35  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0% 

9.查詢節點

curl 'localhost:9200/_cat/nodes?v'
host      ip        heap.percent ram.percent load node.role master name   
127.0.0.1 127.0.0.1            3          87 0.35 d         *      Jekyll 

10. list all indices

curl 'localhost:9200/_cat/indices?v'
health status index   pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   .kibana   1   1          2            0     19.1kb         19.1kb

11. shared

curl 'localhost:9200/_cat/shards?v'
index   shard prirep state      docs  store ip        node   
.kibana 0     p      STARTED       2 19.1kb 127.0.0.1 Jekyll 
.kibana 0     r      UNASSIGNED 

12.按照中文分詞插件

./bin/plugin install medcl/elasticsearch-rtf  

二、使用(請參考README文檔)

1.角色關系對照elasticsearch 跟 MySQL 中定義資料格式的角色關系對照表如下
  MySQL                 elasticsearch     
  database              index                  
  table                 type                    
  table schema          mapping
  row                   document
  field                 field

2.創建索引,相當于數據庫wechat
curl -XPUT 'http://localhost:9200/wechat/user/info'   -d '{ "name" : "wechatUserInformation" }'
返回:{"_index":"wechat","_type":"user","_id":"info","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
說明:索引庫: wechat  
         表: user 
     主鍵id: id
   默認版本: 1  
       字段: name 
     字段值: wechatUserInformation

3.創建表和提交數據
curl -XPUT   'http://localhost:9200/wechat/user/1' -d '{ "subscribe" : 1, "openid" : "oH-JmuL9-lcG9NmufCJ3pHoQtIGE", "nickname" : "濤濤江水", "language" : "zh_CN", "city" : "沈陽", "province" : "遼寧", "country" : "中國", "headimgurl" : "http://wx.qlogo.cn/mmopen/nh5KLO4GkfafDtujhPTVgedAJGgDbNiafQKUian6Xibk7pHAJL5ff05fOjc5EydreJbvcTcgTkaZia4jucgGibtsysMMTFALr5XJU/0", "subscribe_time" : 1468924074, "remark" : "", "groupid" : 0, "tagid_list" : [ ], "platId" : "FH1KH6EXUD", "gender" : 1, "seqId" : 5, "languageId" : 50, "countryId" : 2, "provinceId" : 11, "cityId" : 12, "realtime_countryId" : 2, "realtime_provinceId" : 1108, "realtime_cityId" : 1020, "real_time_country" : "中國", "real_time_province" : "遼寧省", "real_time_city" : "沈陽市" }'

返回:{"_index":"wechat","_type":"user","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}

4 . 查看

curl -XGET   'http://localhost:9200/wechat/user/info?pretty=true'
返回:
{ "_index" : "twitter",  "_type" : "user",  "_id" : "kimchy",  "_version" : 1,  "found" : true,  "_source":{ "name" : "wechatUserInformation" }}

curl -XGET 'http://localhost:9200/wechat/_search?pretty=true' -d '{"query" : {"match" : {"city" : "沈陽" }}}'

5.支持中文全文檢索

  參考:https://github.com/medcl/elasticsearch-analysis-ik
 在elasticsearch.yml 文件末尾增加
  index.analysis.analyzer.ik.type: ik

6.測試中文搜索
  
  curl -XPUT http://localhost:9200/userindex

  7 . create a mapping
curl -XPOST http://localhost:9200/userindex/fulltext/_mapping -d'{ "fulltext": { "_all": { "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "term_vector": "no", "store": "false" }, "properties": { "content": { "type": "string", "store": "no", "term_vector": "with_positions_offsets", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "include_in_all": "true", "boost": 8 } } }}'

 8..index some docs

curl -XPOST http://localhost:9200/userindex/fulltext/1 -d'{"content":"美國留給伊拉克的是個爛攤子嗎"}'

curl -XPOST http://localhost:9200/userindex/fulltext/2 -d'{"content":"公安部:各地校車將享最高路權"}'

curl -XPOST http://localhost:9200/userindex/fulltext/3 -d'{"content":"中韓漁警沖突調查:韓警平均每天扣1艘中國漁船"}'

curl -XPOST http://localhost:9200/userindex/fulltext/4 -d'{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}'

9 .query with highlighting

curl -XPOST http://localhost:9200/userindex/fulltext/_search -d'{ "query" : { "term" : { "content" : "中國" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } }}'

https://github.com/medcl/elasticsearch-analysis-ik

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容