kibana Dev Tools語句查詢簡單使用入門

使用kibana7.0.0的控制檯Dev Tools操作ES數據的基本語法入門示例

因爲使用的是本地啓動的ES庫,所以需要先啓動ES,然後啓動kibana,直接從官網上下載安裝啓動即可,說明一點就是需先啓動ES,在啓動kibana,該部分效果以及添加官方示例數據已在之前一篇文章中寫過,此處不再重複。

直接點擊Dev Tools,來看基本操作

1,輸入:GET /

在右側將看到和啓動完ES後在瀏覽器輸入localhost:9200相同的內容

2,創建索引

輸入:

說明:因爲7版本之後,ES不再支持一個索引(index)可以創建多個類型(type),所以cmcc/後邊不再需要寫入類型名稱,而是統一使用_create代替即可,同樣的,查詢操作使用_doc代替即可,右側看到如下圖所示類似形式表示創建成功

3,查看剛纔創建的索引

輸入:GET cmcc/_doc/1

右側將顯示剛纔創建的內容,其中_index是剛纔創建的索引名稱;_type是類型,7版本統一爲_doc;_id爲創建時的ID,如果創建索引的時候不設置ID,那麼ES將默認分配一個ID,不過樣式會比較長,不好記憶;_version爲版本號,如果我們之後對該數據進行了修改,那麼他會隨之變化;_source裏邊就是我們剛纔加進去的數據內容

4,刪除索引

輸入:DELETE cmcc

只需要在DELETE後邊加上索引名稱即可

5,修改數據

輸入:

這裏我們修改了"name"值,把"province"和"conutry"值改爲中文,並添加了一個新屬性"xingbie",執行之後我們再次執行獲取數據內容命令GET cmcc/_doc/1,如下,可以看到數據已經被修改,版本號變成了2

6,bulk方法批量插入數據

輸入:

使用POST方法,然後每一條數據的格式是一致的,首先第一行輸入 {"index":{"_index":"cmcc"}} ,也就是索引名稱,第二行輸入要插入的完整數據,這裏特別提醒下,插入的這條數據不能使用剛纔創建數據時的那種多行形式,只能使用沒有回車的一條數據,否則會報錯如下:

{
  "error": {
    "root_cause": [
      {
        "type": "json_e_o_f_exception",
        "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@154857fc; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@154857fc; line: 1, column: 3]"
      }
    ],
    "type": "json_e_o_f_exception",
    "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@154857fc; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@154857fc; line: 1, column: 3]"
  },
  "status": 500
}

執行完畢後,我們再次獲取數據看一下,輸入:GET cmcc/_search

結果如下:(不截長圖了,就直接貼結果吧>_<)

{
  "took" : 374,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "cmcc",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "dunkking",
          "age" : 27,
          "location" : "SG",
          "province" : "河北",
          "country" : "中國",
          "xingbie" : "mela"
        }
      },
      {
        "_index" : "cmcc",
        "_type" : "_doc",
        "_id" : "9vD-3moBmjOHTfOJtVLL",
        "_score" : 1.0,
        "_source" : {
          "name" : "points",
          "age" : 23,
          "location" : "PG",
          "province" : "江蘇",
          "country" : "中國",
          "xingbie" : "mela"
        }
      },
      {
        "_index" : "cmcc",
        "_type" : "_doc",
        "_id" : "9_D-3moBmjOHTfOJtVLL",
        "_score" : 1.0,
        "_source" : {
          "name" : "rebound",
          "age" : 24,
          "location" : "SF",
          "province" : "廣州",
          "country" : "中國",
          "xingbie" : "mela"
        }
      },
      {
        "_index" : "cmcc",
        "_type" : "_doc",
        "_id" : "-PD-3moBmjOHTfOJtVLL",
        "_score" : 1.0,
        "_source" : {
          "name" : "center",
          "age" : 23,
          "location" : "C",
          "province" : "北京",
          "country" : "中國",
          "xingbie" : "femela"
        }
      },
      {
        "_index" : "cmcc",
        "_type" : "_doc",
        "_id" : "-fD-3moBmjOHTfOJtVLL",
        "_score" : 1.0,
        "_source" : {
          "name" : "assist",
          "age" : 21,
          "location" : "PF",
          "province" : "廣州",
          "country" : "中國",
          "xingbie" : "famela"
        }
      }
    ]
  }
}

7,按照條件查詢

輸入:

也就是查詢數據中屬性"province"爲"廣州"的數據,結果如下:

{

  "took" : 10,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 2,

      "relation" : "eq"

    },

    "max_score" : 1.7509375,

    "hits" : [

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9_D-3moBmjOHTfOJtVLL",

        "_score" : 1.7509375,

        "_source" : {

          "name" : "rebound",

          "age" : 24,

          "location" : "SF",

          "province" : "廣州",

          "country" : "中國",

          "xingbie" : "mela"

        }

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-fD-3moBmjOHTfOJtVLL",

        "_score" : 1.7509375,

        "_source" : {

          "name" : "assist",

          "age" : 21,

          "location" : "PF",

          "province" : "廣州",

          "country" : "中國",

          "xingbie" : "famela"

        }

      }

    ]

  }

}

8,當同一個屬性滿足邏輯或時的查詢

輸入:

這裏是查詢屬性"age"等於21或者23的數據,如果看着不舒服,我們可以點擊運行按鈕右側的扳手,選擇Auto indent,輸入效果就會直觀一些,

其中,116行固定輸入"query",117行固定輸入"bool",118行輸入爲"should",表示是邏輯或的關係,120行爲"match",121行爲所要查詢的屬性名與屬性值

執行結果如下

{

  "took" : 0,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 3,

      "relation" : "eq"

    },

    "max_score" : 1.0,

    "hits" : [

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9vD-3moBmjOHTfOJtVLL",

        "_score" : 1.0,

        "_source" : {

          "name" : "points",

          "age" : 23,

          "location" : "PG",

          "province" : "江蘇",

          "country" : "中國",

          "xingbie" : "mela"

        }

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-PD-3moBmjOHTfOJtVLL",

        "_score" : 1.0,

        "_source" : {

          "name" : "center",

          "age" : 23,

          "location" : "C",

          "province" : "北京",

          "country" : "中國",

          "xingbie" : "femela"

        }

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-fD-3moBmjOHTfOJtVLL",

        "_score" : 1.0,

        "_source" : {

          "name" : "assist",

          "age" : 21,

          "location" : "PF",

          "province" : "廣州",

          "country" : "中國",

          "xingbie" : "famela"

        }

      }

    ]

  }

}

9,多條件查詢

輸入:

這裏是查詢屬性"age"等於23,並且屬性"country"爲“中國”的數據,這裏和上一條查詢的關鍵區別就在於第98行由"should"改爲"must",執行結果如下:

{

  "took" : 1,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 2,

      "relation" : "eq"

    },

    "max_score" : 1.1740228,

    "hits" : [

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9vD-3moBmjOHTfOJtVLL",

        "_score" : 1.1740228,

        "_source" : {

          "name" : "points",

          "age" : 23,

          "location" : "PG",

          "province" : "江蘇",

          "country" : "中國",

          "xingbie" : "mela"

        }

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-PD-3moBmjOHTfOJtVLL",

        "_score" : 1.1740228,

        "_source" : {

          "name" : "center",

          "age" : 23,

          "location" : "C",

          "province" : "北京",

          "country" : "中國",

          "xingbie" : "femela"

        }

      }

    ]

  }

}

10,範圍查詢並進行排序

輸入:

這裏,151行使用"range",152行輸入屬性名,153行"gte"和154行"lte"表示查詢屬性"age"在20-25範圍的數據,然後158行表示排序,160行表示排序的屬性是"age",161“order”表示排序爲倒序"desc",執行結果如下:

{

  "took" : 0,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 4,

      "relation" : "eq"

    },

    "max_score" : null,

    "hits" : [

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9_D-3moBmjOHTfOJtVLL",

        "_score" : null,

        "_source" : {

          "name" : "rebound",

          "age" : 24,

          "location" : "SF",

          "province" : "廣州",

          "country" : "中國",

          "xingbie" : "mela"

        },

        "sort" : [

          24

        ]

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9vD-3moBmjOHTfOJtVLL",

        "_score" : null,

        "_source" : {

          "name" : "points",

          "age" : 23,

          "location" : "PG",

          "province" : "江蘇",

          "country" : "中國",

          "xingbie" : "mela"

        },

        "sort" : [

          23

        ]

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-PD-3moBmjOHTfOJtVLL",

        "_score" : null,

        "_source" : {

          "name" : "center",

          "age" : 23,

          "location" : "C",

          "province" : "北京",

          "country" : "中國",

          "xingbie" : "femela"

        },

        "sort" : [

          23

        ]

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "-fD-3moBmjOHTfOJtVLL",

        "_score" : null,

        "_source" : {

          "name" : "assist",

          "age" : 21,

          "location" : "PF",

          "province" : "廣州",

          "country" : "中國",

          "xingbie" : "famela"

        },

        "sort" : [

          21

        ]

      }

    ]

  }

}

11,聚合查詢

輸入:

使用聚合查詢,格式是:170行使用"aggs",171行爲所要查詢的屬性名,這裏查詢"age",173行"field"後邊輸入屬性名,174行爲範圍,分別在"from"和"to"後邊輸入要分段的範圍,這條請求實現的是統計屬性"age"按照20-23,23-25,25-30劃分的數據條數分別爲多少,如果想要查看滿足條件的數據,則將169行"size"值置爲非零數,貌似應大於查詢條數,具體還沒查,這裏是不顯示滿足條件的具體數據,直接置零即可,執行結果如下:

{

  "took" : 9,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 5,

      "relation" : "eq"

    },

    "max_score" : null,

    "hits" : [ ]

  },

  "aggregations" : {

    "age" : {

      "buckets" : [

        {

          "key" : "20.0-23.0",

          "from" : 20.0,

          "to" : 23.0,

          "doc_count" : 1

        },

        {

          "key" : "23.0-25.0",

          "from" : 23.0,

          "to" : 25.0,

          "doc_count" : 3

        },

        {

          "key" : "25.0-30.0",

          "from" : 25.0,

          "to" : 30.0,

          "doc_count" : 1

        }

      ]

    }

  }

}

聚合查詢的另外一個示例

輸入:

這條請求是查詢屬性"province"的統計結果,這裏是統計5條數據,並顯示其中2條,並在197行"field"後輸入屬性名,並在其後添加  .keyword,查詢結果如下

{

  "took" : 0,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 5,

      "relation" : "eq"

    },

    "max_score" : 1.0,

    "hits" : [

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "1",

        "_score" : 1.0,

        "_source" : {

          "name" : "dunkking",

          "age" : 27,

          "location" : "SG",

          "province" : "河北",

          "country" : "中國",

          "xingbie" : "mela"

        }

      },

      {

        "_index" : "cmcc",

        "_type" : "_doc",

        "_id" : "9vD-3moBmjOHTfOJtVLL",

        "_score" : 1.0,

        "_source" : {

          "name" : "points",

          "age" : 23,

          "location" : "PG",

          "province" : "江蘇",

          "country" : "中國",

          "xingbie" : "mela"

        }

      }

    ]

  },

  "aggregations" : {

    "province" : {

      "doc_count_error_upper_bound" : 0,

      "sum_other_doc_count" : 0,

      "buckets" : [

        {

          "key" : "廣州",

          "doc_count" : 2

        },

        {

          "key" : "北京",

          "doc_count" : 1

        },

        {

          "key" : "江蘇",

          "doc_count" : 1

        },

        {

          "key" : "河北",

          "doc_count" : 1

        }

      ]

    }

  }

}

暫時寫這麼多,剛開始學,很多不熟悉的,後續有時間慢慢補充

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章