Elasticsearch的基本操作

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 存在就會報錯
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辜伟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子脊另,更是在濱河造成了極大的恐慌导狡,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件偎痛,死亡現(xiàn)場離奇詭異旱捧,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)看彼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門廊佩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來囚聚,“玉大人,你說我怎么就攤上這事标锄⊥缰” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵料皇,是天一觀的道長谓松。 經(jīng)常有香客問我,道長践剂,這世上最難降的妖魔是什么鬼譬? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮逊脯,結(jié)果婚禮上优质,老公的妹妹穿的比我還像新娘。我一直安慰自己军洼,他們只是感情好巩螃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著匕争,像睡著了一般避乏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上甘桑,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天拍皮,我揣著相機(jī)與錄音,去河邊找鬼跑杭。 笑死铆帽,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的艘蹋。 我是一名探鬼主播锄贼,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼女阀!你這毒婦竟也來了宅荤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤浸策,失蹤者是張志新(化名)和其女友劉穎冯键,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庸汗,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡惫确,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片改化。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡掩蛤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出陈肛,到底是詐尸還是另有隱情揍鸟,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布句旱,位于F島的核電站阳藻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谈撒。R本人自食惡果不足惜腥泥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望啃匿。 院中可真熱鬧蛔外,春花似錦、人聲如沸立宜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽橙数。三九已至,卻和暖如春帅戒,著一層夾襖步出監(jiān)牢的瞬間灯帮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工逻住, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钟哥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓瞎访,卻偏偏與公主長得像腻贰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子扒秸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容