ElasticSearch簡介(三)——中文分詞

不少時候,咱們須要在ElasticSearch中啓用中文分詞,本文這裏簡單的介紹一下方法。首先安裝中文分詞插件。這裏使用的是 ik,也能夠考慮其餘插件(好比 smartcn)。html

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.2.0/elasti csearch-analysis-ik-7.2.0.zipgit

上面代碼安裝的是7.2.0版的插件,與 Elastic 7.2.0 配合使用。github

PS:其它插件命令:elasticsearch-plugin help 數據結構

接着,從新啓動 Elastic,就會自動加載這個新安裝的插件。app

而後,新建一個 Index,指定須要分詞的字段。這一步根據數據結構而異,下面的命令只針對本文。基本上,凡是須要搜索的中文字段,都要單獨設置一下。elasticsearch

PUT /accounts
{
    "mappings": {
        "person": {
            "properties": {
                "user": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "desc": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
        }
    }
}
ide

上面代碼中,首先新建一個名稱爲accounts的 Index,裏面有一個名稱爲person的 Type。person有三個字段。 ui

  • user
  • title
  • desc

這三個字段都是中文,並且類型都是文本(text),因此須要指定中文分詞器,不能使用默認的英文分詞器。 spa

Elastic 的分詞器稱爲 analyzer。咱們對每一個字段指定分詞器。 插件

"user": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_max_word"
}

上面代碼中,analyzer是字段文本的分詞器,search_analyzer是搜索詞的分詞器。ik_max_word分詞器是插件ik提供的,能夠對文本進行最大數量的分詞。

相關文章
相關標籤/搜索