# Index document with double nested property using Logstash

**URL:** https://discuss.elastic.co/t/index-document-with-double-nested-property-using-logstash/276755
**Category:** Logstash
**Created:** [June 23, 2021, 9:10am UTC](https://discuss.elastic.co/t/index-document-with-double-nested-property-using-logstash/276755 "2021-06-23T09:10:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joe100](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@joe100](https://discuss.elastic.co/u/joe100)
#### Post date: [June 23, 2021, 9:10am UTC](https://discuss.elastic.co/t/index-document-with-double-nested-property-using-logstash/276755/1 "2021-06-23T09:10:30Z")

</div>

I have `my_index` with a double nested property (nested of nested) and i'm looking for the right logshash configuration to correctly index documents with the following mappings:

```auto
"mappings": {
  "properties": {
    "id": {"type": "long"},
    "name": {"type": "text"},
    "age": {"type": "integer"},
    "children": {
      "type": "nested",
      "properties": {
        "id": {"type": "keyword"},
        "name": {"type": "text"},
        "age": {"type": "integer"},
        "grandchildren": {
          "type": "nested",
          "properties": {
            "id": {"type": "keyword"},
            "name": {"type": "text"},
            "age": {"type": "integer"}
          }
        }
      }
    }
  }
}

```

Database structure:

```auto
Parent
id | age | name
----------------------------------------------------------------------------------------
1 | 58 | James
2 | 62 | Carl

Child
id | age | name | parent_id
-----------------------------------------------------------------------------------------------------------------
1 | 39 | David | 1
2 | 37 | Emma | 1
3 | 41 | Ned | 2

GrandChild
id | age | name | child_id
-----------------------------------------------------------------------------------------------------------------
1 | 7 | Matt | 1
2 | 3 | Zoe | 1
3 | 1 | Vittoria | 1
4 | 4 | Loki | 3
5 | 6 | Oscar | 3

```

Logstash configuration:

```auto
input {
  jdbc {
    ...
    statement => "
	  select 
	  	p.id, p.age, p.name,
	  	c.id c_id, c.age c_age, c.name c_name,
	  	gc.id gc_id, gc.age gc_age, gc.name gc_name
	  from parent p
	  left outer join child c on c.parent_id = p.id
	  left outer join grandchild gc on gc.child_id = c.id"
	...
  }
}

filter {
  mutate {
    ...
    rename => { "c_id" => "[children][id]"}
    rename => { "c_age" => "[children][age]"}
    rename => { "c_name" => "[children][name]"}
    rename => { "gc_id" => "[children][grandchildren][id]"}
    rename => { "gc_age" => "[children][grandchildren][age]"}
    rename => { "gc_name" => "[children][grandchildren][name]"}
  }
  aggregate {
    task_id => "%{id}"
    code => "
      ...
  	  map['children'] ||= []
  	  map['children'] <<= event.get('children')
  	  event.cancel()
    "
    push_previous_map_as_event => true
    timeout => 5
    timeout_tags => ["aggregate"]
  }
  ruby {
    code => "
  	event.set('valutazioni.importi', event.get('valutazioni.importi').uniq)
    "
  }
}

output {
  elasticsearch {
  	index => "my_index"
  	hosts => [".."]
  	document_id => "%{id}"
  }
  stdout { codec => dots }
}

```

What I GET ( **wrong** ) querying my\_index with doc\_id = 1:

```auto
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_source" : {
    "id" : 1,
    "name" : "James",
    "age" : 58,
    "children" : [
      {
        "id" : 1,
        "name" : "David",
        "age" : 39,
        "grandchildren" : [
          {
            "id" : 1,
			"name" : "Matt",
			"age" : 7
          }
        ]
      },
	  {
        "id" : 1,
        "name" : "David",
        "age" : 39,
        "grandchildren" : [
          {
            "id" : 2,
			"name" : "Zoe",
			"age" : 3
          }
		]
	  },
	  {
        "id" : 1,
        "name" : "David",
        "age" : 39,
        "grandchildren" : [
          {
            "id" : 3,
			"name" : "Vittoria",
			"age" : 1
          }
		]
	  },
	  {
        "id" : 2,
        "name" : "Emma",
        "age" : 37,
        "grandchildren" : []
	  }
    ]
  }
}

```

What I WANT:

```auto
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_source" : {
    "id" : 1,
    "name" : "James",
    "age" : 58,
    "children" : [
      {
        "id" : 1,
        "name" : "David",
        "age" : 39,
        "grandchildren" : [
          {
            "id" : 1,
			"name" : "Matt",
			"age" : 7
          },
          {
            "id" : 2,
			"name" : "Zoe",
			"age" : 3
          },
          {
            "id" : 3,
			"name" : "Vittoria",
			"age" : 1
          }
        ]
      },
	  {
        "id" : 2,
        "name" : "Emma",
        "age" : 37,
        "grandchildren" : []
	  }
    ]
  }
}

```

I am able to get my goal in Java with Spring-elasticsearch (therefore i know the mapping is correct), but I NEED to do it via Logstash!

Any suggestion is very appreciated, thanks.

---

<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: [July 21, 2021, 9:10am UTC](https://discuss.elastic.co/t/index-document-with-double-nested-property-using-logstash/276755/2 "2021-07-21T09:10:49Z")

</div>

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