# 表示値を別フィールドの値にする

**URL:** <https://discuss.elastic.co/t/topic/229404>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [April 23, 2020, 8:04am UTC](https://discuss.elastic.co/t/topic/229404 "2020-04-23T08:04:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hashimoto](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hashimoto](https://discuss.elastic.co/u/hashimoto)\
**Post date:** [April 23, 2020, 8:04am UTC](https://discuss.elastic.co/t/topic/229404/1 "2020-04-23T08:04:55Z")

</div>

グラフを作成する際にX軸をユーザIDで分割するさいに軸に表示される値をユーザ名にするような方法はありますでしょうか。

Kibanaは 6.8.2です。

---

<div class="post-metadata">

**Author:** ![hashimoto](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hashimoto](https://discuss.elastic.co/u/hashimoto)\
**Post date:** [April 23, 2020, 8:59am UTC](https://discuss.elastic.co/t/topic/229404/2 "2020-04-23T08:59:46Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/0/7/079219bdd1a130ffd42286fdf2f0f2d78e906ee6.png)

上記のようなデータで、重複の可能性があるのでIDで分割は行うが表示はユーザ名としたい形です。

---

<div class="post-metadata">

**Author:** ![Koji\_Kawamura](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koji_kawamura/32/93938_2.png) [@Koji\_Kawamura](https://discuss.elastic.co/u/Koji_Kawamura)\
**Post date:** [May 21, 2020, 2:44am UTC](https://discuss.elastic.co/t/topic/229404/3 "2020-05-21T02:44:09Z")

</div>

@hashimoto さん、通常のVisualizationではできなさそうですね。ちょっと小難しくなりますが、Vegaを使うとやりたいことはできると思います。こちらで試しにやってみました。ご参考になれば。

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/4/b4eb1292bcc54df973f1ec52d6e7ba22e2d7480b.png)

サンプルデータ登録:

```auto
POST forum_229404/_bulk
{"index": {}}
{"userId": "a", "userName": "あ", "code": "x"}
{"index": {}}
{"userId": "b", "userName": "べ", "code": "x"}
{"index": {}}
{"userId": "c", "userName": "し", "code": "x"}
{"index": {}}
{"userId": "a", "userName": "あ", "code": "y"}
{"index": {}}
{"userId": "a", "userName": "あ", "code": "z"}
{"index": {}}
{"userId": "b", "userName": "べ", "code": "z"}
{"index": {}}
{"userId": "a", "userName": "あ", "code": "z"}

```

アグリゲーションリクエストのテスト。  
`terms`アグリゲーションしつつ、それ以外のフィールドを参照するために`top_hits`も使っているのがミソです。

```auto
GET forum_229404/_search
{
  "size": 0,
  "aggs": {
    "userIds": {
      "terms": {
        "field": "userId.keyword", "size": 10
      },
      "aggs": {
        "doc": {"top_hits": {"size": 1}}}}}
}

```

KibanaのVisualizationをVegaで作成:

```auto
{
  "$schema": "https://vega.github.io/schema/vega/v4.3.0.json",
  "title": "表示値を別フィールドの値にする",
  "data": [
    {
      "name": "es",
      "url": {
        "%context%": true,
        "index": "forum_229404",
        "body": {
          "aggs": {
            "userIds": {
              "terms": {"field": "userId.keyword", "size": 10},
              "aggs": {"doc": {"top_hits": {"size": 1}}}
            }
          },
          "size": 0
        }
      },
      "format": {"property": "aggregations.userIds.buckets"}
    }
  ],
  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "es", "field": "key"},
      "range": "width",
      "padding": 0.05,
      "round": true
    },
    {
      "name": "yscale",
      "domain": {"data": "es", "field": "doc_count"},
      "nice": true,
      "range": "height"
    }
  ],
  "axes": [
    {"orient": "bottom", "scale": "xscale", "labelFontSize": 36},
    {"orient": "left", "scale": "yscale", "labelFontSize": 24}
  ],
  "marks": [
    {
      "type": "rect",
      "from": {"data": "es"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "key"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "doc_count"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {"fill": {"value": "steelblue"}},
        "hover": {"fill": {"value": "red"}}
      }
    },
    {
      "type": "text",
      "from": {"data": "es"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "signal": "datum.key", "band": 0.5},
          "y": {"scale": "yscale", "signal": "datum.doc_count", "offset": -2},
          "align": {"value": "center"},
          "baseline": {"value": "bottom"},
          "fill": {"value": "#333"},
          "text": {"field": "doc.hits.hits[0]._source.userName"},
          "fontSize": {"value": 24}
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 18, 2020, 2:44am UTC](https://discuss.elastic.co/t/topic/229404/4 "2020-06-18T02:44:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
