1.es 的集成ik 分詞
1.1 ik 分詞
IKAnalyzer是一個開源的,基于java語言開發(fā)的輕量級的中文分詞工具包坤溃。從2006年12月推出1.0版
開始拍霜,IKAnalyzer已經(jīng)推出 了3個大版本。最初薪介,它是以開源項(xiàng)目Lucene為應(yīng)用主體的祠饺,結(jié)合詞典分詞
和文法分析算法的中文分詞組件。新版本的IKAnalyzer3.0則發(fā)展為 面向Java的公用分詞組件汁政,獨(dú)立于
Lucene項(xiàng)目道偷,同時提供了對Lucene的默認(rèn)優(yōu)化實(shí)現(xiàn)。
1.2 ik分詞的特點(diǎn)
1.采用了特有的“正向迭代最細(xì)粒度切分算法“烂完,具有60萬字/秒的高速處理能力试疙。
2.采用了多子處理器分析模式,支持:英文字母(IP地址抠蚣、Email祝旷、URL)、數(shù)字(日期嘶窄,常用中文數(shù)
量詞怀跛,羅馬數(shù)字,科學(xué)計數(shù)法)柄冲,中文詞匯(姓名吻谋、地名處理)等分詞處理。
3.支持個人詞條的優(yōu)化的詞典存儲现横,更小的內(nèi)存占用漓拾。
4.支持用戶詞典擴(kuò)展定義阁最。
5.針對Lucene全文檢索優(yōu)化的查詢分析器IKQueryParser;采用歧義分析算法優(yōu)化查詢關(guān)鍵字的搜索
排列組合骇两,能極大的提高Lucene檢索的命中率速种。
1.3 下載地址:
https://github.com/medcl/elasticsearch-analysis-ik/tags?after=v8.0.0
對應(yīng)文章中es版本.
https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.12.0
1.4 在elasticsearch安裝目錄的plugins目錄下新建 analysis-ik 目錄
[root@basenode analysis-ik]# pwd
/root/tools/elasticsearch/plugins/analysis-ik
[root@basenode analysis-ik]# unzip elasticsearch-analysis-ik-7.12.0.zip
[root@basenode analysis-ik]# rm -rf elasticsearch-analysis-ik-7.12.0.zip
1.5 重啟es
[root@basenode analysis-ik]# docker restart 949389b28b4e 6bc63ce3ab40 ea28731af194
949389b28b4e
6bc63ce3ab40
ea28731af194
[root@basenode analysis-ik]#
遇到一個錯誤 采用上門的訪問就會報下面的錯誤
"at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.12.0.jar:7.12.0]",
"... 6 more"] }
java.lang.IllegalStateException: Could not load plugin descriptor for plugin directory [elasticsearch-analysis-ik-7.12.0]
Likely root cause: java.nio.file.NoSuchFileException: /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-7.12.0/plugin-descriptor.properties
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:218)
at java.base/java.nio.file.Files.newByteChannel(Files.java:375)
at java.base/java.nio.file.Files.newByteChannel(Files.java:426)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
at java.base/java.nio.file.Files.newInputStream(Files.java:160)
at org.elasticsearch.plugins.PluginInfo.readFromProperties(PluginInfo.java:173)
at org.elasticsearch.plugins.PluginsService.readPluginBundle(PluginsService.java:405)
at org.elasticsearch.plugins.PluginsService.findBundles(PluginsService.java:382)
at org.elasticsearch.plugins.PluginsService.getPluginBundles(PluginsService.java:375)
at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:146)
at org.elasticsearch.node.Node.<init>(Node.java:336)
at org.elasticsearch.node.Node.<init>(Node.java:278)
at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:217)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:217)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116)
at org.elasticsearch.cli.Command.main(Command.java:79)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81)
For complete error details, refer to the log at /usr/share/elasticsearch/logs/elasticsearch.log
1.6 在鏡像內(nèi)部進(jìn)行安裝分詞
進(jìn)入docker
docker exec -it elasticsearch /bin/bash
下載安裝
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
確認(rèn)輸入y
過程:
[root@basenode ~]# docker exec -it elasticsearch /bin/bash
^[[D[root@949389b28b4e elasticsearch]#
[root@949389b28b4e elasticsearch]# ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
[root@949389b28b4e elasticsearch]# ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
-> Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
[=================================================] 100%??
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.
Continue with installation? [y/N]y
-> Installed analysis-ik
-> Please restart Elasticsearch to activate any plugins installed
[root@949389b28b4e elasticsearch]#
執(zhí)行
POST _analyze
{
"analyzer": "ik_max_word",
"text":"我是中國人我愛大數(shù)據(jù)"
}
顯示
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "中國人",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "中國",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "國人",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人我",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "愛",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 6
},
{
"token" : "大數(shù)",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "數(shù)據(jù)",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 8
}
]
}
2. 創(chuàng)建索引
語法
PUT /索引名稱
{
"settings":
{ "屬性名": "屬性值" }
}
# 實(shí)例
PUT /wudldb
# 判斷索引是否存在
HEAD /索引名稱
#查看索引
GET /索引名稱
# 批量創(chuàng)建索引
GET /索引名稱1,索引名稱2,索引名稱3,...
# 查看所有索引
GET _all
# 或者
GET /_cat/indices?v
#打開索引
POST /索引名稱/_open
#關(guān)閉索引
POST /索引名稱/_close
#刪除索引
DELETE /索引名稱1,索引名稱2,索引名稱3...
3. 映射操作
PUT /索引庫名/_mapping
{
"properties":
{
"字段名":
{
"type": "類型",
"index": true,
"store": true低千,
"analyzer": "分詞器"
}
}
}
#字段名:任意填寫配阵,下面指定許多屬性,例如:
#type:類型示血,可以是text棋傍、long、short难审、date、integer拂铡、object等
#index:是否索引,默認(rèn)為true
#store:是否存儲失球,默認(rèn)為false
#analyzer:指定分詞器
# 實(shí)例
PUT /wudldb-index
PUT /wudldb-index_mapping/
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"job": {
"type": "text",
"analyzer": "ik_max_word"
},
"logo": {
"type": "keyword",
"index": "false"
},
"payment": {
"type": "float"
}
}
}
3.1 打開索引
#查看映射關(guān)系
GET /索引名稱/_mapping
# 或者
GET _mapping
#或者
GET _all/_mapping
3.2 修改索引
修改索引和映射之間的關(guān)系
PUT /索引庫名/_mapping
{
"properties": {
"字段名": {
"type": "類型",
"index": true烈疚,
"store": true爷肝,
"analyzer": "分詞器"
}
}
}
3.3 打開索引
POST /索引名稱/_open
3.4 關(guān)閉索引
P0ST /索引名稱/_close
3.5 刪除索引
DELETE /索引名稱1,索引名稱2,索引名稱3...
4. 索引的映射
簡介:
索引創(chuàng)建之后金赦,等于有了關(guān)系型數(shù)據(jù)庫中的database夹抗。Elasticsearch7.x取消了索引type類型的設(shè)置杏愤,
不允許指定類型乏奥,默認(rèn)為_doc邓了,但字段仍然是有的照宝,我們需要設(shè)置字段的約束信息,叫做字段映射(mapping)
4.1 創(chuàng)建索引字段
PUT /索引庫名/_mapping
{
"properties":
{
"字段名":
{
"type": "類型",
"index": true,
"store": true,
"analyzer": "分詞器"
}
}
}
字段名:任意填寫,下面指定許多屬性忆矛,例如:
type:類型,可以是text漫拭、long、short挑宠、date、integer碎浇、object等
index:是否索引悉默,默認(rèn)為true
store:是否存儲抄课,默認(rèn)為false
analyzer:指定分詞器
4.1.1 實(shí)例
PUT /wudldb-index
PUT /wudldb-index/_mapping/
{
"properties": {
"name": {
"type": "text"
},
"job": {
"type": "text"
},
"logo": {
"type": "keyword",
"index": "false"
},
"payment": {
"type": "float"
}
}
}
4.2 映射的字段類型簡介
基本可以分為以下幾類
String類型攒盈,又分兩種
1.text:可分詞僵蛛,不可參與聚合
2.keyword:不可分詞,數(shù)據(jù)會作為完整字段進(jìn)行匹配喉酌,可以參與聚合纪铺。
Numerical:數(shù)值類型突诬,分兩類
1.基本數(shù)據(jù)類型:long、interger蔬捷、short铡俐、byte滩报、double、float旅择、half_float.
2.浮點(diǎn)數(shù)的高精度類型:scaled_float.
1.需要指定一個精度因子,比如10或100侣姆。elasticsearch會把真實(shí)值乘以這個因子后存儲.
Date:日期類型
1.elasticsearch可以對日期格式化為字符串存儲生真,但是建議我們存儲為毫秒值,存儲為long捺宗,節(jié)省
空間柱蟀。
Array:數(shù)組類型:
1.進(jìn)行匹配時,任意一個元素滿足蚜厉,都認(rèn)為滿足
2.排序時长已,如果升序則用數(shù)組中的最小值來排序,如果降序則用數(shù)組中的最大值來排序
Object:對象
4.3 查看映射關(guān)系
GET /索引名稱/_mapping 或者
GET _all/_mapping
GET _mapping
4.4 一次性創(chuàng)建索引和映射
put /索引庫名稱
{
"settings":{
"索引庫屬性名":"索引庫屬性值"
},
"mappings":{
"properties":{
"字段名":{
"映射屬性名":"映射屬性值"
}
}
}
}
#實(shí)例
PUT /wubigdata
{
"settings": {},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
5. 文檔增刪改查操作
5.1 新增文檔
實(shí)例
POST /wubigdata/_doc/1
{
"name": "flink",
"job": "大數(shù)據(jù)架構(gòu)師",
"payment": "30000",
"logo": "https://image.baidu.com/"
}
自動生成id
POST /索引名稱/_doc
{
"field":"value"
}
5.2 查看單個文檔
語法
GET /索引名稱/_doc/{id}
5.3 查看所有文檔
/索引名稱/_search
{
"query":{
"match_all": { }
}
}
5.4 _source定制返回結(jié)果
場景:
某些業(yè)務(wù)場景下昼牛,我們不需要搜索引擎返回source中的所有字段术瓮,可以使用source進(jìn)行定制,如下贰健,多個字段之間使用逗號分隔胞四。
實(shí)例:
GET /wubigdata/_doc/1?_source=name,job
5.5 文檔的更新
5.5.1 全部更新
POST /wubigdata/_doc/1
{
"name": "flink",
"job": "大數(shù)據(jù)架構(gòu)師2",
"payment": "30000",
"logo": "https://image.baidu.com/"
}
5.5.1 局部更新
POST /索引名/_update/{id}
{
"doc":{
"field":"value"
}
}
5.6 刪除文檔
根據(jù)id 進(jìn)行刪除
DELETE /索引名/_doc/{id}
刪除全部文檔
POST 索引名/_delete_by_query
{
"query": {
"match_all": {}
}
}
5.7 文檔的全量替換、強(qiáng)制創(chuàng)建
強(qiáng)制創(chuàng)建
PUT /index/_doc/{id}?op_type=create {}伶椿,PUT /index/_doc/{id}/_create {}
如果id 存在就會報錯