Kibana の Top hits Aggregation の可視化、Timepicker を試してみた

2017.03.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、藤本です。

現地時間 3/28 に Elastic Stack 5.3.0 がリリースされました。

Elastic Stack 5.3.0 は先日の Elastic{ON}17 のロードマップでも発表があった新機能がいくつか含まれていて胸熱でした!新機能が多かったので気になった機能を何回かに分けて試してみます!

前回は Filebeat 5.3.0 の新機能である Filebeat modules を試してみました。

今回は Kibana 5.3.0 の新機能である Top Hits Aggregation の可視化、Timepicker を試してみました。

Top hits Aggregation の Kibana 対応

Elasticsearch は Aggregation という機能でインデキシングされているデータを様々な方法で集計できます。Aggregation の一つの機能に Top hits Aggregation があります。Top hits Aggregation は集計結果(Bucket Aggregation の結果)に対して、あるフィールドの上位◯件のデータを取り出すことができます。はい、日本語下手ですみません。ブログを例とします。インデックスでブログエントリのフィールドとして、タイトル、カテゴリ、公開日を持っているとします。全エントリを公開日順に取得することは簡単です。公開日でソートすれば取得できます。それをカテゴリ別で公開日順に取得するのはいかがでしょうか? Top hits Aggregation を利用すると一つのクエリで各カテゴリの最新◯件のエントリを取得することが可能です。

それでは早速試してみましょう。まずは Kibana による可視化の前に Elasticsearch のクエリで Top hits Aggregation を見てみます。ちょっと古いですが弊社のブログデータを利用します。

$ curl 10.255.0.100:9200/devio/_search -d '{
  "sort": [ { "post_date": { "order": "desc" } } ],
  "size": 1
}'
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5115,
    "max_score": null,
    "hits": [
      {
        "_index": "devio",
        "_type": "logs",
        "_id": "AVshkA27NTWwpd4B7Xhk",
        "_score": null,
        "_source": {
          "referencecat": [
            "aws-special",
            "aws-redshift"
          ],
          "post_title": "モニタリングツール Redshift Console を試してみた",
          "blog_id": 1,
          "dsq_thread_id": "",
          "category_name": [
            "AWS",
            "クラウド",
            "ツール"
          ],
          "post_format": [],
          "tags": [],
          "post_content": "(省略)"
          "@timestamp": "2017-03-31T07:57:45.355Z",
          "post_date": "2015-09-28T09:20:58+00:00",
          "referencecat_name": [
            "AWS特集",
            "Redshift"
          ],
          "series": [],
          "@version": "1",
          "post_type": "post",
          "category": [
            "aws",
            "cloud",
            "tool"
          ],
          "class": [],
          "post_tag": []
        },
        "sort": [
          1443432058000
        ]
      }
    ]
  }
}

5115 件のエントリが入っており、最新のデータは 2015/09/28 です。めっちゃ古いデータですみません。。

これをカテゴリ別に最新データを 3件づつ取得するクエリを投げてみます。

$ curl 10.255.0.100:9200/devio/_search -d '{
  "size": 0,
  "aggs": {
    "category_bucket": {
      "terms": {
        "field": "category.keyword"
      },
      "aggs": {
        "update": {
          "top_hits": {
            "size": 3,
            "sort": [{"post_date": {"order": "desc"}}],
            "_source": {
              "includes": [
                "post_title",
                "post_date"
              ]
            }
          }
        }
      }
    }
  }
}'
{
  "took": 138,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5115,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "category_bucket": {
      "doc_count_error_upper_bound": 103,
      "sum_other_doc_count": 4229,
      "buckets": [
        {
          "key": "cloud",
          "doc_count": 1878,
          "update": {
            "hits": {
              "total": 1878,
              "max_score": null,
              "hits": [
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xhk",
                  "_score": null,
                  "_source": {
                    "post_title": "モニタリングツール Redshift Console を試してみた",
                    "post_date": "2015-09-28T09:20:58+00:00"
                  },
                  "sort": [
                    1443432058000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7XiQ",
                  "_score": null,
                  "_source": {
                    "post_title": "AWS re:Invent 2015 in ラスベガスを256倍楽しむ方法",
                    "post_date": "2015-09-28T09:00:16+00:00"
                  },
                  "sort": [
                    1443430816000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xgw",
                  "_score": null,
                  "_source": {
                    "post_title": "Active Directory資産を活用したAWS API認証",
                    "post_date": "2015-09-26T22:10:25+00:00"
                  },
                  "sort": [
                    1443305425000
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "aws",
          "doc_count": 1826,
          "update": {
            "hits": {
              "total": 1826,
              "max_score": null,
              "hits": [
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xhk",
                  "_score": null,
                  "_source": {
                    "post_title": "モニタリングツール Redshift Console を試してみた",
                    "post_date": "2015-09-28T09:20:58+00:00"
                  },
                  "sort": [
                    1443432058000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7XiQ",
                  "_score": null,
                  "_source": {
                    "post_title": "AWS re:Invent 2015 in ラスベガスを256倍楽しむ方法",
                    "post_date": "2015-09-28T09:00:16+00:00"
                  },
                  "sort": [
                    1443430816000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xgw",
                  "_score": null,
                  "_source": {
                    "post_title": "Active Directory資産を活用したAWS API認証",
                    "post_date": "2015-09-26T22:10:25+00:00"
                  },
                  "sort": [
                    1443305425000
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "smartphone",
          "doc_count": 738,
          "update": {
            "hits": {
              "total": 738,
              "max_score": null,
              "hits": [
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xh2",
                  "_score": null,
                  "_source": {
                    "post_title": "[iOS][Android]【第21回】potatotips(iOS/Android開発Tips共有会) に参加しました!",
                    "post_date": "2015-09-17T20:29:01+00:00"
                  },
                  "sort": [
                    1442521741000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA3DNTWwpd4B7XkX",
                  "_score": null,
                  "_source": {
                    "post_title": "[iOS 9] ついにiOS 9のリリース日が決定!iPad ProやiPhone 6sの発表も!",
                    "post_date": "2015-09-10T05:17:51+00:00"
                  },
                  "sort": [
                    1441862271000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xid",
                  "_score": null,
                  "_source": {
                    "post_title": "[iOS] UICollectionView のレイアウトクラスを作成して「左右のアイテムをチラ見せするレイアウト」を実現する",
                    "post_date": "2015-09-08T08:55:14+00:00"
                  },
                  "sort": [
                    1441702514000
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "server-side",
          "doc_count": 677,
          "update": {
            "hits": {
              "total": 677,
              "max_score": null,
              "hits": [
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7XiJ",
                  "_score": null,
                  "_source": {
                    "post_title": "【Scala】foldLeftとfoldRightのしくみ",
                    "post_date": "2015-09-25T17:26:35+00:00"
                  },
                  "sort": [
                    1443201995000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xhu",
                  "_score": null,
                  "_source": {
                    "post_title": "RESTful Hypermedia API サーバサイド編 - garage",
                    "post_date": "2015-09-25T07:00:06+00:00"
                  },
                  "sort": [
                    1443164406000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7Xhz",
                  "_score": null,
                  "_source": {
                    "post_title": "【Scala】Future と未来のセカイ",
                    "post_date": "2015-09-24T18:59:41+00:00"
                  },
                  "sort": [
                    1443121181000
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "etc",
          "doc_count": 670,
          "update": {
            "hits": {
              "total": 670,
              "max_score": null,
              "hits": [
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA27NTWwpd4B7XiB",
                  "_score": null,
                  "_source": {
                    "post_title": "[JRuby][Warbler]Rubyで書いたソースからJARを作成する - (2)定義ファイルの参照と、その他のポイント",
                    "post_date": "2015-09-24T09:34:11+00:00"
                  },
                  "sort": [
                    1443087251000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA3DNTWwpd4B7Xis",
                  "_score": null,
                  "_source": {
                    "post_title": "【Amazon Aurora】MySQLからのデータ移行手順例(Redmine環境",
                    "post_date": "2015-09-23T12:00:20+00:00"
                  },
                  "sort": [
                    1443009620000
                  ]
                },
                {
                  "_index": "devio",
                  "_type": "logs",
                  "_id": "AVshkA3DNTWwpd4B7XkR",
                  "_score": null,
                  "_source": {
                    "post_title": "【AWS】API GatewayとLambdaを使ってBacklog Webhookを扱ってみた",
                    "post_date": "2015-09-17T07:48:29+00:00"
                  },
                  "sort": [
                    1442476109000
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}

cloudsmartphoneserver-side などのカテゴリ毎に公開日が新しい順に結果を取得できました。

Kibana 5.3.0 では Top hits Aggregation を Kibana のメトリクスに指定可能となり、これを Kibana で簡単に取得することができるようになりました。

それでは早速この結果を Kibana で表示してみましょう。Visualize から Data table を選択します。選択可能な Metric Aggregation にTop Hitが追加されています。

screenshot_2017-03-31_20_07_57

Kibana で先ほどの Elasticsearch Search API のクエリと同様の設定を実施します。

Kibana

同じ結果を取得できました。ヒートマップで PV数とか出せればデータとして面白かったのかもしれませんが生のサンプルデータを用意できませんでした。。

また Top hits Aggregation のユースケースを踏まえた解説は弊社木戸のブログエントリが分かりやすいです。

Timepicker

Kibana は時系列データを扱う時にデータ取得範囲を指定します。デフォルト設定は過去 15分以内のデータを取得します。Kibana を初めて触る方がデータ取得できない!という状況の多くは過去 15分のデフォルト設定に気づいていないことが多いです。私も初めて触った時は陥りましたw 話を戻して、データ取得範囲の指定方法は相対指定、絶対指定が可能です。

相対指定1

よく使われる相対指定のセットが用意されています。今日のデータとか、過去1日のデータとか。

Kibana 2

相対指定2

任意の数値で現在までの範囲を指定できます。

screenshot 2017-03-31 17.56.32

絶対指定

任意の時間範囲を指定できます。

Kibana 3

このように今までは可視化したいデータの取得範囲を都度指定する必要がありました。

それが今回のリリースにより、データ取得範囲のスライドが可能となりました。データ取得範囲の左右に大なり小なりが表示されるようになり、左をクリックすると同範囲のまま一つ前にスライドし、右をクリックすると一つ後ろにスライドします。

例えば、CloudFront のアクセスログを可視化します。2月7日(UTC) の時間単位のアクセス数を線グラフでプロットしています。

devio-accesscount_-_Kibana

左を押すと、

devio-accesscount_-_Kibana 3

2月6日(UTC) に移動してプロットされます。

障害の調査や、今後の予測で前日、前週、前月と傾向を見るのに役立ちそうですね。

まとめ

いかがでしたでしょうか?
Kibana 5.3.0 は 20の新機能が実装されています。今回は 2つの機能しか紹介できていないので是非リリースノートをご参照ください。