# Vega: Field collapsing and inner hits not working for Vega visualization

**URL:** <https://discuss.elastic.co/t/vega-field-collapsing-and-inner-hits-not-working-for-vega-visualization/258233>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [December 10, 2020, 8:59am UTC](https://discuss.elastic.co/t/vega-field-collapsing-and-inner-hits-not-working-for-vega-visualization/258233 "2020-12-10T08:59:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)\
**Post date:** [December 10, 2020, 8:59am UTC](https://discuss.elastic.co/t/vega-field-collapsing-and-inner-hits-not-working-for-vega-visualization/258233/1 "2020-12-10T08:59:29Z")

</div>

Hi,

I have an index with two fields; **ID** and **Status**. Each ID will have 3 statuses; **start** , **pending** and **end** , which are included in the same index. What I want to achieve is to get the _ **last status** _ for each ID from the index so that I can use it with a Vega visualization to create a pie-chart.

I'm trying to **collapse search results based on field values**. Here's the API call I tried on my index.

```
GET /demo*/_search
{
  "collapse": {
    "field": "ID.keyword",
    "inner_hits": {
      "name": "by_status",
      "collapse": {
        "field": "Status.keyword"
      },
      "sort": [
        {
          "Status.keyword": {
            "order": "asc"
          }
        }
      ],
      "size": 1
    }
  },
  "sort": [
    {
      "ID.keyword": {
        "order": "asc"
      }
    }
  ]
}

```

It delivers the need to get the _last status_ for each ID. Here's the response from Elasticsearch.

```
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 16,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "demo-index",
        "_type" : "_doc",
        "_id" : "0b2JR3YBsgdxC8Q2vhg5",
        "_score" : null,
        "_source" : {
          "message" : "1,start",
          "host" : "host-computer",
          "ID" : "1",
          "@timestamp" : "2020-12-09T12:46:16.532Z",
          "Status" : "start",
          "path" : "/path/to/the/data/file.csv",
          "@version" : "1"
        },
        "fields" : {
          "ID.keyword" : [
            "1"
          ]
        },
        "sort" : [
          "1"
        ],
        "inner_hits" : {
          "by_status" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "demo-index",
                  "_type" : "_doc",
                  "_id" : "2L2JR3YBsgdxC8Q2whgP",
                  "_score" : null,
                  "_source" : {
                    "message" : "1,end",
                    "host" : "host-computer",
                    "ID" : "1",
                    "@timestamp" : "2020-12-09T12:46:16.586Z",
                    "Status" : "end",
                    "path" : "/path/to/the/data/file.csv",
                    "@version" : "1"
                  },
                  "fields" : {
                    "Status.keyword" : [
                      "end"
                    ]
                  },
                  "sort" : [
                    "end"
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "demo-index",
        "_type" : "_doc",
        "_id" : "0r2JR3YBsgdxC8Q2vhjG",
        "_score" : null,
        "_source" : {
          "message" : "2,start",
          "host" : "host-computer",
          "ID" : "2",
          "@timestamp" : "2020-12-09T12:46:16.588Z",
          "Status" : "start",
          "path" : "/path/to/the/data/file.csv",
          "@version" : "1"
        },
        "fields" : {
          "ID.keyword" : [
            "2"
          ]
        },
        "sort" : [
          "2"
        ],
        "inner_hits" : {
          "by_status" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "demo-index",
                  "_type" : "_doc",
                  "_id" : "072JR3YBsgdxC8Q2vhjG",
                  "_score" : null,
                  "_source" : {
                    "message" : "2,end",
                    "host" : "host-computer",
                    "ID" : "2",
                    "@timestamp" : "2020-12-09T12:46:16.588Z",
                    "Status" : "end",
                    "path" : "/path/to/the/data/file.csv",
                    "@version" : "1"
                  },
                  "fields" : {
                    "Status.keyword" : [
                      "end"
                    ]
                  },
                  "sort" : [
                    "end"
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "demo-index",
        "_type" : "_doc",
        "_id" : "1L2JR3YBsgdxC8Q2vhjG",
        "_score" : null,
        "_source" : {
          "message" : "3,start",
          "host" : "host-computer",
          "ID" : "3",
          "@timestamp" : "2020-12-09T12:46:16.589Z",
          "Status" : "start",
          "path" : "/path/to/the/data/file.csv",
          "@version" : "1"
        },
        "fields" : {
          "ID.keyword" : [
            "3"
          ]
        },
        "sort" : [
          "3"
        ],
        "inner_hits" : {
          "by_status" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "demo-index",
                  "_type" : "_doc",
                  "_id" : "1r2JR3YBsgdxC8Q2vhjG",
                  "_score" : null,
                  "_source" : {
                    "message" : "3,end",
                    "host" : "host-computer",
                    "ID" : "3",
                    "@timestamp" : "2020-12-09T12:46:16.590Z",
                    "Status" : "end",
                    "path" : "/path/to/the/data/file.csv",
                    "@version" : "1"
                  },
                  "fields" : {
                    "Status.keyword" : [
                      "end"
                    ]
                  },
                  "sort" : [
                    "end"
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

I tried using the same collapse in Vega as the body of the data field as shown below.

```
"data": [
  {
    "name": "table",
    "url": {
      "%context%": true,
      "%timefield%": "@timestamp",
      "index": "demo*",
      "body": {
        "collapse": {
          "field": "ID.keyword",
          "inner_hits": {
            "name": "by_location",
            "collapse": {"field": "Status.keyword"},
            "sort": [{"Status.keyword": {"order": "asc"}}],
            "size": 1
          }
        },
        "sort": [{"ID.keyword": {"order": "asc"}}]
      }
    },
    "format": {
      "property": "hits.hits.inner_hits.by_status.hits.hits.fields"
    },
    "transform": [{"type": "pie", "field": "Status.keyword"}]
 }
]

```

But it produces an error.

 ![Issue_Image](https://us1.discourse-cdn.com/elastic/original/3X/4/2/42431fbeae4fdecb2a0c762191b83bdacb2b2e11.png)

What am I doing wrong? Is using a **collapse search** in Vega not possible? Is there any alternative way for achieving this requirement in Vega or any other type of visualization?

Any help would be greatly appreciated. 😊

Cheers!

---

<div class="post-metadata">

**Author:** ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)\
**Post date:** [December 10, 2020, 4:06pm UTC](https://discuss.elastic.co/t/vega-field-collapsing-and-inner-hits-not-working-for-vega-visualization/258233/2 "2020-12-10T16:06:39Z")

</div>

Well, apparantly, **collapse** is a feature of document search, not aggregations. Followed a wrong path there. 😅

---

<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:** [January 7, 2021, 4:06pm UTC](https://discuss.elastic.co/t/vega-field-collapsing-and-inner-hits-not-working-for-vega-visualization/258233/3 "2021-01-07T16:06:41Z")

</div>

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