亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

Elasticsearch中ik分詞器的使用

crossea / 1154人閱讀

摘要:比如對(duì)中華人民共和國(guó)進(jìn)行分詞,會(huì)認(rèn)為這就是一個(gè)詞,結(jié)果就是一個(gè)中華人民共和國(guó)。和配合使用一般情況下,為了提高搜索的效果,需要這兩種分詞器配合使用。我們首先在內(nèi)創(chuàng)建一個(gè)叫的索引,其中名字的分詞器用的是。

1.Elasticsearch默認(rèn)分詞器

? ? ? ? 我在之前的文章中介紹過(guò) Elasticsearch的安裝和使用,這里我們使用Kibina作為工具來(lái)操作es,可以使用es的_analyze來(lái)分析分詞器的分詞結(jié)果。

? ? ? ? ES默認(rèn)的分詞器為英文分詞器,對(duì)英文句子能做到比較好的分詞,我們看一個(gè)例子。當(dāng)輸入以下請(qǐng)求時(shí),對(duì)"What"s your name"句子進(jìn)行分詞,能看到將幾個(gè)詞都分了出來(lái)。

POST _analyze
{
  "tokenizer": "standard",
  "text": "What"s your name"
}? ? ? ??

{
  "tokens" : [
    {
      "token" : "What"s",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "your",
      "start_offset" : 7,
      "end_offset" : 11,
      "type" : "",
      "position" : 1
    },
    {
      "token" : "name",
      "start_offset" : 12,
      "end_offset" : 16,
      "type" : "",
      "position" : 2
    }
  ]
}

? ? ? ? 當(dāng)輸入中文"你叫什么名字"時(shí),可以看到標(biāo)準(zhǔn)分詞器將句子分成了一個(gè)一個(gè)的字,這顯然在我們實(shí)際使用的過(guò)程中是沒(méi)辦法接受的。

POST _analyze
{
  "tokenizer": "standard",
  "text": "你叫什么名字"
}

{
  "tokens" : [
    {
      "token" : "你",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "叫",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "",
      "position" : 1
    },
    {
      "token" : "什",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "",
      "position" : 2
    },
    {
      "token" : "么",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "",
      "position" : 3
    },
    {
      "token" : "名",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "",
      "position" : 4
    },
    {
      "token" : "字",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "",
      "position" : 5
    }
  ]
}

2. IK分詞器

? ? ? ? 由于英文句子都是使用空{(diào) "tokens" : [ { "token" : "你", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "叫什么", "start_offset" : 1, "end_offset" : 4, "type" : "CN_WORD", "position" : 1 }, { "token" : "名字", "start_offset" : 4, "end_offset" : 6, "type" : "CN_WORD", "position" : 2 } ] } 格進(jìn)行分隔,因此在分詞比較明確,但是中文由于語(yǔ)言特性,分詞比較難分,也容易產(chǎn)生分詞歧義,如果自己開(kāi)發(fā)分詞器,成本會(huì)比較大,所以一般在使用過(guò)程中都會(huì)用一些分詞器,比較著名的有Jieba分詞器,hanlp等,我們這里介紹一個(gè)es的插件分詞器,ik分詞器。可以從github下載分詞器的壓縮包,下載地址:?github.com/medcl/elast…?,在es的plugins目錄下創(chuàng)建一個(gè)ik的目錄,把解壓后的文件放到ik目錄下,然后重啟Elasticsearch。

? ? ? ? 這時(shí),我們把之前的分詞器換成ik_smart,再來(lái)看效果。可以看到用ik_smart已經(jīng)能夠?qū)⒅形倪M(jìn)行分詞。

POST _analyze
{
  "tokenizer": "ik_smart",
  "text": "你叫什么名字"
}

{
  "tokens" : [
    {
      "token" : "你",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "叫什么",
      "start_offset" : 1,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "名字",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

? ? ? ? 除了ik_smart之外,還有一個(gè)ik_max_wrod分詞器。

ik_smart會(huì)將文本做比較粗粒度的切分。比如對(duì)中華人民共和國(guó)進(jìn)行分詞,會(huì)認(rèn)為這就是一個(gè)詞,結(jié)果就是一個(gè)中華人民共和國(guó)。

而ik_max_word則對(duì)文本做比較細(xì)粒度的切分,會(huì)出現(xiàn)各種長(zhǎng)度的詞。如果同樣對(duì)中華人民共和國(guó)進(jìn)行分詞,會(huì)分出很多的詞。

{
  "tokens" : [
    {
      "token" : "中華人民共和國(guó)",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "中華人民",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "中華",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "華人",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "人民共和國(guó)",
      "start_offset" : 2,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "人民",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "共和國(guó)",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "共和",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "國(guó)",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 8
    }
  ]
}

? ? ? ? 這兩種分詞器在應(yīng)對(duì)具體的場(chǎng)景時(shí),需要選擇合適的分詞器進(jìn)行使用。

3. ik_smart和ik_max_word配合使用

? ? ? ?一般情況下,為了提高搜索的效果,需要這兩種分詞器配合使用。既索引時(shí)用ik_max_word盡可能多的分詞,而搜索時(shí)用ik_smart盡可能提高匹配準(zhǔn)度,讓用戶(hù)的搜索盡可能的準(zhǔn)確。比如一個(gè)常見(jiàn)的場(chǎng)景,就是搜索"進(jìn)口紅酒"的時(shí)候,盡可能的不要出現(xiàn)口紅相關(guān)商品或者讓口紅不要排在前面。

? ? ? ? 我們首先在Elasticsearch內(nèi)創(chuàng)建一個(gè)叫g(shù)oods的索引,其中名字的分詞器用的是ik_max_word。

PUT /goods
{
  "mappings":{
	"goods": {
		"properties": {
			"id": {
				"type": "keyword"
			},
			"name": {
				"analyzer": "ik_max_word",
				"type": "text"
			}
		}
	  }
  },
  "settings":{
            "index": {
                "refresh_interval": "1s",
                "number_of_shards": 5,
                "max_result_window": "10000000",
                "mapper": {
                    "dynamic": "false"
                },
                "number_of_replicas": 0
            }
  }
}

? ? ? ? ?然后我們通過(guò)POST請(qǐng)求,往里面添加一些數(shù)據(jù)。

POST /goods/goods
{
  "id":"1",
  "name":"美麗粉色口紅明星"
}

POST /goods/goods
{
  "id":"2",
  "name":"好喝的進(jìn)口紅酒"
}

POST /goods/goods
{
  "id":"3",
  "name":"進(jìn)口紅酒真好喝"
}

? ? ? ? 最后,在查詢(xún)的時(shí)候,我們指定查詢(xún)分詞器為ik_smart。

GET /goods/goods/_search
{
  "query":{
    "match": {
      "name": {
        "query": "進(jìn)口紅酒",
        "analyzer": "ik_smart"
      }
    
    }
  }
}

? ? ? ? 可以看到兩條進(jìn)口紅酒相關(guān)的記錄被搜了出來(lái),但是口紅沒(méi)有被搜出來(lái)

{
  "took" : 28,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.36464313,
    "hits" : [
      {
        "_index" : "goods",
        "_type" : "goods",
        "_id" : "cdLk1WoBvRMfJWIKVfOP",
        "_score" : 0.36464313,
        "_source" : {
          "id" : "3",
          "name" : "進(jìn)口紅酒真好喝"
        }
      },
      {
        "_index" : "goods",
        "_type" : "goods",
        "_id" : "ctLk1WoBvRMfJWIKX_O6",
        "_score" : 0.36464313,
        "_source" : {
          "id" : "2",
          "name" : "好喝的進(jìn)口紅酒"
        }
      }
    ]
  }
}

4. 總結(jié)

? ? ? ? 分詞器是Elasticsearch中很重要的一部分,網(wǎng)上也有很多開(kāi)源的分詞器,對(duì)于一般的應(yīng)用這些開(kāi)源分詞器也許足夠用了,但是在某些特定的場(chǎng)景下,可能需要對(duì)分詞器做一些優(yōu)化,甚至需要自研一些分詞器。


文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/7320.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<