tft每日頭條

 > 生活

 > elasticsearch分詞器插件開發

elasticsearch分詞器插件開發

生活 更新时间:2024-11-11 16:26:46

elasticsearch分詞器插件開發?[elk@linux1 ~]$ curl -X GET 'http://localhost:9200/_cat/indices?v',今天小編就來說說關于elasticsearch分詞器插件開發?下面更多詳細答案一起來看看吧!

elasticsearch分詞器插件開發(Elasticsearch配置---驗證中文分詞插件)1

elasticsearch分詞器插件開發

  1. 創建Index:

[elk@linux1 ~]$ curl -X GET 'http://localhost:9200/_cat/indices?v'

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

創建名稱為index1的Index:

[elk@linux1 ~]$ curl -XPUT http://localhost:9200/index1

{"acknowledged":true,"shards_acknowledged":true,"index":"index1"}[elk@linux1 ~]$

[elk@linux1 ~]$

[elk@linux1 ~]$ curl -X GET 'http://localhost:9200/_cat/indices?v'

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open index1 9jeo0t0eSh-Lw3Ntb73g5A 1 1 0 0 230b 230b

創建名稱為accounts的Index:

[elk@linux1 ~]$ curl -X PUT 'localhost:9200/accounts'

{"acknowledged":true,"shards_acknowledged":true,"index":"accounts"}[elk@linux1 ~]$

[elk@linux1 ~]$

[elk@linux1 ~]$ curl -X GET 'http://localhost:9200/_cat/indices?v'

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open index1 9jeo0t0eSh-Lw3Ntb73g5A 1 1 0 0 230b 230b

yellow open accounts nK6QbHUwS4CUvHJf43BwTA 1 1 0 0 230b 230b


2.創建mapping

curl -XPOST http://localhost:9200/index1/_mapping -H 'Content-type:application/json' -d'

{

"properties": {

"content": {

"type": "text",

"analyzer": "ik_max_word",

"search_analyzer": "ik_max_word"

}

}

}'

curl -XPOST 'localhost:9200/accounts/_mapping' -H 'Content-Type:application/json' -d '

{

"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"

}

}

}'

【注釋】有三個字段:user, title, desc。這三個字段都是中文,而且類型都是文本(text),所以需要指定中文分詞器,不能使用默認的英文分詞器。

【注釋】創建Index和mapping的兩步(先PUT再POST到_mapping)可以一步(PUT)完成:

curl -X PUT 'localhost:9200/accounts' -H 'Content-Type:application/json' -d '

{

"mappings": {

"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"

}

}

}

}'

3.數據操作

Elastic 的分詞器稱為 analyzer。我們對每個字段指定分詞器。

"user": {

"type": "text",

"analyzer": "ik_max_word",

"search_analyzer": "ik_max_word"

}

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

以accounts 這個Index為例:

a) 新增記錄: 向指定的 /Index 發送 PUT 請求,就可以在 Index 裡面新增一條記錄。比如,向/accounts發送請求,就可以新增一條人員記錄。

curl -X PUT 'localhost:9200/accounts/_create/1' -H 'Content-Type:application/json' -d '

{

"user": "張三",

"title": "工程師",

"desc": "數據庫管理"

}'

{

"_index": "accounts",

"_type": "_doc",

"_id": "1",

"_version": 1,

"result": "created",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"_seq_no": 0,

"_primary_term": 1

}

服務器返回的 JSON 對象,會給出 Index、Type、Id、Version 等信息。

curl -X PUT 'localhost:9200/accounts/_create/2' -H 'Content-Type:application/json' -d '

{

"user": "張三四",

"title": "工程師",

"desc": "中間件管理"

}'

b) 查詢記錄

curl -XPOST http://localhost:9200/accounts/_search -H 'Content-Type:application/json' -d'

{

"query" : { "match" : { "user" : "張三" }},

"highlight" : {

"pre_tags" : ["<tag1>", "<tag2>"],

"post_tags" : ["</tag1>", "</tag2>"],

"fields" : {

"user" : {}

}

}

}

'

{

"took": 328,

"timed_out": false,

"_shards": {

"total": 1,

"successful": 1,

"skipped": 0,

"failed": 0

},

"hits": {

"total": {

"value": 1,

"relation": "eq"

},

"max_score": 0.8630463,

"hits": [

{

"_index": "accounts",

"_type": "_doc",

"_id": "1",

"_score": 0.8630463,

"_source": {

"user": "張三",

"title": "工程師",

"desc": "數據庫管理"

},

"highlight": {

"user": [

"<tag1>張</tag1><tag1>三</tag1>"

]

}

}

]

}

}

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved