Elasticsearch簡單使用和環境搭建


Elasticsearch簡單使用和環境搭建

1 Elasticsearch簡介

Elasticsearch是一個可用於構建搜索應用的成品軟件,它最早由Shay Bannon創建並於2010年2月發布。現在已經非常流行,成為商業解決方案之外一個開源的重要選擇。

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

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

2 Elasticsearch下載安裝

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

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的利器。

安裝

有兩種方式來使用head。一種方式是作為ealsticsearch的插件,另一種是將其作為一個獨立的webapp來運行。這里將其作為elasticsearch插件來使用。

在線安裝步驟:

離線安裝步驟:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM