# Add weekend field via ingest pipeline

**URL:** <https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039>\
**Category:** Elasticsearch\
**Created:** [April 16, 2019, 9:13am UTC](https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039 "2019-04-16T09:13:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasony](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jasony](https://discuss.elastic.co/u/jasony)\
**Post date:** [April 16, 2019, 9:13am UTC](https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039/1 "2019-04-16T09:13:24Z")

</div>

Hello,

i need to add a field to check whether the date field is weekend or not.

I found below script for weekend identification.

doc[alrm\_occr\_ddt'].date.dayOfWeek == 6 || doc[alrm\_occr\_ddt'].date.dayOfWeek == 7"

so i tries to put ingest pipelines as below.

```
PUT _ingest/pipeline/weekend_true

{
  
  "processors" : [

    {
      
      "set": {
       
          "field": "weekend",
        
          "value": "true"
      
       }
    
    }   
  
  ]

}

PUT _ingest/pipeline/addWeek

{
  
  "description": "check weekend by using alrm_occr_ddt.date.dayOfWeek",
  
  "version": 1,
  
  "processors": [
    
  {
      
     "pipeline": {
        
         "if": "doc[alrm_occr_ddt'].date.dayOfWeek == 6 || doc[alrm_occr_ddt'].date.dayOfWeek == 7",
        
         "name": "weekend_false"
      
      }
    
  }
  
  ]

}

but i am experiencing below error when i tried to put a test index.

PUT test1/doc/2?pipeline=addWeek
{
  "alrm_occr_ddt" : "2019-04-15T02:03:14.000Z"
}

#ERROR
{
  "error": {
    "root_cause": [
      {
        "type": "exception",
        "reason": "java.lang.IllegalArgumentException: ScriptException[compile error]; nested: IllegalArgumentException[unexpected character ['].date.dayOfWeek == 2].]; nested: LexerNoViableAltException;",
        "header": {
          "processor_type": "conditional"
        }
      }
    ],
    "type": "exception",
    "reason": "java.lang.IllegalArgumentException: ScriptException[compile error]; nested: IllegalArgumentException[unexpected character ['].date.dayOfWeek == 2].]; nested: LexerNoViableAltException;",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "ScriptException[compile error]; nested: IllegalArgumentException[unexpected character ['].date.dayOfWeek == 2].]; nested: LexerNoViableAltException;",
      "caused_by": {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "doc[alrm_occr_ddt'].date.dayOfWeek == 2",
          " ^---- HERE"
        ],
        "script": "doc[alrm_occr_ddt'].date.dayOfWeek == 2",
        "lang": "painless",
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "unexpected character ['].date.dayOfWeek == 2].",
          "caused_by": {
            "type": "lexer_no_viable_alt_exception",
            "reason": null
          }
        }
      }
    },
    "header": {
      "processor_type": "conditional"
    }
  },
  "status": 500
}

```

can anyone please advise how i can avoid the error and put correct weekend info?

thank you!

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 16, 2019, 9:49am UTC](https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039/2 "2019-04-16T09:49:36Z")

</div>

Hey Jason,

When asking a question on this forum, please format any code with the `</>` button, as explained [here](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It makes it much easier to read. 🙂

Your script has several issues, the most important of which is that you're using the `doc['field']` notation to access field values. That notation only works at query and aggregation time. At index time, you need to access fields with the `ctx.field` notation.

The following pipeline should work:

```auto
PUT _ingest/pipeline/addWeek
{
  "description": "check weekend by using alrm_occr_ddt.date.dayOfWeek",
  "version": 1,
  "processors": [
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)",
        "name": "weekend_true"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![jasony](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jasony](https://discuss.elastic.co/u/jasony)\
**Post date:** [April 17, 2019, 2:19am UTC](https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039/3 "2019-04-17T02:19:50Z")

</div>

```
PUT _ingest/pipeline/adddayofweek
{
  "description": "check weekend by using alrm_occr_ddt",
  "version": 1,
  "processors": [
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.MONDAY)",
        "name": "dayofweek_monday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.TUESDAY)",
        "name": "dayofweek_tuesday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.WEDNESDAY)",
        "name": "dayofweek_wednesday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.THURSDAY)",
        "name": "dayofweek_thursday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.FRIDAY)",
        "name": "dayofweek_friday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.SATURDAY)",
        "name": "dayofweek_saturday"
      }
    },
    {
      "pipeline": {
        "if": "def dayOfWeek = OffsetDateTime.parse(ctx.alrm_occr_ddt).getDayOfWeek(); return (dayOfWeek == DayOfWeek.SUNDAY)",
        "name": "dayofweek_sunday"
      }
    }
  ]
}

```

i successfully add dayofweek field with above ingest pipeline.

thank you for your reply!!! 🙂

---

<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:** [May 15, 2019, 2:19am UTC](https://discuss.elastic.co/t/add-weekend-field-via-ingest-pipeline/177039/4 "2019-05-15T02:19:56Z")

</div>

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