日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ElasticSearch中文分词器-IK分词器的使用

發布時間:2025/3/20 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch中文分词器-IK分词器的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IK分詞器的使用

首先我們通過Postman發送GET請求查詢分詞效果

GET http://localhost:9200/_analyze {"text":"農業銀行" }

得到如下結果,可以發現es的默認分詞器無法識別中文中農業、銀行這樣的詞匯,而是簡單的將每個字拆完分為一個詞,這顯然不符合我們的使用要求。

Copy {"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}] }


首先我們訪問?https://github.com/medcl/elasticsearch-analysis-ik/releases?下載與es對應版本的中文分詞器。將解壓后的后的文件夾放入es根目錄下的plugins目錄下,重啟es即可使用。

我們這次加入新的參數"analyzer":"ik_max_word"

  • k_max_word:會將文本做最細粒度的拆分,例如「中華人民共和國國歌」會被拆分為「中華人民共和國、中華人民、中華、華人、人民共和國、人民、人、民、共和國、共和、和、國國、國歌」,會窮盡各種可能的組合
  • ik_smart:會將文本做最粗粒度的拆分,例如「中華人民共和國國歌」會被拆分為「中華人民共和國、國歌」
GET http://localhost:9200/_analyze {"analyzer":"ik_max_word","text":"農業銀行" }

得到如下結果

{"tokens": [{"token": "農業銀行","start_offset": 0,"end_offset": 4,"type": "CN_WORD","position": 0},{"token": "農業","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 1},{"token": "銀行","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 2}] }

百度搜索中每天都會收錄新的詞匯,es中也可以進行擴展詞匯。

我們首先查詢弗雷爾卓德字段

GET http://localhost:9200/_analyze {"analyzer":"ik_max_word","text":"弗雷爾卓德" }

僅僅可以得到每個字的分詞結果,我們需要做的就是使分詞器識別到弗雷爾卓德也是一個詞語。

{"tokens": [{"token": "弗","start_offset": 0,"end_offset": 1,"type": "CN_CHAR","position": 0},{"token": "雷","start_offset": 1,"end_offset": 2,"type": "CN_CHAR","position": 1},{"token": "爾","start_offset": 2,"end_offset": 3,"type": "CN_CHAR","position": 2},{"token": "卓","start_offset": 3,"end_offset": 4,"type": "CN_CHAR","position": 3},{"token": "德","start_offset": 4,"end_offset": 5,"type": "CN_CHAR","position": 4}] }

首先進入es根目錄中的plugins文件夾下的ik文件夾,進入config目錄,創建custom.dic文件,寫入弗雷爾卓德。同時打開IKAnalyzer.cfg文件,將新建的custom.dic配置其中,重啟es。

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties><comment>IK Analyzer 擴展配置</comment><!--用戶可以在這里配置自己的擴展字典 --><entry key="ext_dict">custom.doc</entry><!--用戶可以在這里配置自己的擴展停止詞字典--><entry key="ext_stopwords"></entry><!--用戶可以在這里配置遠程擴展字典 --><!-- <entry key="remote_ext_dict">words_location</entry> --><!--用戶可以在這里配置遠程擴展停止詞字典--><!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>

再次查詢發現es的分詞器可以識別到弗雷爾卓德詞匯

{"tokens": [{"token": "弗雷爾卓德","start_offset": 0,"end_offset": 5,"type": "CN_WORD","position": 0},{"token": "弗雷爾","start_offset": 0,"end_offset": 3,"type": "CN_WORD","position": 1},{"token": "卓","start_offset": 3,"end_offset": 4,"type": "CN_CHAR","position": 2},{"token": "德","start_offset": 4,"end_offset": 5,"type": "CN_CHAR","position": 3}] }

我們也可以配置遠程擴展字典

我們啟動一個nginx服務器,創建分詞文件 es/fenci.txt

弗雷爾卓德 德瑪西亞 祖安

配置訪問路徑

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties><comment>IK Analyzer 擴展配置</comment><!--用戶可以在這里配置自己的擴展字典 --><entry key="ext_dict">custom.doc</entry><!--用戶可以在這里配置自己的擴展停止詞字典--><entry key="ext_stopwords"></entry><!--用戶可以在這里配置遠程擴展字典 --><!-- <entry key="remote_ext_dict">http://xxx.xxx.xxx.xx/es/fenci.txt</entry> --><!--用戶可以在這里配置遠程擴展停止詞字典--><!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>

本文轉載自:

作者:?海向

出處:https://www.cnblogs.com/haixiang/p/11810799.html

總結

以上是生活随笔為你收集整理的ElasticSearch中文分词器-IK分词器的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。