[大數據]-Elasticsearch5.3.1 IK分詞,同義詞/聯想搜索設置


--題外話:最近發現了一些問題,一些高搜索量的東西相當一部分沒有價值。發現大部分是一些問題的錯誤日志。而我是個比較愛貼圖的。搜索引擎的檢索會將我們的博文文本分詞。所以圖片內容一般是檢索不到的,也就是說同樣的問題最好是帖錯誤代碼,日志,雖然圖片很直觀,但是並不利與傳播。希望大家能夠優化一部分博文的內容,這樣有價值的東西傳播量可能會更高。

本文主要是記錄Elasticsearch5.3.1 IK分詞,同義詞/聯想搜索設置,本來是要寫fscrawler的多種格式(html,pdf,word...)數據導入的,但是IK分詞和同義詞配置還是折騰了兩天,沒有很詳細的內容,這里決定還是記錄下來。IK Analyzer是一個開源的,基於java語言開發的輕量級的中文分詞工具包。從2006年12月推出1.0版開始, IKAnalyzer已經推出了3個大版本。最初,它是以開源項目Luence為應用主體的,結合詞典分詞和文法分析算法的中文分詞組件。新版本的IK Analyzer 3.0則發展為面向Java的公用分詞組件,獨立於Lucene項目,同時提供了對Lucene的默認優化實現。所以IK跟ES本來是天生一對,當然是對於中文來說,起碼對於英文分詞來說,空格分詞就足夠簡單粗暴。中文檢索為了達到更好的檢索效果分詞效果還是很重要的,所以IK分詞插件有必要一試。

一、IK分詞的安裝:

1、下載IK分詞器:https://github.com/medcl/elasticsearch-analysis-ik/releases 我這里下載的是5.3.2的已經編譯的版本,因為這里沒有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 查看目錄結構如下:

4、編輯plugin-sescriptor.properties:

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

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

  • 如下圖可以看到loaded plugin [analysis-ik],說明已經加載了插件
  • IK分詞支持兩種分析器Analyzer: ik_smart , ik_max_word , 兩種分詞器Tokenizer: ik_smart , ik_max_word,

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

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

  • 試驗一下能否進行分詞:調用Elasticsearch的分詞器API
    • standard分詞器【analyzer=standard】http://192.168.230.150:9200/_analyze?analyzer=standard&pretty=true&text=hello word西紅柿 結果如下:
    • {
        "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西紅柿 結果如下:
    • {
        "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分詞就安裝成功了,非常簡單只需要下載編譯包解壓就可以了,至於修改配置是對於版本不對應的情況。

二、配置同義詞對應:

  • 配置同義詞是為了能夠檢索一個詞的時候相關詞也能夠檢索到。關聯詞和同義詞可以合二為一配置在這個文件里。
  • 新建同義詞文件:在Elasticsearch的confg目錄下新建文件夾analysis並在其下創建文件synonyms.txt,這一步可以直接在conf目錄下創建synonyms.txt並不影響,只需要在后面建立縮印的時候指定路徑就行。 mkdir analysis   vim synonyms.txt 
  • 向文件synonyms.txt添加如下內容: 注意‘"逗號"一定是英文的
  • 西紅柿,番茄 =>西紅柿,番茄
    社保,公積金 =>社保,公積金
  • 啟動Elasticsearch,此時同義詞就會被加載進來。

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

  • 創建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": ["| => |"]
            }
          }
        }
      }
    }'
  • 創建mapping:定義一個字段title,並且設置分詞器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":"番茄"}' 結果如下:分詞西紅柿會通過同義詞創建相關索引。
  • 添加數據:
  • 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":"西紅柿雞蛋面"}'
  • 檢索數據:我們從index索引中檢索關鍵字"番茄"並用標簽標記命中的關鍵字。
  • 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" : {}
            }
        }
    }
    '

    結果如下:命中了三條數據,命中了"番茄"和他的同義詞"西紅柿".

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

三、存在的故障和問題:

  • 非常感謝寫這邊http://blog.csdn.net/u012859681/article/details/60147864文章的博友,我寫的很大一部分是參考他的,但是其中有些問題試驗不通過。可能是自身配的問題,大家可以多方參考。
  • 故障1:講道理如下兩個結果應該是一樣的,但是這里卻是如下,可能是哪里有問題。。。【此故障已解決,機子腦抽了,睡一覺起來自己就好了】
  • 問題2:有沒有可以直接配置的,按這樣來的話見建一次索引就要設置一次分詞器,有沒有直接修改默認配置的方法。????
    • 以前好像是配置文件加下面兩行:
      index.analysis.analyzer.default.type" : "ik",
      index.analysis.analyzer.default.use_smart" : "true"
      但是爆了錯:
      Since elasticsearch 5.x index level settings can NOT be set on the nodes 
      configuration like the elasticsearch.yaml, in system properties or command line 
      arguments.In order to upgrade all indices the settings must be updated via the 
      /${index}/_settings API. Unless all settings are dynamic all indices must be closed 
      in order to apply the upgradeIndices created in the future should use index templates 
      to set default values. 
      5.x版本以后就不支持這種設置方式,因為考慮到后面的一些更新。
      In order to upgrade all indices the settings must be updated via the /${index}/_settings API
      沒理解錯的話說了半天還是要跟Index綁定。
    • ES中文社區爆6.0可能會移除type,
  • 問題3:synonyms.txt這個同義詞配置文件中的格式有哪幾種,分別表示什么??如故障1中的文章內提到的兩種格式,一種有"=>",另一無"=>",但是第二種我試驗有問題。【此問題已解決,兩種配置都可用】如下所示:添加一行如下:兒童,青年,少年,幼年。不過這兩種建立索引的方式有什么區別還沒有弄明白。
  • 兒童,青年,少年,幼年
    西紅柿,番茄 => 西紅柿,番茄
    社保,公積金 => 社保,公積金
  • 重啟ES再進行分詞:curl -XGET 'http://192.168.230.150:9200/index/_analyze?pretty=true&analyzer=by_smart' -d '{"text":"青年"}' 結果如下:

  • [rzxes@rzxes elasticsearch-5.3.1]$  curl -XGET 'http://192.168.230.150:9200/index/_analyze?pretty=true&analyzer=by_smart' -d '{"text":"青年"}'
    {
      "tokens" : [
        {
          "token" : "青年",
          "start_offset" : 0,
          "end_offset" : 2,
          "type" : "CN_WORD",
          "position" : 0
        },
        {
          "token" : "兒童",
          "start_offset" : 0,
          "end_offset" : 2,
          "type" : "SYNONYM",
          "position" : 0
        },
        {
          "token" : "少年",
          "start_offset" : 0,
          "end_offset" : 2,
          "type" : "SYNONYM",
          "position" : 0
        },
        {
          "token" : "幼年",
          "start_offset" : 0,
          "end_offset" : 2,
          "type" : "SYNONYM",
          "position" : 0
        }
      ]
    }

四、聯想檢索:(這種檢索名稱純屬個人杜撰)

  • 目標:搜索 "筆記本",出現"聯想","戴爾","電腦"。。等等相關連的詞。類似ML的相似度高的詞。。或者推薦系統。
  • 在這里只需要類比同義詞,配置synonyms.txt。將檢索詞與關聯詞做對應就可以了。

 


免責聲明!

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



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