1. 程式人生 > >ElasticSearch學習筆記(二)IK分詞器和拼音分詞器的安裝

ElasticSearch學習筆記(二)IK分詞器和拼音分詞器的安裝

ElasticSearch是自帶分詞器的,但是自帶的分詞器一般就只能對英文分詞,對英文的分詞只要識別空格就好了,還是很好做的(ES的這個分詞器和Lucene的分詞器很想,是不是直接使用Lucene的就不知道),自帶的分詞器對於中文就只能分成一個字一個字,這個顯然是不能滿足在開發中的要求的。

先看看自帶的分詞器的分詞效果(還是使用Sense工具):

POST /_analyze
{
  "analyzer":"standard",
  "text":"中華人民共和國國歌"
}

得到的結果是下面這個:

{
   "tokens": [
      {
         "token": "中"
, "start_offset": 0, "end_offset": 1, "type": "<IDEOGRAPHIC>", "position": 0 }, { "token": "華", "start_offset": 1, "end_offset": 2, "type": "<IDEOGRAPHIC>", "position": 1 }, { "token
": "人", "start_offset": 2, "end_offset": 3, "type": "<IDEOGRAPHIC>", "position": 2 }, { "token": "民", "start_offset": 3, "end_offset": 4, "type": "<IDEOGRAPHIC>", "position": 3 }, { "token
": "共", "start_offset": 4, "end_offset": 5, "type": "<IDEOGRAPHIC>", "position": 4 }, { "token": "和", "start_offset": 5, "end_offset": 6, "type": "<IDEOGRAPHIC>", "position": 5 }, { "token": "國", "start_offset": 6, "end_offset": 7, "type": "<IDEOGRAPHIC>", "position": 6 }, { "token": "國", "start_offset": 7, "end_offset": 8, "type": "<IDEOGRAPHIC>", "position": 7 }, { "token": "歌", "start_offset": 8, "end_offset": 9, "type": "<IDEOGRAPHIC>", "position": 8 } ]
}

IK分詞器的安裝

在ES中,從資料來看好像都是使用IK分詞器的,所以這篇部落格配的也是IK分詞器,有興趣的同學可以自己去試試其他的分詞器(我是用過的有MMSeg4j ,NPL,jieba)。

如果是在window下:

從這個地址上把專案clone下來或者直接下載zip下來

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

拉下來的專案是maven專案的,需要電腦上已經安裝了maven。主意下載下來的elasticsearch的版本要和你正在用的同一個版本,或者在pom.xml修改成對應的版本號:

<properties>
        <!--Elasticsearch的版本號-->
        <elasticsearch.version>5.5.0</elasticsearch.version>
        <!--JDK的版本-->
        <maven.compiler.target>1.8</maven.compiler.target>
        <elasticsearch.assembly.descriptor>${project.basedir}/src/main/assemblies/plugin.xml</elasticsearch.assembly.descriptor>
        <elasticsearch.plugin.name>analysis-ik</elasticsearch.plugin.name>
        <elasticsearch.plugin.classname>org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin</elasticsearch.plugin.classname>
        <elasticsearch.plugin.jvm>true</elasticsearch.plugin.jvm>
        <tests.rest.load_packaged>false</tests.rest.load_packaged>
        <skip.unit.tests>true</skip.unit.tests>
        <gpg.keyname>4E899B30</gpg.keyname>
        <gpg.useagent>true</gpg.useagent> 
    </properties>

然後使用下面的命令打包一下專案:

mvn package

如果成功的話會在target/releases下有一個zip壓縮包elasticsearch-analysis-ik-5.5.0.zip
這個壓縮包就是我們待會要用的。

注意到上面的pom.xml檔案中使用的JDK1.8,如果電腦是之前安裝的maven,而且恰好使用的JDK1.7的maven,mvn package是會報錯的,這個使用需要找到maven下的setting檔案,然後將JDK版本改成1.8之後,重新mvn package應該就可以了。

      <profile>       
           <id>jdk-1.8</id>       
           <activation>       
               <activeByDefault>true</activeByDefault>       
               <jdk>1.8</jdk>       
           </activation>       
           <properties>       
               <maven.compiler.source>1.8</maven.compiler.source>       
               <maven.compiler.target>1.8</maven.compiler.target>       
               <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>       
           </properties>       
   </profile> 

再注意,這個不能像之前修改像之前修改elasticsearch版本號一樣,直接在pom.xml檔案中將jdk改成1.7,因為專案中使用到了jdk1.8的新特性,這樣修改之後專案本身會報錯的。

如果還是無法使用mvn package打包專案的話,可以把專案拉進eclipse中,然後右鍵run–>maven install,可以達到同樣的效果。

然後把剛剛得到的elasticsearch-analysis-ik-5.5.0.zip上傳到伺服器。需要把這個檔案elasticsearch-5.5.0/plugins/ik這個路徑下;

解壓壓縮包:

unzip elasticsearch-analysis-ik-5.5.0.zip

到這一步基本上elasticsearch的ik分詞器就安裝好了。示例待會說,下面將一個可能遇到的問題:

如果伺服器上沒有安裝unzip功能的話,可以使用下面的命令列安裝

yum install -y unzip zip

如果服務沒有安裝上傳下載功能的,可以使用下面的方法安裝

yum install -y lrzsz

然後輸入下面命令列就會彈出一個視窗,選擇檔案上傳到伺服器上就可以。

rz

順便說一下,使用下面的命令可以將伺服器上的檔案下載到本地。

sz XXX(下載的檔名)

如果是在linux下:
之前的那些操作之所以在windows下完成是因為在linux環境下很多簡單的操作都要輸入很多命令列,如果有報錯不容易除錯,但是如果可以保證linux下的功能比較完整的話,伺服器上起碼安裝了Git和maven的,可以直接在linux下完成這些功能的:
把專案從github克隆下來

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

如果需要修改elasticsearch版本的,修改一下版本號,然後打包專案

maven package

而後把target/release下的壓縮包移動到elasticsearch下plugins下解壓就OK了。

接下來重啟一下elasticsearch服務,如果出現下面的圖片就基本上算是安裝成功了。說明了elasticsearch就載入成功了。
這裡寫圖片描述

在sense中測試一下,IK中有兩種分詞器,一種是ik_max_word另一種是ik_smart,分別會最大粒度的分詞和最小粒度的分詞:

POST /_analyze
{
  "analyzer":"ik_max_word",
  "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
      }
   ]
}

如果是

POST /_analyze
{
  "analyzer":"ik_smart",
  "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
      }
   ]
}

基本上這樣就把IK分詞器安裝成功了。

拼音分詞器的安裝

拼音分詞器的安裝和Ik的安裝基本上是相同的

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

講專案拉下來,打包專案,將target/release下的壓縮包移動到elasticsearch-5.5.0下的plugins下,重啟一下elasticsearch就可以了。

測試一下:

POST /_analyze
{
    "analyzer":"pinyin",
    "text":"中華人民共和國國歌"
}

得到

{
   "tokens": [
      {
         "token": "zhong",
         "start_offset": 0,
         "end_offset": 1,
         "type": "word",
         "position": 0
      },
      {
         "token": "zhrmghggg",
         "start_offset": 0,
         "end_offset": 9,
         "type": "word",
         "position": 0
      },
      {
         "token": "hua",
         "start_offset": 1,
         "end_offset": 2,
         "type": "word",
         "position": 1
      },
      {
         "token": "ren",
         "start_offset": 2,
         "end_offset": 3,
         "type": "word",
         "position": 2
      },
      {
         "token": "min",
         "start_offset": 3,
         "end_offset": 4,
         "type": "word",
         "position": 3
      },
      {
         "token": "gong",
         "start_offset": 4,
         "end_offset": 5,
         "type": "word",
         "position": 4
      },
      {
         "token": "he",
         "start_offset": 5,
         "end_offset": 6,
         "type": "word",
         "position": 5
      },
      {
         "token": "guo",
         "start_offset": 6,
         "end_offset": 7,
         "type": "word",
         "position": 6
      },
      {
         "token": "guo",
         "start_offset": 7,
         "end_offset": 8,
         "type": "word",
         "position": 7
      },
      {
         "token": "ge",
         "start_offset": 8,
         "end_offset": 9,
         "type": "word",
         "position": 8
      }
   ]
}

順便說一下,在這些分詞結果在處理的時候,就會被存進索引,搜尋的時候,如果輸入“國歌”,“guoge”,“zhrmghggg”,“中國人民共和國國歌”,那麼中華人民共和國國歌就會被索引到。