# Dynamic template copy\_to does not work with flattened type

**URL:** <https://discuss.elastic.co/t/dynamic-template-copy-to-does-not-work-with-flattened-type/354026>\
**Category:** Elasticsearch\
**Created:** [February 23, 2024, 11:23pm UTC](https://discuss.elastic.co/t/dynamic-template-copy-to-does-not-work-with-flattened-type/354026 "2024-02-23T23:23:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s.moran](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@s.moran](https://discuss.elastic.co/u/s.moran)\
**Post date:** [February 23, 2024, 11:23pm UTC](https://discuss.elastic.co/t/dynamic-template-copy-to-does-not-work-with-flattened-type/354026/1 "2024-02-23T23:23:53Z")

</div>

I have a field of flattened type that contains a subfield that I want to do numeric range searches on. I am trying to use a dynamic template to copy the field's value to a top level field that I can use for range queries and sorting but it's not working.

Here is a simplified version of the template:

```auto
{
    "mappings": {
        "dynamic_templates": [
            {
                "age_as_long": {
                    "path_match": "person.age",
                    "mapping": {
                        "type": "long",
                        "copy_to": "personAge"
                    }
                }
            }
        ],
        "properties": {
            "id": {
                "type": "keyword"
            },
            "person": {
                "type": "flattened"
            }
        }
    }
}

```

With an example doc

```auto
{
    "id": "123",
	"person": {
		"name": "jim",
		"age": 30
	}
}

```

I would expect the following query to return the doc but it doesn't

```auto
{
    "query": {
        "range": {
            "personAge": {
                "gte": 20,
                "lte": 40
            }
        }
    }
}

```

Is this expected behaviour or is it a bug?

Elasticsearch version: 8.3.2

---

<div class="post-metadata">

**Author:** ![Sunile\_Manjee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunile_manjee/32/111461_2.png) [@Sunile\_Manjee](https://discuss.elastic.co/u/Sunile_Manjee)\
**Post date:** [February 25, 2024, 11:36pm UTC](https://discuss.elastic.co/t/dynamic-template-copy-to-does-not-work-with-flattened-type/354026/2 "2024-02-25T23:36:16Z")

</div>

that may not work using `flattened` field type. considering moving `person.age` to its own field. However you can use runtime fields. here is what worked for me using your mapping and docs

```auto
GET /suntest3/_search
{
  "runtime_mappings": {
    "personAge": {
      "type": "long",
      "script": {
        "source": """
        if (doc.containsKey('person.age')) {
          String ageString = doc['person.age'].value;
          if (ageString != null && !ageString.isEmpty()) {
            long age = Long.parseLong(ageString);
            emit(age);
          }
        }
        """
      }
    }
  },
  "query": {
    "range": {
      "personAge": {
        "gte": 20,
        "lte": 40
      }
    }
  }
}
```

---

<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:** [March 24, 2024, 11:37pm UTC](https://discuss.elastic.co/t/dynamic-template-copy-to-does-not-work-with-flattened-type/354026/3 "2024-03-24T23:37:02Z")

</div>

Closed on March 24, 2024, 11:37pm UTC

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