# Is it possible to extract day, month of a date at the time of indexing

**URL:** https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121
**Category:** Elasticsearch
**Created:** [December 26, 2018, 9:50am UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121 "2018-12-26T09:50:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![msmani](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@msmani](https://discuss.elastic.co/u/msmani)
#### Post date: [December 26, 2018, 9:50am UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121/1 "2018-12-26T09:50:32Z")

</div>

Assume I index a field named "created\_on" as date type (epoch), now I want to extract reports like

1. number of documents created on Monday.

2.number of documents created in the month of December.

etc...

now if I simply use date type then these kind of queries are not possible without using scripts but using scripts will make it slower. So it looks like I have to extract these information at the time of indexing and store it as a separate field like created\_on\_day, created\_on\_month etc...

I can extract these information at the application level and then index it as separate field altogether but I want to know is it possible to index created\_on itself in dfifferent ways (something like [this](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)).

e.g...

```
 "properties" : {
         "created_on" : {
                 "type" : "date",
                  "fields" : {
                          "day" : {
                                 "type" : "integer",
                                  // is it possible to add some custom analyzer or script to extract the day
                           }
                   }
          }
  }
```

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 26, 2018, 4:48pm UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121/2 "2018-12-26T16:48:49Z")

</div>

You can use the [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/master/script-processor.html) functionality of the ingest node.

---

<div class="post-metadata">

### Author: ![msmani](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@msmani](https://discuss.elastic.co/u/msmani)
#### Post date: [December 27, 2018, 9:36am UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121/3 "2018-12-27T09:36:06Z")

</div>

I tried script processor and it worked for the question I originally posted but I need something generic/reusable.

In actual scenario my index will have lots of date field, like updated\_on, closed\_on etc..., and if use this script processor then I have to add a three processors for each field, is there any way we can do something generic and use it for all date fields.

The script I wrote :

```
  "pipeline" :
  {
     "description": "_description",
     "processors": [
	  {
	    	"set": {
	    		"field": "created_on_extract.month",
	    		"value": ""
			}	
	 },
	 {
	 	"set": {
	    		"field": "created_on_extract.day",
	    		"value": ""
		}	
	},
  {
  	
 "script": {
		"lang": "painless",
		"source": "def formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd'); def localDate = LocalDate.parse( ctx.created_on , formatter ); ctx.created_on_extract.month =localDate.getMonthValue(); ctx.created_on_extract.day =localDate.getDayOfWeek().getValue() "
		}
    }
   ]
   }
```

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 27, 2018, 2:32pm UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121/4 "2018-12-27T14:32:45Z")

</div>

You can do everything in a single script processor. Nobody limits you to working with only one field at a time and you don't have to set values to the new fields in separate processors.

---

<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: [January 24, 2019, 2:32pm UTC](https://discuss.elastic.co/t/is-it-possible-to-extract-day-month-of-a-date-at-the-time-of-indexing/162121/5 "2019-01-24T14:32:52Z")

</div>

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