日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kibana7.x操作

發(fā)布時間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kibana7.x操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

elasticsearch7.x取消了type(類型的概念)對應數(shù)據(jù)庫表的概念

一、添加一個索引

PUT 索引名 { "settings": { "number_of_shards": 1, "number_of_replicas": 0 } }

二、創(chuàng)建映射字段

analyzer:分詞器 下載地址:https://github.com/medcl/elasticsearch-analysis-ik

PUT /索引名/_mapping { "properties": { "title":{ "type": "text", "analyzer": "ik_max_word" }, "images":{ "type": "keyword", "index": false }, "price":{ "type": "float" } } }

三、查看映射關系

GET /索引名/_mapping

四、新增數(shù)據(jù)

  • 隨機生成id
  • POST /索引庫名/_doc { "title":"大米手機", "images":"http://image.leyou.com/12479122.jpg", "price":2899.00 }
  • 自定義id
  • 自定義id值不能重復,否則數(shù)據(jù)將會被覆蓋

    POST /索引庫名/_doc/自定義id值 {"title":"超米手機","images":"http://image.leyou.com/12479122.jpg","price":3699.00,"Saleable":true }

    五、修改數(shù)據(jù)

    PUT /索引庫/_doc/id值 { "title":"超大米手機", "images":"http://image.leyou.com/12479122.jpg", "price":3899.00, "stock": 100, "saleable":true }

    六、刪除數(shù)據(jù)

    DELETE /索引庫名/_doc/id值

    七、查詢

  • 查詢所有
  • GET /索引庫名/_search { "query": { "match_all": {} } }
    • 響應內容:
    {"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 6,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "goods","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"title" : "小米手機","images" : "http://image.leyou.com/12479122.jpg","price" : 2699.0,"Saleable" : true}},{"_index" : "goods","_type" : "_doc","_id" : "mmHtSnEBVcsVh4Caiarl","_score" : 1.0,"_source" : {"title" : "大米手機","images" : "http://image.leyou.com/12479122.jpg","price" : 2899.0}},{"_index" : "goods","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"title" : "超米手機","images" : "http://image.leyou.com/12479122.jpg","price" : 3699.0,"Saleable" : true}},{"_index" : "goods","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"title" : "小米電視4A","images" : "http://image.leyou.com/12479122.jpg","price" : 4699.0,"Saleable" : true}},{"_index" : "goods","_type" : "_doc","_id" : "4","_score" : 1.0,"_source" : {"title" : "華為手機","subTitle" : "小米","images" : "http://image.leyou.com/12479122.jpg","price" : 4699.0}},{"_index" : "goods","_type" : "_doc","_id" : "5","_score" : 1.0,"_source" : {"title" : "oppo","subTitle" : "小米","images" : "http://image.leyou.com/12479122.jpg","price" : 4899.0}}]} }
    • 字段解析:
    • took:查詢花費時間,單位是毫秒
    • time_out:是否超時
    • _shards:分片信息
    • hits:搜索結果總覽對象
      • total:搜索到的總條數(shù)
      • max_score:所有結果中文檔得分的最高分
      • hits:搜索結果的文檔對象數(shù)組,每個元素是一條搜索到的文檔信息
        • _index:索引庫
        • _type:文檔類型
        • _id:文檔id
        • _score:文檔得分
        • _source:文檔的源數(shù)據(jù)
  • 匹配查詢
  • GET /索引庫名/_search{ "query": { "match": { "title": { "query": "小米手機電視","minimum_should_match": "60%" } } }}
  • 多字段查詢
    • title,subTitle字段名
    GET /索引庫名/_search { "query": { "multi_match": { "query": "小米", "fields":["title","subTitle"] } }

    }

    詞條查詢:可分割的最小詞條單位 title為字段名 [ “字段值” ]

    GET /索引庫名/_search {"query": {"terms": {"title": ["小米","手機"]}} }

    多詞條查詢

    GET /索引庫名/_search { "query": { "terms": { "title": ["小米","手機"] } } }
  • 結果過濾
    excludes:不顯示的字段 includes: 顯示的字段
  • GET /索引庫名/_search { "_source": { "excludes": "{images}" }, "query": { "terms": { "title": ["小米","手機"] } } }
  • 布爾查詢
  • 標題一定有小米,或者價格為2699,4699
    bool把各種其它查詢通過must(與)、must_not(非)、should(或)的方式進行組合

    GET /索引庫名/_search { "query": { "bool": { "must": [ {"match": { "title": "小米" } } ], "should": [ {"terms": { "price": [ "2699", "2799" ] }} ] } } }
  • 范圍查詢
  • 價格大于等于2799 小于等于3899

    GET /索引庫名/_search { "query": { "range": { "price": { "gte": 2799, "lte": 3899 } } } }
  • 模糊查詢
  • 標題為oppo 默認允許錯誤一個字母,最大為兩個字母 正確標題 oppo

    fuzziness:配置篇里

    GET /索引庫名/_search { "query": { "fuzzy": { "title": { "value": "oope", "fuzziness": 2 } } } }
  • 過濾filter
    不會影響查詢的分數(shù)_score
  • GET /索引庫名/_search { "query": { "bool": { "must": [ { "match": { "title": "小米" } } ], "filter": [ { "range": { "price": { "gte": 2699, "lte": 4999 } } } ] } } }
  • 查詢全部數(shù)據(jù),如果數(shù)據(jù)很多,kibana默認只返回前10條數(shù)據(jù)
  • 如果想一開始就設置的話,參考這篇文章
    不然就在查詢的時候,帶上from和size這兩個參數(shù)

    ## 查詢所有 GET /poem/_search {"from" : 0, "size" : 50,"query": {"match_all": {}} }

    參考文章

    八、排序

    GET /索引庫名/_search { "query": { "bool": { "filter": [ { "range": { "price": { "gte": 2699, "lte": 4999 } } } ] } }, "sort": [ { "price": { "order": "desc" } }, { "_id":{ "order": "asc" } } ] }

    九、聚合 aggregations

    聚合可以讓我們極其方便的實現(xiàn)對數(shù)據(jù)的統(tǒng)計、分析。例如:

    • 什么品牌的手機最受歡迎?

    • 這些手機的平均價格、最高價格、最低價格?

    • 這些手機每月的銷售情況如何?

    實現(xiàn)這些統(tǒng)計功能的比數(shù)據(jù)庫的sql要方便的多,而且查詢速度非常快,可以實現(xiàn)實時搜索效果。

  • 基本概念
  • Elasticsearch中的聚合,包含多種類型,最常用的兩種,一個叫桶,一個叫度量:

    • 桶(bucket)

    桶的作用,是按照某種方式對數(shù)據(jù)進行分組,每一組數(shù)據(jù)在ES中稱為一個桶,例如我們根據(jù)國籍對人劃分,可以得到中國桶、英國桶,日本桶……或者我們按照年齡段對人進行劃分:010,1020,2030,3040等。

    Elasticsearch中提供的劃分桶的方式有很多:

    • Date Histogram Aggregation:根據(jù)日期階梯分組,例如給定階梯為周,會自動每周分為一組

    • Histogram Aggregation:根據(jù)數(shù)值階梯分組,與日期類似

    • Terms Aggregation:根據(jù)詞條內容分組,詞條內容完全匹配的為一組

    • Range Aggregation:數(shù)值和日期的范圍分組,指定開始和結束,然后按段分組

    • ……

    bucket aggregations 只負責對數(shù)據(jù)進行分組,并不進行計算,因此往往bucket中往往會嵌套另一種聚合:metrics aggregations即度量

    • 度量(metrics)

    分組完成以后,我們一般會對組中的數(shù)據(jù)進行聚合運算,例如求平均值、最大、最小、求和等,這些在ES中稱為度量

    比較常用的一些度量聚合方式:

    • Avg Aggregation:求平均值

    • Max Aggregation:求最大值

    • Min Aggregation:求最小值

    • Percentiles Aggregation:求百分比

    • Stats Aggregation:同時返回avg、max、min、sum、count等

    • Sum Aggregation:求和

    • Top hits Aggregation:求前幾

    • Value Count Aggregation:求總數(shù)

    • ……

    • 使用聚合先加入新的索引
    PUT /cars { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties": { "color": { "type": "keyword" }, "make": { "type": "keyword" } } } }
    • 批量添加數(shù)據(jù)
    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" }
    • 聚合為桶
    GET /cars/_search { "aggs": { "color": { "terms": { "field": "color" } } } }
    • 桶內度量
    GET /cars/_search { "size": 0, "aggs": { "color": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
    • 桶內嵌套桶
    GET /cars/_search { "size": 0, "aggs": { "color": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "mark":{ "terms": { "field": "make" } } } } } }
    • 階梯分組
      對價格進行階梯分組,最小數(shù)量為1才顯示
    GET /cars/_search { "size": 0, "aggs": { "price_histogram": { "histogram": { "field": "price", "interval": 5000, "min_doc_count": 1 } } } }
    • 范圍分組
    GET /cars/_search { "size": 0, "aggs": { "price_range": { "range": { "field": "price", "ranges": [ { "from": 5000, "to": 15000 }, { "from": 15000, "to": 20000 }, { "from": 20000, "to": 25000 }, { "from": 25000, "to":35000 }, { "from": 35000, "to":40000 } ] } } } }

    文章轉自

    總結

    以上是生活随笔為你收集整理的kibana7.x操作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。