# Possible to 'flatten' nested mapping with reindex?

**URL:** <https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313>\
**Category:** Elasticsearch\
**Created:** [June 3, 2018, 11:49am UTC](https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313 "2018-06-03T11:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)\
**Post date:** [June 3, 2018, 11:49am UTC](https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313/1 "2018-06-03T11:49:30Z")

</div>

Hi... I have a nested mapping, which I'd like to reindex into a non-nested structure - i.e. each nested object + the 'parent' document becomes a new document. Is this possible by reindexing? I'd like to avoid reloading from the source ideally.

I've had a good play about and a google but haven't come across any winning combination yet...

Existing mapping is:

```auto
{
	"mappings" : {
		"properties" : {
		  "wsid" : {
			"type" : "keyword"
		  },
		  "date" : {
			"type" : "date",
			"index" : false
		  },
		  "md" : {
			"type" : "short"
		  },
		  "year" : {
			"type" : "short",
			"index" : false
		  },
		  "obs" : {
			"type" : "nested",
			"properties" : {
			  "hr" : {
				"type" : "byte"
			  },
			  "precipmm" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 100.0
			  },
			  "tempc" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 10.0
			  },
			  "snowcm" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 100.0
			  },
			  "sunmins" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 10.0
			  },
			  "cloudpct" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 100.0
			  },
			  "windkph" : {
				"type" : "scaled_float",
				"index" : true,
				"scaling_factor" : 100.0
			  }
		  }
		}
	}
}

```

and what I'd like to get to:

```auto
{
	"mappings" : {
		"properties" : {
		  "wsid" : {
			"type" : "keyword"
		  },
		  "date" : {
			"type" : "date",
			"index" : false
		  },
		  "md" : {
			"type" : "short"
		  },
		  "year" : {
			"type" : "short",
			"index" : false
		  },
		  "hr" : {
			"type" : "byte"
		  },
		  "precipmm" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 100.0
		  },
		  "tempc" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 10.0
		  },
		  "snowcm" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 100.0
		  },
		  "sunmins" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 10.0
		  },
		  "cloudpct" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 100.0
		  },
		  "windkph" : {
			"type" : "scaled_float",
			"index" : true,
			"scaling_factor" : 100.0
		  }
	  }
	}
}

```

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 4, 2018, 6:49am UTC](https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313/2 "2018-06-04T06:49:29Z")

</div>

Yes I think this is doable with the reindex API by using a script.

ex:

```auto
ctx._source.flat_field = ctx._source.nested_field.field;

```

But if you're using a nested mapping, the value will be an array of values.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#\_how\_arrays\_of\_objects\_are\_flattened](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#_how_arrays_of_objects_are_flattened)

Another solution would be to use logstash for this purpose.

---

<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 2, 2018, 6:56am UTC](https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313/3 "2018-07-02T06:56:08Z")

</div>

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