Skip to content

Instantly share code, notes, and snippets.

@ijokarumawak
Last active April 12, 2022 05:54
Show Gist options
  • Save ijokarumawak/3dfaa10827759c66c79cee71a4459bca to your computer and use it in GitHub Desktop.
Save ijokarumawak/3dfaa10827759c66c79cee71a4459bca to your computer and use it in GitHub Desktop.
Runtime Field を利用した Kibana ダッシュボードサンプル

Runtime Field を利用した Kibana ダッシュボードサンプル

"socks_a" のように、カテゴリ名_会社名という値を Runtime field を使い、カテゴリ名、会社名と別々のフィールドとして扱うサンプル。

# 1. サンプルデータの登録
POST items-sample/_bulk
{"index": {}}
{"sku":"socks_a","price":500}
{"index": {}}
{"sku":"socks_b","price":300}
{"index": {}}
{"sku":"hat_a","price":2500}
{"index": {}}
{"sku":"hat_b","price":3200}

# 2. Runtime Field の追加
PUT items-sample/_mapping
{
  "runtime": {
    "item": {
      "type": "composite",
      "script": "emit(dissect(\"%{category}_%{company}\").extract(doc[\"sku.keyword\"].value))",
      "fields": {
        "category": {
          "type": "keyword"
        },
        "company": {
          "type": "keyword"
        }
      }
    }
  }
}

# 3. 検索してテスト
GET items-sample/_search
{
  "query": {
    "term": {
      "item.category": {
        "value": "socks"
      }
    }
  }
}

# 4. Stack Management > Saved Objects から runtime-sample.ndjson をインポートしてダッシュボードを作成
{"attributes":{"fieldAttrs":"{}","fields":"[]","runtimeFieldMap":"{}","title":"items-sample","typeMeta":"{}"},"coreMigrationVersion":"7.15.1","id":"518a2310-ba20-11ec-921d-9d7a7d39d334","migrationVersion":{"index-pattern":"7.11.0"},"references":[],"type":"index-pattern","updated_at":"2022-04-12T05:20:51.148Z","version":"WzI5MzcsMl0="}
{"attributes":{"description":"","hits":0,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[]}"},"optionsJSON":"{\"useMargins\":true,\"syncColors\":false,\"hidePanelTitles\":false}","panelsJSON":"[{\"version\":\"7.15.1\",\"type\":\"lens\",\"gridData\":{\"x\":0,\"y\":0,\"w\":18,\"h\":15,\"i\":\"c554e293-70d6-4e87-9f89-53d4b0697c69\"},\"panelIndex\":\"c554e293-70d6-4e87-9f89-53d4b0697c69\",\"embeddableConfig\":{\"attributes\":{\"title\":\"\",\"visualizationType\":\"lnsDatatable\",\"type\":\"lens\",\"references\":[{\"type\":\"index-pattern\",\"id\":\"518a2310-ba20-11ec-921d-9d7a7d39d334\",\"name\":\"indexpattern-datasource-current-indexpattern\"},{\"type\":\"index-pattern\",\"id\":\"518a2310-ba20-11ec-921d-9d7a7d39d334\",\"name\":\"indexpattern-datasource-layer-a984a68d-995f-4500-8e51-9b3d9adc8e81\"}],\"state\":{\"visualization\":{\"layerId\":\"a984a68d-995f-4500-8e51-9b3d9adc8e81\",\"layerType\":\"data\",\"columns\":[{\"isTransposed\":false,\"columnId\":\"4278a3d3-1ad3-45b6-9cb8-33c2604b7e0a\"},{\"isTransposed\":false,\"columnId\":\"6373b701-88e1-43a1-b99f-7255bb831700\"}]},\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filters\":[],\"datasourceStates\":{\"indexpattern\":{\"layers\":{\"a984a68d-995f-4500-8e51-9b3d9adc8e81\":{\"columns\":{\"4278a3d3-1ad3-45b6-9cb8-33c2604b7e0a\":{\"label\":\"Top values of sku.keyword\",\"dataType\":\"string\",\"operationType\":\"terms\",\"scale\":\"ordinal\",\"sourceField\":\"sku.keyword\",\"isBucketed\":true,\"params\":{\"size\":5,\"orderBy\":{\"type\":\"column\",\"columnId\":\"6373b701-88e1-43a1-b99f-7255bb831700\"},\"orderDirection\":\"desc\",\"otherBucket\":true,\"missingBucket\":false}},\"6373b701-88e1-43a1-b99f-7255bb831700\":{\"label\":\"Median of price\",\"dataType\":\"number\",\"operationType\":\"median\",\"sourceField\":\"price\",\"isBucketed\":false,\"scale\":\"ratio\"}},\"columnOrder\":[\"4278a3d3-1ad3-45b6-9cb8-33c2604b7e0a\",\"6373b701-88e1-43a1-b99f-7255bb831700\"],\"incompleteColumns\":{}}}}}}},\"enhancements\":{}}},{\"version\":\"7.15.1\",\"type\":\"lens\",\"gridData\":{\"x\":18,\"y\":0,\"w\":24,\"h\":15,\"i\":\"ca8d5f66-1aab-46c2-bb71-b8828aeb58aa\"},\"panelIndex\":\"ca8d5f66-1aab-46c2-bb71-b8828aeb58aa\",\"embeddableConfig\":{\"attributes\":{\"title\":\"\",\"visualizationType\":\"lnsPie\",\"type\":\"lens\",\"references\":[{\"type\":\"index-pattern\",\"id\":\"518a2310-ba20-11ec-921d-9d7a7d39d334\",\"name\":\"indexpattern-datasource-current-indexpattern\"},{\"type\":\"index-pattern\",\"id\":\"518a2310-ba20-11ec-921d-9d7a7d39d334\",\"name\":\"indexpattern-datasource-layer-62aaf373-9e58-45c5-b24b-4b38347b472e\"}],\"state\":{\"visualization\":{\"shape\":\"pie\",\"layers\":[{\"layerId\":\"62aaf373-9e58-45c5-b24b-4b38347b472e\",\"groups\":[\"47c92ad4-a0c3-4f9a-b00e-9694213894b2\",\"6f8d4b39-c826-4899-b0cd-b98feaac3672\"],\"metric\":\"c8f591c1-2da3-4cad-84b8-f2fca3afcb56\",\"numberDisplay\":\"percent\",\"categoryDisplay\":\"default\",\"legendDisplay\":\"default\",\"nestedLegend\":false,\"layerType\":\"data\"}]},\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filters\":[],\"datasourceStates\":{\"indexpattern\":{\"layers\":{\"62aaf373-9e58-45c5-b24b-4b38347b472e\":{\"columns\":{\"47c92ad4-a0c3-4f9a-b00e-9694213894b2\":{\"label\":\"Top values of item.category\",\"dataType\":\"string\",\"operationType\":\"terms\",\"scale\":\"ordinal\",\"sourceField\":\"item.category\",\"isBucketed\":true,\"params\":{\"size\":5,\"orderBy\":{\"type\":\"column\",\"columnId\":\"c8f591c1-2da3-4cad-84b8-f2fca3afcb56\"},\"orderDirection\":\"desc\",\"otherBucket\":true,\"missingBucket\":false}},\"6f8d4b39-c826-4899-b0cd-b98feaac3672\":{\"label\":\"Top values of item.company\",\"dataType\":\"string\",\"operationType\":\"terms\",\"scale\":\"ordinal\",\"sourceField\":\"item.company\",\"isBucketed\":true,\"params\":{\"size\":3,\"orderBy\":{\"type\":\"column\",\"columnId\":\"c8f591c1-2da3-4cad-84b8-f2fca3afcb56\"},\"orderDirection\":\"desc\",\"otherBucket\":true,\"missingBucket\":false}},\"c8f591c1-2da3-4cad-84b8-f2fca3afcb56\":{\"label\":\"Sum of price\",\"dataType\":\"number\",\"operationType\":\"sum\",\"sourceField\":\"price\",\"isBucketed\":false,\"scale\":\"ratio\"}},\"columnOrder\":[\"47c92ad4-a0c3-4f9a-b00e-9694213894b2\",\"6f8d4b39-c826-4899-b0cd-b98feaac3672\",\"c8f591c1-2da3-4cad-84b8-f2fca3afcb56\"],\"incompleteColumns\":{}}}}}}},\"enhancements\":{}}}]","timeRestore":false,"title":"runtime-sample","version":1},"coreMigrationVersion":"7.15.1","id":"f28b3820-ba21-11ec-921d-9d7a7d39d334","migrationVersion":{"dashboard":"7.15.0"},"references":[{"id":"518a2310-ba20-11ec-921d-9d7a7d39d334","name":"c554e293-70d6-4e87-9f89-53d4b0697c69:indexpattern-datasource-current-indexpattern","type":"index-pattern"},{"id":"518a2310-ba20-11ec-921d-9d7a7d39d334","name":"c554e293-70d6-4e87-9f89-53d4b0697c69:indexpattern-datasource-layer-a984a68d-995f-4500-8e51-9b3d9adc8e81","type":"index-pattern"},{"id":"518a2310-ba20-11ec-921d-9d7a7d39d334","name":"ca8d5f66-1aab-46c2-bb71-b8828aeb58aa:indexpattern-datasource-current-indexpattern","type":"index-pattern"},{"id":"518a2310-ba20-11ec-921d-9d7a7d39d334","name":"ca8d5f66-1aab-46c2-bb71-b8828aeb58aa:indexpattern-datasource-layer-62aaf373-9e58-45c5-b24b-4b38347b472e","type":"index-pattern"}],"type":"dashboard","updated_at":"2022-04-12T05:42:46.280Z","version":"WzMxNzYsMl0="}
{"excludedObjects":[],"excludedObjectsCount":0,"exportedCount":2,"missingRefCount":0,"missingReferences":[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment