Elasticsearch簡單使用和環境搭建

Elasticsearch簡單使用和環境搭建

1 Elasticsearch簡介

Elasticsearch是一個可用於構建搜索應用的成品軟件,它最先由Shay Bannon建立並於2010年2月發佈。如今已經很是流行,成爲商業解決方案以外一個開源的重要選擇。前端

Elasticsearch是一個基於Lucene的搜索服務器,提供一個分佈式多用戶能力的全文搜索引擎,基於RESTful web藉口,使用Java開發,在Apache許可條款下開發源代碼發佈。作到準實時搜索、穩定、可靠、安裝使用方便。git

Elasticsearch具備合理的默認配置,默認就是分佈式工做模式,使用對等架構(P2P)避免單點故障(SPOF),易於橫向擴展新的節點。此外因爲沒有對索引中的數據結構強行添加限制,從而容許用戶調整現有數據模型。github

2 Elasticsearch下載安裝

官網下載頁面中有ZIP、TAR、DEB、RPM幾種格式的安裝包提供下載。web

step1 下載

下載並解壓Elasticsearch的最新版本瀏覽器

step2 運行

在Uinx上運行命令: bin/elasticsearch,在Windows環境下運行命令:bin\elasticsearch.bat。服務器

step3 訪問

運行命令: curl -X GET http://localhost:9200/數據結構

結果:
架構

3 插件的安裝和使用

3.1 head插件

elasticsearch-head是elastic search集羣的一個web前端。源代碼託管在github.com,地址是:https://github.com/mobz/elasticsearch-head。這是一個學習elasticsearch的利器。app

安裝

有兩種方式來使用head。一種方式是做爲ealsticsearch的插件,另外一種是將其做爲一個獨立的webapp來運行。這裏將其做爲elasticsearch插件來使用。curl

在線安裝步驟:

離線安裝步驟:

3.2 IK分詞插件

這裏使用的是elasticsearch-analysis-ik,這是一個將Lucence IK分詞器集成到elasticsearch的ik分詞器插件,而且支持自定義的詞典。源代碼託管在github.com上,地址是:https://github.com/medcl/elasticsearch-analysis-ik

安裝

  • 1,在github上下載同elasticsearch匹配的版本
  • 2,將下載zip包,在解壓到elasticsearch/plugin/ik下
  • 3,重啓elasticsearch

3.2.1測試IK分詞

IK分詞安裝後有三種分詞策略:ik、ik_max_word和ik_smart。ik和ik_max_word分詞效果相同,對輸入文本根據詞典窮盡各類分割方法是細力度分割策略。ik_smart會對輸入文本根據詞典以及歧義判斷等方式進行一次最合理的粗粒度分割。能夠在Terml中使用curl查看分詞效果。

  • 分詞策略ik/ik_max_word
$ curl -G 'localhost:9200/_analyze?analyzer=ik&pretty=true' --data-urlencode "text=中華人民共和國國歌"
{
  "tokens" : [ {
    "token" : "中華人民共和國",
    "start_offset" : 0,
    "end_offset" : 7,
    "type" : "CN_WORD",
    "position" : 0
  }, {
    "token" : "中華人民",
    "start_offset" : 0,
    "end_offset" : 4,
    "type" : "CN_WORD",
    "position" : 1
  }, {
    "token" : "中華",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "CN_WORD",
    "position" : 2
  }, {
    "token" : "華人",
    "start_offset" : 1,
    "end_offset" : 3,
    "type" : "CN_WORD",
    "position" : 3
  }, {
    "token" : "人民共和國",
    "start_offset" : 2,
    "end_offset" : 7,
    "type" : "CN_WORD",
    "position" : 4
  }, {
    "token" : "人民",
    "start_offset" : 2,
    "end_offset" : 4,
    "type" : "CN_WORD",
    "position" : 5
  }, {
    "token" : "共和國",
    "start_offset" : 4,
    "end_offset" : 7,
    "type" : "CN_WORD",
    "position" : 6
  }, {
    "token" : "共和",
    "start_offset" : 4,
    "end_offset" : 6,
    "type" : "CN_WORD",
    "position" : 7
  }, {
    "token" : "國",
    "start_offset" : 6,
    "end_offset" : 7,
    "type" : "CN_CHAR",
    "position" : 8
  }, {
    "token" : "國歌",
    "start_offset" : 7,
    "end_offset" : 9,
    "type" : "CN_WORD",
    "position" : 9
  } ]
}
  • 分詞策略ik_smart
$ curl -G 'localhost:9200/_analyze?analyzer=ik_smart&pretty=true' --data-urlencode "text=中華人民共和國國歌"
{
  "tokens" : [ {
    "token" : "中華人民共和國",
    "start_offset" : 0,
    "end_offset" : 7,
    "type" : "CN_WORD",
    "position" : 0
  }, {
    "token" : "國歌",
    "start_offset" : 7,
    "end_offset" : 9,
    "type" : "CN_WORD",
    "position" : 1
  } ]
}

這裏使用的版本分別是:

soft version
elasticsearch 2.4.0
elasticsearch-head master分支的當前版本
elasticsearch-analysis-ik 1.10.0
相關文章
相關標籤/搜索