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

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).

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
                           }
                   }
          }
  }

You can use the script processor functionality of the ingest node.

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() "
		}
    }
   ]
   }

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.

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