elasticsearch7.8.0入门操作
一、基本概念
Cluster:集群, 由一組 es 實(shí)例組成
Node:節(jié)點(diǎn), 單個(gè) es 運(yùn)行的實(shí)例
Index:索引, 類似于數(shù)據(jù)表
Type 類型ES7以后固定值是 _doc
Document: 文檔, 類似于數(shù)據(jù)表中的一條數(shù)據(jù)
Field: 字段, 類似于數(shù)據(jù)表的字段
Mapping:映射, 類似于數(shù)據(jù)表的字段定義, 包括數(shù)據(jù)類型
二、索引-增刪改查
2.1 創(chuàng)建索引,PUT請(qǐng)求,不能重復(fù)添加
http://192.168.0.104:9200/shopping2.2 全查索引,GET請(qǐng)求
http://192.168.0.104:9200/_cat/indices?v2.3 單個(gè)索引查詢,GET請(qǐng)求
http://192.168.0.104:9200/shopping2.4 刪除索引,DELETE
http://192.168.0.104:9200/shopping三、文檔-增刪改查
3.1 創(chuàng)建文檔,POST請(qǐng)求
// 隨機(jī)主鍵id http://192.168.0.104:9200/shopping/_doc/// 指定主鍵 http://192.168.0.104:9200/shopping/_doc/13.2 查詢文檔,GET請(qǐng)求
// 主鍵查詢 http://192.168.0.104:9200/shopping/_doc/1// 全查詢 http://192.168.0.104:9200/shopping/_doc/_search3.3 修改文檔,POST請(qǐng)求
http://192.168.0.104:9200/shopping/_doc/1 // 全量修改的json參數(shù) {"title": "蘋果13","category": "蘋果","price": 6999.00 }// 局部修改的json參數(shù) http://192.168.0.104:9200/shopping/_update/1 {"doc": {"title": "蘋果13","price": 6998.00} }四、常見(jiàn)查詢
4.1?條件查詢,GET請(qǐng)求
http://192.168.0.104:9200/shopping/_search // 查詢category為蘋果的文檔,json參數(shù) {"query":{"match":{"category":"蘋果"}} }// 全條件查詢 {"query":{"match_all":{}} }// 查詢某一個(gè)字段 {"query":{"match_all":{}},"_source":["title"] }// 分頁(yè)查詢 {"query":{"match_all":{}},"from":0,"size":3 }// 排序 {"query":{"match_all":{}},"sort":{"price":{"order":"desc"}} }// 多條件查詢,must類似&& {"query":{"bool":{"must":[{"match":{"category":"蘋果"}},{"match":{"price":8999.00}}]}} } // should類似|| {"query":{"bool":{"should":[{"match":{"category":"蘋果"}},{"match":{"category":"華為"}}],"filter":{"range":{"price":{"gt":9000}}}}} }五、全文檢索、完全匹配、高亮查詢
5.1 全文檢索,類似我們百度搜索一樣,輸入“蘋為”,可以查出蘋果和華為的數(shù)據(jù)。
{"query":{"match":{"category" : "蘋為"}} }5.2 完全匹配,只會(huì)篩選出蘋果的數(shù)據(jù)
{"query":{"match_phrase":{"category" : "蘋"}} }5.3 高亮查詢
{"query":{"match_phrase":{"category" : "蘋"}},"highlight":{"fields":{"category":{}//<----高亮這字段}} }返回結(jié)果中蘋字被高亮展示"<em>蘋</em>果"六、聚合查詢(類似數(shù)據(jù)庫(kù)的聚合查詢)
6.1 分組
按照price分組
{"aggs":{//聚合操作"price_group":{//名稱,隨意起名"terms":{//分組"field":"price"//分組字段}}} }// 不帶原始數(shù)據(jù)返回 {"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0 }但是如果按照category分組
{"aggs":{//聚合操作"category_group":{//名稱,隨意起名"terms":{//分組"field":"category"//分組字段}}} }則報(bào)錯(cuò)
Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [category] in order to load field data by uninverting the inverted index. Note that this can use significant memory.度娘之后大概的意思就是category這個(gè)字段在建立的時(shí)候沒(méi)有進(jìn)行優(yōu)化,默認(rèn)沒(méi)有索引。沒(méi)有優(yōu)化的字段es默認(rèn)是禁止聚合/排序操作的。但是category分詞之后的keyword是有索引的,所以可以對(duì)category.keyword進(jìn)行聚合。
正確寫(xiě)法
{"aggs":{//聚合操作"category_group":{//名稱,隨意起名"terms":{//分組"field":"category.keyword"//分組字段}}} }不建議的另外一種方式(添加映射,設(shè)置fielddata=ture,取消翻轉(zhuǎn)索引,占用大量?jī)?nèi)存)
http://192.168.0.104:9200/shopping/_mapping {"properties": {"category":{"type": "text","fielddata": true}} }七、映射
7.1 創(chuàng)建user映射(PUT)
http://192.168.0.104:9200/user/_mapping{"properties": {"name":{"type": "text","index": true},"sex":{"type": "keyword","index": true},"tel":{"type": "keyword","index": false}} }7.2 查看映射(GET)
http://192.168.0.104:9200/user/_mapping7.3 增加數(shù)據(jù)(doc、PUT)
http://192.168.0.104:9200/user/_create/1001 {"name":"小蘇","sex":"男的","tel":"1111" }7.4 查找name里面含有“小”的數(shù)據(jù)
http://192.168.0.104:9200/user/_search {"query":{"match":{"name":"小"}} }7.5 查找sex含有“男”的數(shù)據(jù)
http://192.168.0.104:9200/user/_search {"query":{"match":{"sex":"男"}} }返回結(jié)果為空,因?yàn)閯?chuàng)建映射時(shí)sex的類型為keyword。只有全匹配的時(shí)候才能查找出來(lái)。
http://192.168.0.104:9200/user/_search {"query":{"match":{"sex":"男的"}} }7.6 查找tel為1111的數(shù)據(jù)
http://192.168.0.104:9200/user/_search {"query":{"match":{"tel":"1111"}} }報(bào)錯(cuò)
Cannot search on field [tel] since it is not indexed.原因是建立映射的時(shí)候,把tel字段把i的index設(shè)置為不可以通過(guò)這個(gè)字段進(jìn)行搜索,重新建立索引。
es的一些基本入門操作如上,測(cè)試工具用的是postman。經(jīng)過(guò)一些基本操作之后,才會(huì)加深對(duì)概念的理解。
總結(jié)
以上是生活随笔為你收集整理的elasticsearch7.8.0入门操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 获取qq群成员信息_用Py
- 下一篇: android模拟器无法上局域网,模拟器