# Use date field to copy year to another field index

**URL:** <https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416>\
**Category:** Elasticsearch\
**Created:** [June 5, 2019, 3:59pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416 "2019-06-05T15:59:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlaprade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dlaprade/32/47576_2.png) [@dlaprade](https://discuss.elastic.co/u/dlaprade)\
**Post date:** [June 5, 2019, 3:59pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/1 "2019-06-05T15:59:57Z")

</div>

Elasticsearch version:

6.3

Situation:

I have a date field that contains a full date:  
`"releaseDate" : { "type" : "date", "format" : "yyyy-MM-dd'T'HH:mm:ssZ" }`  
I would like to copy the year out of a that field into another field. Is this possible to do at index time?

---

<div class="post-metadata">

**Author:** ![dlaprade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dlaprade/32/47576_2.png) [@dlaprade](https://discuss.elastic.co/u/dlaprade)\
**Post date:** [June 5, 2019, 6:43pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/2 "2019-06-05T18:43:54Z")

</div>

I realize that I could use 'copy\_to' to copy the "releaseDate" into another field that uses a normalizer with a char\_filter to strip out the year but the "copy\_to" documentation states:

> You cannot copy recursively via intermediary fields

Maybe that is why this doesn't work:

```
 "releaseDate" : {
                "type" : "date",
                "format" : "yyyy-MM-dd'T'HH:mm:ssZ",
                "copy_to":"releaseYear"
            },
            "releaseYear" : {
            	"type": "keyword",
            	"normalizer": "year_filtered_normalizer",
            	"copy_to": "searchterms"
            },

```

And I tried:

```
"releaseDate" : {
                "type" : "date",
                "format" : "yyyy-MM-dd'T'HH:mm:ssZ",
                "fields" : {
                	"year" : {
                		"type": "keyword",
	                	"normalizer": "year_filtered_normalizer",
	                	"copy_to": "searchterms"
                	}
                }
            },

```

The above gives my an error on indexing:

> "copy\_to in multi fields is not allowed. Found the copy\_to in field [year] which is within a multi field."

Is it possible to even do what I want using settings/mappings?

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 6, 2019, 8:45am UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/3 "2019-06-06T08:45:16Z")

</div>

Hi,

Did you check about ingest to reformat your date?

[https://www.elastic.co/guide/en/elasticsearch/reference/current/date-processor.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-processor.html)

---

<div class="post-metadata">

**Author:** ![dlaprade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dlaprade/32/47576_2.png) [@dlaprade](https://discuss.elastic.co/u/dlaprade)\
**Post date:** [June 6, 2019, 8:26pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/4 "2019-06-06T20:26:17Z")

</div>

When using Elastic version 6.7, I created an ingest pipeline with a given name.

```
{
	"description" : "A pre-processor to handle pulling the release year out of the releaseDate field.",
    "processors": [
        {
            "date": {
                "field": "releaseDate",
                "target_field": "releaseYear",
                "formats": [
                    "yyyy"
                ],
                "ignore_failure" : true
            }
        }
    ]
}

```

I created a new index that set the default\_pipeline to be my created pipeline.

```
"settings": {
    "default_pipeline": "my-pipeline",

```

When re-indexing the data into the new index, ingest pipeline does not fire and therefore the year field does not get populated. Am I doing something incorrectly?

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 7, 2019, 12:34am UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/5 "2019-06-07T00:34:19Z")

</div>

Hi,

without

```auto
"ignore_failure" : true

```

Did you have error message if the year is not set there's certainly a raised error.

---

<div class="post-metadata">

**Author:** ![dlaprade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dlaprade/32/47576_2.png) [@dlaprade](https://discuss.elastic.co/u/dlaprade)\
**Post date:** [June 7, 2019, 3:33pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/6 "2019-06-07T15:33:08Z")

</div>

In my current data set, all records have a releaseDate .

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 8, 2019, 5:35am UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/7 "2019-06-08T05:35:51Z")

</div>

but if you remove ignore\_failure do you have any error message that can help?

Or better can you provide an example of document that you store, it can help to reproduce the problem.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 10, 2019, 1:37pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/8 "2019-06-10T13:37:50Z")

</div>

How about this

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "_description",
    "processors": [
      {
        "script": {
          "lang": "painless",
          "source": "ctx.year = ZonedDateTime.parse(ctx.foo).getYear()"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "foo": "2019-06-17T12:34:56.789Z"
      }
    }
  ]
}

```

---

<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 8, 2019, 1:37pm UTC](https://discuss.elastic.co/t/use-date-field-to-copy-year-to-another-field-index/184416/9 "2019-07-08T13:37:51Z")

</div>

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