elasticsearch7.8.0入门操作
一、基本概念
Cluster:集群, 由一組 es 實例組成
Node:節點, 單個 es 運行的實例
Index:索引, 類似于數據表
Type 類型ES7以后固定值是 _doc
Document: 文檔, 類似于數據表中的一條數據
Field: 字段, 類似于數據表的字段
Mapping:映射, 類似于數據表的字段定義, 包括數據類型
二、索引-增刪改查
2.1 創建索引,PUT請求,不能重復添加
http://192.168.0.104:9200/shopping2.2 全查索引,GET請求
http://192.168.0.104:9200/_cat/indices?v2.3 單個索引查詢,GET請求
http://192.168.0.104:9200/shopping2.4 刪除索引,DELETE
http://192.168.0.104:9200/shopping三、文檔-增刪改查
3.1 創建文檔,POST請求
// 隨機主鍵id http://192.168.0.104:9200/shopping/_doc/// 指定主鍵 http://192.168.0.104:9200/shopping/_doc/13.2 查詢文檔,GET請求
// 主鍵查詢 http://192.168.0.104:9200/shopping/_doc/1// 全查詢 http://192.168.0.104:9200/shopping/_doc/_search3.3 修改文檔,POST請求
http://192.168.0.104:9200/shopping/_doc/1 // 全量修改的json參數 {"title": "蘋果13","category": "蘋果","price": 6999.00 }// 局部修改的json參數 http://192.168.0.104:9200/shopping/_update/1 {"doc": {"title": "蘋果13","price": 6998.00} }四、常見查詢
4.1?條件查詢,GET請求
http://192.168.0.104:9200/shopping/_search // 查詢category為蘋果的文檔,json參數 {"query":{"match":{"category":"蘋果"}} }// 全條件查詢 {"query":{"match_all":{}} }// 查詢某一個字段 {"query":{"match_all":{}},"_source":["title"] }// 分頁查詢 {"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 全文檢索,類似我們百度搜索一樣,輸入“蘋為”,可以查出蘋果和華為的數據。
{"query":{"match":{"category" : "蘋為"}} }5.2 完全匹配,只會篩選出蘋果的數據
{"query":{"match_phrase":{"category" : "蘋"}} }5.3 高亮查詢
{"query":{"match_phrase":{"category" : "蘋"}},"highlight":{"fields":{"category":{}//<----高亮這字段}} }返回結果中蘋字被高亮展示"<em>蘋</em>果"六、聚合查詢(類似數據庫的聚合查詢)
6.1 分組
按照price分組
{"aggs":{//聚合操作"price_group":{//名稱,隨意起名"terms":{//分組"field":"price"//分組字段}}} }// 不帶原始數據返回 {"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0 }但是如果按照category分組
{"aggs":{//聚合操作"category_group":{//名稱,隨意起名"terms":{//分組"field":"category"//分組字段}}} }則報錯
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這個字段在建立的時候沒有進行優化,默認沒有索引。沒有優化的字段es默認是禁止聚合/排序操作的。但是category分詞之后的keyword是有索引的,所以可以對category.keyword進行聚合。
正確寫法
{"aggs":{//聚合操作"category_group":{//名稱,隨意起名"terms":{//分組"field":"category.keyword"//分組字段}}} }不建議的另外一種方式(添加映射,設置fielddata=ture,取消翻轉索引,占用大量內存)
http://192.168.0.104:9200/shopping/_mapping {"properties": {"category":{"type": "text","fielddata": true}} }七、映射
7.1 創建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 增加數據(doc、PUT)
http://192.168.0.104:9200/user/_create/1001 {"name":"小蘇","sex":"男的","tel":"1111" }7.4 查找name里面含有“小”的數據
http://192.168.0.104:9200/user/_search {"query":{"match":{"name":"小"}} }7.5 查找sex含有“男”的數據
http://192.168.0.104:9200/user/_search {"query":{"match":{"sex":"男"}} }返回結果為空,因為創建映射時sex的類型為keyword。只有全匹配的時候才能查找出來。
http://192.168.0.104:9200/user/_search {"query":{"match":{"sex":"男的"}} }7.6 查找tel為1111的數據
http://192.168.0.104:9200/user/_search {"query":{"match":{"tel":"1111"}} }報錯
Cannot search on field [tel] since it is not indexed.原因是建立映射的時候,把tel字段把i的index設置為不可以通過這個字段進行搜索,重新建立索引。
es的一些基本入門操作如上,測試工具用的是postman。經過一些基本操作之后,才會加深對概念的理解。
總結
以上是生活随笔為你收集整理的elasticsearch7.8.0入门操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 获取qq群成员信息_用Py
- 下一篇: android模拟器无法上局域网,模拟器