Kibana使用


  一、什么是Kibana  

    Kibana 是一個開源的分析和可視化平台,Kibana 提供搜索、查看和與存儲在 Elasticsearch 索引中的數據進行交互的功能。開發者或運維人員可以輕松地執行高級數據分析,並在各種圖表、表格和地圖中可視化數據

 

 

  二、安裝使用

    ①:下載Kibana https://www.elastic.co/cn/downloads/kibana

    ②:配置Kibana

      Open config/kibana.yml in an editor.

      Set elasticsearch.hosts to point at your Elasticsearch instance.

        默認情況下,Kibana 會連接運行在 localhost 上的 Elasticsearch 實例。如果需要連接不同的 Elasticsearch實例,可以修改 kibana.yml 配置文件中的 Elasticsearch URL 配置項並重啟 Kibana

        如:elasticsearch.hosts: ["http://localhost:9200"]

    ③:運行

      Run bin/kibana (or bin\kibana.bat on Windows)

    ④:訪問

      Point your browser at http://localhost:5601

        

 

    Kibana使用:

      ①:導入數據文件

      

      ②:預覽並確認導入

             

      ③:創建索引模式

      

      ④:在Discover中查看數據

      

 

     

      Kibana左側的Toolbar主要分為一下幾塊功能:

        Discovery 發現:用於查看和搜索原始數據
        Visualize 可視化:用來創建圖表、表格和地圖等
        Dashboard:多個圖表和合並為一個 Dashboard 儀表盤
        Timelion 時間線:用於分析時序數據,以二維圖形的方式展示
        Dev Tools 開發工具:用於進行DSL查詢、Query性能分析等
        Management 管理:主要用於創建 Index Patterns,ES中的索引在創建 Index Patterns 之后,才能在 Discover 中被搜索,在 Visualize 和 Dashboard 中制圖。

 

 

 

  三、檢索

     1、選擇日期

    

     2、左側欄目展示可用的字段列表:

    

 

  KQL(Kibana Query Language):

    1、根據具體字段檢索

       ①:如果只想展示某個字段的內容,則在字段欄目上將鼠標懸停在類別字段上,然后單擊 +          

       

       ②:根據字段內容檢索

        如根據category字段 

      

 

         多個字段一起檢索

      

 

         如檢索價格字段>=60 且 category為 Women's Clothing 的數據:products.taxless_price >= 60 and category : Women's Clothing

 

      ③:通過filter:

        

         選擇過濾的字段,和值的包含關系:

                

         填入值,保存即可檢索:

        

 

    2、檢索字符串

      ①:匹配多個字符串,每個字段都會單獨匹配。如:force and clean

      ②:匹配單個確切的字符串或者匹配字符串短語,用雙引號括起來。如"force and clean"

 

 

   Luceue:

     1、根據字段查詢

      限定字段全文搜索:field:value

      精確搜索:關鍵字加上雙引號 filed:"value"

    2、通配符

      ? 匹配單個字符

      * 匹配0到多個字符

    3、模糊搜索

      ~:在一個單詞后面加上~啟用模糊搜索,可以搜到一些拼寫錯誤的單詞

    4、近似搜索

      在短語后面加上~,可以搜到被隔開或順序不同的單詞      

      "where select"~5 表示 select 和 where 中間可以隔着5個單詞,可以搜到 select password from users where id=1

    5、范圍搜索

      mod_date:[20020101 TO 20030101]:查找 mod_date 字段的值介於 20020101 和 20030101 之間的文檔

      

   

  Dev Tools:

    Kinaba > Management > Dev Tools

    PUT(修改),POST(添加),DELETE(刪除),GET(查詢)

    1、GET /   等價於 http://localhost:9200/,對應的curl為:curl -XGET "http://localhost:9200/"  

    2、創建一個索引及文檔

      PUT index_name/_doc(type_name)/document_id

      {文檔內容}       

    3、簡單檢索文檔

     ①:GET /index_name/type_name/document_id 

      對應的curl:curl -X GET "localhost:9200/megacorp/employee/1?pretty"

      如 GET /megacorp/employee/1  的返回,_source屬性里的是原始JSON文檔

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [
      "sports",
      "music"
    ]
  }
}

     ②:搜索索引下的全部文檔:

      GET /megacorp/employee/_search

      curl -X GET "localhost:9200/megacorp/employee/_search?pretty"

      搜索結果放在了hit數組中,一個搜索默認返回10條結果

#! [types removal] Specifying types in search requests is deprecated.
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        }
      }
    ]
  }
}
View Code

 

     ③:根據文檔中的屬性值搜索

      搜索lastname屬性值為Smith的文檔,使用q參數:

      GET /megacorp/employee/_search?q=last_name:Smith

      curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"

 

    4、查詢表達式搜索

      查詢表達式支持構建更加復雜和健壯的查詢

      ①:使用 match 查詢屬性last_name值為Smith的文檔

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

      對應curl為:

curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
'

 

      ②:使用過濾器filter,搜索last_name屬性值為Smith、age屬性值大於30的文檔

GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

 

    5、全文搜索

     Elasticsearch會在全文屬性上搜索並返回相關性最強的結果,區別於傳統關系數據庫的一條記錄要么匹配要么不匹配

     如在`about` 屬性上搜索 “rock climbing” 

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

     返回結果:

{
  "took" : 67,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.4167401,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.4167401,     # 相關性得分
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 0.4589591,     # 相關性得分
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
    ]
  }
}

      Elasticsearch 默認按照相關性得分排序,即每個文檔跟查詢的匹配程度。第一個最高得分的結果很明顯:John Smith 的 about 屬性清楚地寫着 “rock climbing”   

      但為什么 Jane Smith 也作為結果返回了呢?原因是她的 about 屬性里提到了 “rock” 。因為只有 “rock” 而沒有 “climbing” ,所以她的相關性得分低於 John 的

 

    6、短語搜索

      找出一個屬性中的獨立單詞是沒有問題的,但有時候想要精確匹配一系列單詞或者_短語_ 。 比如, 我們想執行這樣一個查詢,僅匹配同時包含 “rock” 和 “climbing” ,並且 二者以短語 “rock climbing” 的形式緊挨着文檔

      為此對 match 查詢稍作調整,使用一個叫做 match_phrase 的查詢: 

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

 

    7、高亮搜索

      在搜索結果中高亮顯示部分文本片段,以便讓用戶知道為何該文檔符合查詢條件

      使用highlight參數即可:

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

 

    8、聚合搜索

      Elasticsearch 有一個功能叫聚合(aggregations),允許我們基於數據生成一些精細的分析結果。聚合與 SQL 中的 GROUP BY 類似但更強大。

      聚合使用aggs,如聚合搜索文檔中所有age的值:

GET /megacorp/employee/_search
{
  "aggs": {
    "all_ages": {
      "terms": { "field": "age" }
    }
  }
}

      結果:

{
  ...
  "aggregations" : {
    "all_ages" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ # age 所有出現的值,及出現改之文檔的個數
        {
          "key" : 25,   
          "doc_count" : 1
        },
        {
          "key" : 32,
          "doc_count" : 1
        },
        {
          "key" : 35,
          "doc_count" : 1
        }
      ]
    }
  }
}  

    Elasticsearch搜索語法中文文檔:https://www.elastic.co/guide/cn/elasticsearch/guide/2.x/_retrieving_a_document.html

  

    9、清空索引數據

POST index_name/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

    10、根據id刪除一條數據

DELETE index_name/_doc/${_id}

 

    更多API:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html 

     

 

END.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM