一、ElasticSearch-IK分詞的安裝:

一、IK分詞的安裝:

原網(wǎng)址:https://www.cnblogs.com/NextNight/p/6837407.html

1、下載IK分詞器:https://github.com/medcl/elasticsearch-analysis-ik/releases 我這里下載的是5.3.2的已經(jīng)編譯的版本,因為這里沒有5.3.1的版本。

2、在Elasticsearch的plugins目錄下新建目錄analysis-ik: mkdir analysis-ik

3、將IK分詞器的壓縮包解壓到analysis-ik目錄下:

[rzxes@rzxes analysis-ik]$ unzip elasticsearch-analysis-ik-5.3.2.zip 查看目錄結(jié)構(gòu)如下:

4、編輯plugin-sescriptor.properties:

修改一些配置,主要是修改elasticsearch.version,因為下載的是5.3.2的而我本身是5.3.1的elasticsearch所以這里修改對應(yīng)即可。

5、啟動Elasticsearch測試IK分詞:[rzxes@rzxes elasticsearch-5.3.1]$ bin/elasticsearch

如下圖可以看到loaded plugin [analysis-ik],說明已經(jīng)加載了插件

IK分詞支持兩種分析器Analyzer: ik_smart , ik_max_word , 兩種分詞器Tokenizer: ik_smart , ik_max_word,

ik_max_word: 會將文本做最細粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,中華人民,中華,華人,人民共和國,人民,人,民,共和國,共和,和,國國,國歌”,會窮盡各種可能的組合;

ik_smart: 會做最粗粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,國歌”。

試驗一下能否進行分詞:調(diào)用Elasticsearch的分詞器API

standard分詞器【analyzer=standard】http://192.168.230.150:9200/_analyze?analyzer=standard&pretty=true&text=hello word西紅柿 結(jié)果如下:

{

  "tokens" : [

    {

      "token" : "hello",

      "start_offset" : 0,

      "end_offset" : 5,

      "type" : "<ALPHANUM>",

      "position" : 0

    },

    {

      "token" : "word",

      "start_offset" : 6,

      "end_offset" : 10,

      "type" : "<ALPHANUM>",

      "position" : 1

    },

    {

      "token" : "西",

      "start_offset" : 10,

      "end_offset" : 11,

      "type" : "<IDEOGRAPHIC>",

      "position" : 2

    },

    {

      "token" : "紅",

      "start_offset" : 11,

      "end_offset" : 12,

      "type" : "<IDEOGRAPHIC>",

      "position" : 3

    },

    {

      "token" : "柿",

      "start_offset" : 12,

      "end_offset" : 13,

      "type" : "<IDEOGRAPHIC>",

      "position" : 4

    }

  ]

}

采用IK分詞器【analyzer=ik_smart】http://192.168.230.150:9200/_analyze?analyzer=ik_smart&pretty=true&text=hello word西紅柿 結(jié)果如下:

{

  "tokens" : [

    {

      "token" : "hello",

      "start_offset" : 0,

      "end_offset" : 5,

      "type" : "ENGLISH",

      "position" : 0

    },

    {

      "token" : "word",

      "start_offset" : 6,

      "end_offset" : 10,

      "type" : "ENGLISH",

      "position" : 1

    },

    {

      "token" : "西紅柿",

      "start_offset" : 10,

      "end_offset" : 13,

      "type" : "CN_WORD",

      "position" : 2

    },

    {

      "token" : "9f",

      "start_offset" : 13,

      "end_offset" : 15,

      "type" : "LETTER",

      "position" : 3

    }

  ]

}

采用IK分詞器【analyzer=ik_max_word】http://192.168.230.150:9200/_analyze?analyzer=ik_max_word&pretty=true&text=hello word中華人民

{

  "tokens" : [

    {

      "token" : "hello",

      "start_offset" : 0,

      "end_offset" : 5,

      "type" : "ENGLISH",

      "position" : 0

    },

    {

      "token" : "word",

      "start_offset" : 6,

      "end_offset" : 10,

      "type" : "ENGLISH",

      "position" : 1

    },

    {

      "token" : "中華人民",

      "start_offset" : 10,

      "end_offset" : 14,

      "type" : "CN_WORD",

      "position" : 2

    },

    {

      "token" : "中華",

      "start_offset" : 10,

      "end_offset" : 12,

      "type" : "CN_WORD",

      "position" : 3

    },

    {

      "token" : "華人",

      "start_offset" : 11,

      "end_offset" : 13,

      "type" : "CN_WORD",

      "position" : 4

    },

    {

      "token" : "人民",

      "start_offset" : 12,

      "end_offset" : 14,

      "type" : "CN_WORD",

      "position" : 5

    }

  ]

}

致此IK分詞就安裝成功了,非常簡單只需要下載編譯包解壓就可以了,至于修改配置是對于版本不對應(yīng)的情況。

二、配置同義詞對應(yīng):

配置同義詞是為了能夠檢索一個詞的時候相關(guān)詞也能夠檢索到。關(guān)聯(lián)詞和同義詞可以合二為一配置在這個文件里。

新建同義詞文件:在Elasticsearch的confg目錄下新建文件夾analysis并在其下創(chuàng)建文件synonyms.txt,這一步可以直接在conf目錄下創(chuàng)建synonyms.txt并不影響,只需要在后面建立縮印的時候指定路徑就行。 mkdir analysis vim synonyms.txt

向文件synonyms.txt添加如下內(nèi)容: 注意‘"逗號"一定是英文的

西紅柿,番茄 =>西紅柿,番茄

社保,公積金 =>社保,公積金

啟動Elasticsearch,此時同義詞就會被加載進來。

三、測試同義詞是否生效:

創(chuàng)建index:自定義分詞器和過濾器并引用IK分詞器。

curl -XPUT 'http://192.168.230.150:9200/index' -d'

{

  "index": {

    "analysis": {

      "analyzer": {

        "by_smart": {

          "type": "custom",

          "tokenizer": "ik_smart",

          "filter": ["by_tfr","by_sfr"],

          "char_filter": ["by_cfr"]

        },

        "by_max_word": {

          "type": "custom",

          "tokenizer": "ik_max_word",

          "filter": ["by_tfr","by_sfr"],

          "char_filter": ["by_cfr"]

        }

      },

      "filter": {

        "by_tfr": {

          "type": "stop",

          "stopwords": [" "]

        },

        "by_sfr": {

          "type": "synonym",

          "synonyms_path": "analysis/synonyms.txt"

        }

      },

      "char_filter": {

        "by_cfr": {

          "type": "mapping",

          "mappings": ["| => |"]

        }

      }

    }

  }

}'

創(chuàng)建mapping:定義一個字段title,并且設(shè)置分詞器analyzer和查詢分詞器search_analyzer.

curl -XPUT 'http://192.168.230.150:9200/index/_mapping/typename' -d'

{

  "properties": {

    "title": {

      "type": "text",

      "index": "analyzed",

      "analyzer": "by_max_word",

      "search_analyzer": "by_smart"

    }

  }

}'

使用自定義分詞器分詞: curl -XGET 'http://192.168.230.150:9200/index/_analyze?pretty=true&analyzer=by_smart' -d '{"text":"番茄"}' 結(jié)果如下:分詞西紅柿?xí)ㄟ^同義詞創(chuàng)建相關(guān)索引。

添加數(shù)據(jù):

curl -XPOST http://192.168.230.150:9200/index/title/1 -d'{"title":"我有一個西紅柿"}'

curl -XPOST http://192.168.230.150:9200/index/title/2 -d'{"title":"番茄炒蛋飯"}'

curl -XPOST http://192.168.230.150:9200/index/title/3 -d'{"title":"西紅柿雞蛋面"}'

檢索數(shù)據(jù):我們從index索引中檢索關(guān)鍵字"番茄"并用標(biāo)簽標(biāo)記命中的關(guān)鍵字。

curl -XPOST http://192.168.230.150:9200/index/title/_search -d'

{

    "query" : { "match" : { "title" : "番茄" }},

    "highlight" : {

        "pre_tags" : ["<tag1>", "<tag2>"],

        "post_tags" : ["</tag1>", "</tag2>"],

        "fields" : {

            "title" : {}

        }

    }

}

結(jié)果如下:命中了三條數(shù)據(jù),命中了"番茄"和他的同義詞"西紅柿".

致此,IK分詞以及同義詞的配置就完成了,。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容