elasticsearch 7.6 在 IK分詞 中使用 同義詞

關於es7.6同義詞的內容真的好難找…………

現在簡單記錄一下實現過程

es同義詞官方文檔傳送

1、安裝同義詞插件

插件下載傳送,其他的同義詞插件未測試

2、插件使用mvn打包(最新版master沒有打包), 使用maven編譯插件

 打包提示   需要修改 elasticsearch-analysis-dynamic-synonym-master 文件下的 pom.xml文件,

把版本改爲自己es對應的版本號,然後進行打包操作

打包操作參考傳送

3、按照打包處理,然後把 elasticsearch-analysis-dynamic-synonym-master\target\releases 的文件 解壓 添加到  elasticsearch/plugins/synonym   文件夾synonym 自己新建

4、添加同義詞詞庫

    在 es 的 config 新建 analysis 文件夾,新建 synonyms.txt 文件(文件名自己定只要統一就可以), 一定要是UTF-8格式。 有幾種格式自己選擇

       synonyms.txt文件測試詞

西紅柿,番茄=>聖女果
哪兒,在哪,何處,什麼地方=>哪裏
狀況,情況=>狀態
註解,註釋=>備註
universe,cosmos

5、啓動es(如果已經啓動,需要重啓,否則不能加載同義詞詞典)

6、重新設置分詞器

    1)關閉所有索引    通過 (postman、curl)post 請求  

            postman方式:localhost:9200/_all/_close

            curl方式:curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_all/_close'

     2)設置索引 通過 (postman、curl)使用put請求

        postman方式:

        接口地址:

             http://localhost:9200/_all/_settings?preserve_existing=true

        參數:

{
  "index.analysis.analyzer.ik_syno_max_word.filter" : [
    "my_synonym_filter"
  ],
  "index.analysis.analyzer.ik_syno_max_word.tokenizer" : "ik_max_word",
  "index.analysis.analyzer.ik_syno_max_word.type" : "custom",
  "index.analysis.analyzer.ik_syno_smart.filter" : [
    "my_synonym_filter"
  ],
  "index.analysis.analyzer.ik_syno_smart.tokenizer" : "ik_smart",
  "index.analysis.analyzer.ik_syno_smart.type" : "custom",
  "index.analysis.filter.my_synonym_filter.synonyms_path" : "analysis/synonyms.txt",
  "index.analysis.filter.my_synonym_filter.type" : "synonym"
}

    返回值

{
    "acknowledged": true
}

            curl示例

            

curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
  "index.analysis.analyzer.ik_syno_max_word.filter" : [
    "my_synonym_filter"
  ],
  "index.analysis.analyzer.ik_syno_max_word.tokenizer" : "ik_max_word",
  "index.analysis.analyzer.ik_syno_max_word.type" : "custom",
  "index.analysis.analyzer.ik_syno_smart.filter" : [
    "my_synonym_filter"
  ],
  "index.analysis.analyzer.ik_syno_smart.tokenizer" : "ik_smart",
  "index.analysis.analyzer.ik_syno_smart.type" : "custom",
  "index.analysis.filter.my_synonym_filter.synonyms_path" : "analysis/synonyms.txt",
  "index.analysis.filter.my_synonym_filter.type" : "synonym"
}'

    3)重新打開所有索引, 通過 (postman、curl)使用 post請求

             postman方式:localhost:9200/_all/_open

            curl 方式:curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_all/_open'

            返回值:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

7、測試同義詞

postman方式:http://localhost:9200/_index/_analyze     _index 爲自己使用了同義詞的 mapping

參數:

{
  "analyzer": "ik_syno_smart",
  "text": "cosmos"
}

 

        curl方式:

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/vas_article/_analyze' -d '{
  "analyzer": "ik_syno_smart",
  "text": "cosmos"
}'

返回值:

{
    "tokens": [
        {
            "token": "cosmos",
            "start_offset": 0,
            "end_offset": 6,
            "type": "ENGLISH",
            "position": 0
        },
        {
            "token": "universe",
            "start_offset": 0,
            "end_offset": 6,
            "type": "SYNONYM",
            "position": 0
        }
    ]
}

以上,完結。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章