Check if id exist in array field or not using script_fields

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

Here is my mapping -

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

Here is my example documents -

{
    "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 -

{
    "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

i find the the solution in stackoverflow :smiley:

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