一、安裝
1.拉取鏡像
docker pull kibana:7.4.2
2.運行容器
docker run -di --name kibana -e ELASTICSEARCH_URL=http://128.1.49.131:9200 -p 5601:5601 kibana:7.4.2
3.開9200、9300、5601端口
二、操作es(左側導航欄dev tools)
1.操作索引
● 創建索引(包括創建映射)
PUT /viuman { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties": { "images": { "type": "keyword", "index": false }, "price": { "type": "float" }, "saleable": { "type": "boolean" }, "stock": { "type": "long" }, "title": { "type": "text", "analyzer": "ik_max_word" } } } }
● 查看所有索引
GET *
● 查看索引信息
GET viuman
● 查看映射
GET viuman/_mapping
● 刪除索引
DELETE viuman
● 導入數據
POST /cars/_bulk { "index": {}} { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" } { "index": {}} { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } { "index": {}} { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" } { "index": {}} { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" } { "index": {}} { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" } { "index": {}} { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } { "index": {}} { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" } { "index": {}} { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
2.操作文檔
● 添加文檔(隨機生成id)
POST /viuman/_doc/ { "title":"小米手機", "images":"http://image.leyou.com/12479122.jpg", "price":2699.00 }
● 添加/修改文檔
PUT /viuman/_doc/3 { "title":"超米手機", "images":"http://image.leyou.com/12479122.jpg", "price":2899.00, "stock": 200, "saleable":true }
● 刪除文檔
DELETE /viuman/_doc/2
3.基本搜索
● 查所有文檔
GET _search
● or搜索
GET /viuman/_search { "query":{ "match":{ "title":"小米電視" } } }
● and搜索
GET /viuman/_search { "query":{ "match": { "title": { "query": "小米電視", "operator": "and" } } } }
● 按匹配到總分詞數量的百分比搜索
GET /viuman/_search { "query":{ "match":{ "title":{ "query":"小米曲面電視", "minimum_should_match": "75%" } } } }
● 多字段搜索
GET /viuman/_search { "query":{ "multi_match": { "query": "小米", "fields": [ "title", "subTitle" ] } } }
● 精確匹配
GET /viuman/_search { "query":{ "term":{ "price":2699.00 } } }
● 多詞條精確匹配
GET /viuman/_search { "query":{ "terms":{ "price":[2699.00,2899.00,3899.00] } } }
● 只返回指定字段
GET /viuman/_search { "_source": ["title","price"], "query": { "term": { "price": 2699 } } }
● 不返回指定字段
GET /viuman/_search { "_source": { "excludes": ["images"] }, "query": { "term": { "price": 2699 } } }
4.高級搜索
● must
(與)、must_not
(非)、should
(或)布爾查詢
GET /viuman/_search { "query":{ "bool":{ "must": { "match": { "title": "大米" }}, "must_not": { "match": { "title": "電視" }}, "should": { "match": { "title": "手機" }} } } }
● 范圍查詢(gt大於 gte大於等於 lt小於 lte小於等於)
GET /viuman/_search { "query":{ "range": { "price": { "gte": 1000.0, "lt": 2800.00 } } } }
● 模糊查詢(默認最大編輯距離2)
GET /viuman/_search { "query": { "fuzzy": { "title": "appla" } } }
● 模糊查詢(指定編輯距離)
GET /viuman/_search { "query": { "fuzzy": { "title": { "value":"appla", "fuzziness":1 } } } }
● 條件查詢中進行過濾
GET /viuman/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手機" }}, "filter":{ "range":{"price":{"gt":2000.00,"lt":3800.00}} } } } }
● 無查詢條件直接過濾
GET /viuman/_search { "query":{ "constant_score": { "filter": { "range":{"price":{"gt":2000.00,"lt":3000.00}} } } } }
● 單字段排序
GET /viuman/_search { "query": { "match": { "title": "小米手機" } }, "sort": [ { "price": { "order": "desc" } } ] }
● 多字段排序(首先按照價格排序,然后按照相關性得分排序)
GET /viuman/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手機" }}, "filter":{ "range":{"price":{"gt":2000,"lt":300000}} } } }, "sort": [ { "price": { "order": "desc" }}, { "_score": { "order": "desc" }} ] }
5.聚合
1)划分桶(查看各桶的數量)
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" } } } }
- size: 查詢條數,這里設置為0,因為我們不關心搜索到的數據,只關心聚合結果,提高效率
- aggs:聲明這是一個聚合查詢,是aggregations的縮寫
- popular_colors:給這次聚合起一個名字,任意。
- terms:划分桶的方式,這里是根據詞條划分
- field:划分桶的字段
- terms:划分桶的方式,這里是根據詞條划分
- popular_colors:給這次聚合起一個名字,任意。
2)桶內度量
● 求平均值
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } } } } } }
- aggs:我們在上一個aggs(popular_colors)中添加新的aggs。可見
度量
也是一個聚合,度量是在桶內的聚合 - avg_price:聚合的名稱
- avg:度量的類型,這里是求平均值
- field:度量運算的字段
● 桶內嵌套桶
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } }, "maker":{ "terms":{ "field":"make" } } } } } }
- 原來的color桶和avg計算我們不變
- maker:在嵌套的aggs下新添一個桶,叫做maker
- terms:桶的划分類型依然是詞條
- filed:這里根據make字段進行划分
新的聚合maker
被嵌套在原來每一個color
的桶中,每個顏色下面都根據 make
字段進行了分組
3)階梯分桶Histogram
GET /cars/_search { "size":0, "aggs":{ "price":{ "histogram": { "field": "price", "interval": 5000, "min_doc_count": 1 } } } }
- interval:階梯間隔
- min_doc_count:約束最少文檔數量為1,過濾文檔數量為0的桶
附:es常用類型
-
String類型,又分兩種:
- text:可分詞,不可參與聚合
- keyword:不可分詞,數據會作為完整字段進行匹配,可以參與聚合
-
Numerical:數值類型,分兩類
- 基本數據類型:long、interger、short、byte、double、float、half_float
- 浮點數的高精度類型:scaled_float。需要指定一個精度因子,比如10或100。elasticsearch會把真實值乘以這個因子后存儲,取出時再還原。