# Check for field's existence in script processor ingest node

**URL:** <https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171>\
**Category:** Elasticsearch\
**Created:** [September 19, 2018, 5:44pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171 "2018-09-19T17:44:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![meetdave2611997](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@meetdave2611997](https://discuss.elastic.co/u/meetdave2611997)\
**Post date:** [September 19, 2018, 5:44pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/1 "2018-09-19T17:44:48Z")

</div>

Hello,

I am using ingest node for processing documents and I am using script processor for processing documents. Now my requirement is to check whether the field exists and based on existence process goes further.

I am using below code but it's not working.

```
if (ctx.field1) {
     // process 1
}else{
    // process 2
}

```

`field1` is not present in all document. some of the documents may not have that field.  
How can i check whether field exists in current doc or not?

Thanks,  
Meet

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 19, 2018, 6:17pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/2 "2018-09-19T18:17:45Z")

</div>

Currently `ctx` is a `Map`, so you can use `containsKey`:

```auto
if (ctx.containsKey('field1')) {
     // process 1
}else{
    // process 2
}

```

---

<div class="post-metadata">

**Author:** ![meetdave2611997](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@meetdave2611997](https://discuss.elastic.co/u/meetdave2611997)\
**Post date:** [September 19, 2018, 7:34pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/3 "2018-09-19T19:34:21Z")

</div>

@rjernst

I tried the above solution but didn't work. Here is my script

```
POST _scripts/threat_preference-script
{
  "script":{
    "lang":"painless",
    "source":"""
    if(ctx.containsKey('threat.id')){
      ctx.threat_preference = "With Threat"
    }else{
      ctx.threat_preference = "Without Threat"
    }
    """
  }
}

```

Here is my ingest pipeline

```
POST _ingest/pipeline/test
{
"description": "Pipeline to process scripted fields.",
"processors": [
    {
    "script": {
        "lang": "painless",
        "on_failure": [
        {
            "set": {
            "field": "threat_preference-error",
            "value": "{{ _ingest.on_failure_message }}"
            }
        }
        ],
        "id": "threat_preference-script"
    }
    }
]
}

```

After ingesting some document, I see each document contains `Without threat` value in `threat_preference` field even though `threat.id` field exist

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 19, 2018, 10:01pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/4 "2018-09-19T22:01:06Z")

</div>

Unfortunately in this case, ctx is a map of maps, so using the full path to the field will not work. You will need to check both maps, eg:

```auto
if (ctx.containsKey('threat')) {
  if (ctx['threat'].containsKey('id')) {
    // has threat.id field
  }
}

```

---

<div class="post-metadata">

**Author:** ![meetdave2611997](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@meetdave2611997](https://discuss.elastic.co/u/meetdave2611997)\
**Post date:** [September 20, 2018, 4:50pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/5 "2018-09-20T16:50:35Z")

</div>

@rjernst Thank you very much. That worked!

---

<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:** [October 18, 2018, 4:50pm UTC](https://discuss.elastic.co/t/check-for-fields-existence-in-script-processor-ingest-node/149171/6 "2018-10-18T16:50:52Z")

</div>

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