# Workaround for nested objects

**URL:** <https://discuss.elastic.co/t/workaround-for-nested-objects/107108>\
**Category:** Kibana\
**Created:** [November 9, 2017, 9:15pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108 "2017-11-09T21:15:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirek](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@mirek](https://discuss.elastic.co/u/mirek)\
**Post date:** [November 9, 2017, 9:15pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/1 "2017-11-09T21:15:18Z")

</div>

Hi

I'm working or reports in Kibana and recently I faced issues with lack of support for querying over nested types ( [https://www.elastic.co/guide/en/kibana/current/nested-objects.html](https://www.elastic.co/guide/en/kibana/current/nested-objects.html) )

Let's assume I have following index with people

```
 {
        playerId: 123,
    	firstName: "John",
    	lastName: "Doe",
    	clubs:[
                { clubName: "Bayern", status: "active",position: "goal keeper"},
    	        { clubName: "Manchester United", status: "inactive", position: "defense" }]
    }

```

.  
**Can I create filter for "active" players" in "Bayern" in Kibana ?**

I thought I have found a solution by flattening record (like below) but then I have to use UniqeCount over "playerId" fields. Unfortunatelly uniqueCount is only an approximation ( [https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html) )

```
{ playerId:123, firstName: "John", lastName: "Doe", clubName: "Bayern", status: "active", position: "goal keeper" }
{ playerId:123, firstName: "John", lastName: "Doe", clubName: "Manchester United", status: "inactive", position: "defense" }

```

.  
**Do you have any suggestion how to apporach this with Kibana?**

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [November 10, 2017, 3:00pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/2 "2017-11-10T15:00:32Z")

</div>

You should be able to do this if you set your mapping type to clubs as nested. Then you can use a nested query.

Here is how I'm updating the mapping (before I index data, mappings can't be adjusted afterwards):

```auto
PUT soccerplayers
{
  "mappings": {
    "data": {
      "properties": {
        "clubs": {
          "type": "nested" 
        }
      }
    }
  }
}

```

Then I can use some special syntax to create a filter to give me the right data (I edit the Query DSL, I'm not sure how to do this in the query bar - @Bargs - would Kuery be able to handle nested queries like this?) :

```auto
{
  "query": {
    "nested": {
      "path": "clubs",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "clubs.clubName": "Bayern"
              }
            },
            {
              "match": {
                "clubs.status": "active"
              }
            }
          ]
        }
      }
    }
  }
}

```

Then I get back results where players are only active in Bayern, not Manchester United:

 ![25 AM](https://us1.discourse-cdn.com/elastic/original/3X/7/b/7b6e1edbeca0bd8ceaa2b98b83ef321b238580cc.png)

I am seeing an error, though it doesn't seem to affect the actual results:

 ![49 AM](https://us1.discourse-cdn.com/elastic/original/3X/5/2/52e00127d29dab2a58f32d92e73dd574c2481aab.png)  
(@bargs - any idea on that error?)

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [November 10, 2017, 3:01pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/3 "2017-11-10T15:01:14Z")

</div>

btw, here is some more information which goes over how to index and query nested objects: [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/nested.html)

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [November 10, 2017, 5:33pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/4 "2017-11-10T17:33:15Z")

</div>

It's impossible to do a nested query with the lucene query syntax. I think we can add nested support to Kuery in a way that's transparent to the user, but we don't support it today. It's on my wishlist of things to implement in the future.

I'm not sure about that error, I haven't seen it before. Is it specific to this particular query/data set, or does it show up for any nested query?

---

<div class="post-metadata">

**Author:** ![mirek](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@mirek](https://discuss.elastic.co/u/mirek)\
**Post date:** [November 13, 2017, 11:58am UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/5 "2017-11-13T11:58:40Z")

</div>

@Stacey_Gammon Thank you for suggestions - indeeed this might help with querying for nested objects.

I was only wondering if it's possible to build such queries by uses who clicks on visualizaitons. For example if I have a Pie Chart with all clubs then I would like to click piece with "Bayern" and filter by Bayern. This approach would work in simple (non-nested) scenario.

Do you have an idea how to achieve it with nested query?

@Bargs Do you think that future release of Kuery might support such a scenario?

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [November 13, 2017, 2:40pm UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/6 "2017-11-13T14:40:01Z")

</div>

What about flattening your data structure and indexing these as separate documents, is that a possibility? e.g.

```auto
 {
        playerId: 123,
    	firstName: "John",
    	lastName: "Doe",
    	clubName: "Bayern",
       status: "active",
       position: "goal keeper"
    }
 {
        playerId: 123,
    	firstName: "John",
    	lastName: "Doe",
    	clubName: "Manchester United",
       status: "inactive",
       position: "defense"
    }

```

I think that would be easier to work with in Kibana.

---

<div class="post-metadata">

**Author:** ![mirek](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@mirek](https://discuss.elastic.co/u/mirek)\
**Post date:** [November 14, 2017, 5:59am UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/7 "2017-11-14T05:59:49Z")

</div>

> [@Stacey\_Gammon](#):
>
> your data structure and indexing these as separate

@Stacey_Gammon, yes that's exactly the same approach as I proposes in first post 🙂 Unfortunatelly the I have to aggregate players by playerId using uniqueCount which is an approximation (not acceptable by business) - [Finding Distinct Counts | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html)

---

<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:** [December 12, 2017, 6:00am UTC](https://discuss.elastic.co/t/workaround-for-nested-objects/107108/8 "2017-12-12T06:00:00Z")

</div>

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