# Check if id exist in array field or not using script\_fields

**URL:** <https://discuss.elastic.co/t/check-if-id-exist-in-array-field-or-not-using-script-fields/266453>\
**Category:** Elasticsearch\
**Created:** [March 6, 2021, 9:06pm UTC](https://discuss.elastic.co/t/check-if-id-exist-in-array-field-or-not-using-script-fields/266453 "2021-03-06T21:06:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aninda\_halder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aninda_halder/32/68707_2.png) [@aninda\_halder](https://discuss.elastic.co/u/aninda_halder)\
**Post date:** [March 6, 2021, 9:06pm UTC](https://discuss.elastic.co/t/check-if-id-exist-in-array-field-or-not-using-script-fields/266453/1 "2021-03-06T21:06:09Z")

</div>

Hi,  
i like to get a script field if a specific value exist in a array .

Here is my mapping -

```auto
"mappings": {
    "properties": {
        "field1": {
            "type": "text",
        },
        "field2": {
            "type": "text",
        },
    }
}

```

Here is my example documents -

```auto
{
    "field1": "hello 1",
    "field2": ["id1","id3"]
}

{
    "field1": "hello 2",
    "field2": ["id2","id3"]
}
{
    "field1": "hello 3",

}
{
    "field1": "mobile 1",
    "field2": ["id1","id4"]
}

```

Here is my search query -

```auto
{
    "query": {
        "match" : {
            "title": "hello"
        }
    },
    "script_fields":{
        "testField2":{
            "script": {
                "source":"doc['field2'].value == params.id ? true : false",
                "params": {
                    "id": "id1"
                }
            }
        }
    }

}

```

what i want to achieve is to get all those document which has "hello" in their title and add a custom field (testField2) in every doc . the value of that custom field would be True if "field2" contain a specific id ("id1") else False

---

<div class="post-metadata">

**Author:** ![aninda\_halder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aninda_halder/32/68707_2.png) [@aninda\_halder](https://discuss.elastic.co/u/aninda_halder)\
**Post date:** [March 8, 2021, 9:08am UTC](https://discuss.elastic.co/t/check-if-id-exist-in-array-field-or-not-using-script-fields/266453/2 "2021-03-08T09:08:16Z")

</div>

i find the the solution in [stackoverflow](https://stackoverflow.com/a/66516260/8422313) 😃

---

<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:** [April 5, 2021, 9:09am UTC](https://discuss.elastic.co/t/check-if-id-exist-in-array-field-or-not-using-script-fields/266453/3 "2021-04-05T09:09:08Z")

</div>

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