# Nested fields vs. flat document

**URL:** <https://discuss.elastic.co/t/nested-fields-vs-flat-document/167191>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 8:05pm UTC](https://discuss.elastic.co/t/nested-fields-vs-flat-document/167191 "2019-02-05T20:05:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpeedDaemon](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@SpeedDaemon](https://discuss.elastic.co/u/SpeedDaemon)\
**Post date:** [February 5, 2019, 8:05pm UTC](https://discuss.elastic.co/t/nested-fields-vs-flat-document/167191/1 "2019-02-05T20:05:21Z")

</div>

I'm currently creating an index template for network logs. I currently have something like this:

```auto
    "mappings" : {
      "doc" : {
        "properties" : {
    ...
          "ip_src" : {
            "type" : "ip"
          },
          "ip_dst" : {
            "type" : "ip"
          },
          "ip_nat_src" : {
            "type" : "ip"
          },
          "ip_nat_dst" : {
            "type" : "ip",
            "index" : "false"
          },
    ...

```

I'm thinking something that would allow referring to the fields as src.nat.ip would be better:

```auto
    "mappings" : {
      "doc" : {
        "properties" : {
    ...
          "src" : {
		    "properties" : {
              "ip" : {
                "type" : "ip"
              },
              "port" : {
                "type" : "long"
              },
              "nat" : {
                "properties" : {
                  "ip" : {
                    "type" : "ip"
                  },
                  "port" : {
                    "type" : "long"
                  }
				}
			  },
    ...

```

Are there any performace/storage implications to this other than changing how you reference the fields (I'm indexing about 500 mil documents/day, so every little bit matters)? I've not been able to find anything definitive.

---

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [March 5, 2019, 1:36pm UTC](https://discuss.elastic.co/t/nested-fields-vs-flat-document/167191/2 "2019-03-05T13:36:50Z")

</div>

Hi,  
what are the queries or aggregations that you have in mind? That might help to answer your question.

Also, slightly related to the optimisation topic, you might consider using [dynamic templates](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/dynamic-templates.html) to shrink down the size of your index templates. E.g., with respect to your first mapping, something like

```auto
{
  "mappings": {
    "doc": {
     "properties": { ... },
      "dynamic_templates": {
        "ip_address": {
          "match": "ip_*",
          "mapping": {
            "type": "ip"
          }
        }
      }
    }
  }
}

```

---

<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:** [April 2, 2019, 1:39pm UTC](https://discuss.elastic.co/t/nested-fields-vs-flat-document/167191/3 "2019-04-02T13:39:17Z")

</div>

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