# Combine nested fields in scripted field

**URL:** <https://discuss.elastic.co/t/combine-nested-fields-in-scripted-field/161500>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [December 19, 2018, 11:29am UTC](https://discuss.elastic.co/t/combine-nested-fields-in-scripted-field/161500 "2018-12-19T11:29:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nele](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nele](https://discuss.elastic.co/u/Nele)\
**Post date:** [December 19, 2018, 11:29am UTC](https://discuss.elastic.co/t/combine-nested-fields-in-scripted-field/161500/1 "2018-12-19T11:29:59Z")

</div>

Hello Community!

I would like to create a scripted field that checks two properties of a nested field.

I have an ES index that contains information about processes. A process is configured as a **nested object**. Each _doc_ can have more than one process. It is also possible to have more than one process of the same kind (same _processDefinitionKey_).

This is how the processes are structured at the moment:

```
"process" : {
  "type" : "nested",
  "include_in_parent" : true,
  "properties" : {
    "created" : {
      "type" : "date",
      "format" : "date_optional_time"
    },
    "ended" : {
      "type" : "date",
      "format" : "date_optional_time"
    },
    "id" : {
      "type" : "text",
      "index" : false
    },
    "processDefinitionKey" : {
      "type" : "keyword"
    },
    "processDefinitionName" : {
      "type" : "text",
      "fields" : {
        "raw" : {
          "type" : "keyword",
          "index" : false
        }
      },
      "analyzer" : "case_insensitive_sort"
    }
  }
}

```

Now I am interested in all active processes of a specific kind. So they must have a specific `processDefinitionKey` and must _not_ have an `ended` property yet. I've written an ES query for it:

```
{
  "query": {
    "nested": {
      "path": "process",
      "query": {
        "bool": {
          "must": {
            "match": {
              "process.processDefinitionKey": "service_agreement"
            }
          },
          "must_not": {
            "exists": {
              "field": "process.ended"
            }
          }
        }
      }
    }
  }
}

```

I have added this query as a filter that I can use on the Kibana dashboards. I think it works as it should.

However, I would prefer to add this property "has an active process of this kind" as a column in a search table by creating a **scripted field** for it. This is the scripted field I have come up with so far (in Painless):

```
doc['process.processDefinitionKey'].contains('service_agreement') && doc['process.ended'].empty

```

But the results here do not align with the query above. I am afraid that this script checks whether there is _a_ process with the correct `processDefinitionKey` and _a_ process without the `ended` property. But it does not check whether both properties are true for the same process.

How can I make sure the scripted field checks both the `processDefinitionKey` and the `ended` property of the same process?

I use Kibana 6.4.2

**EDIT:**  
Example of a document where the scripted field should return `true` (the service\_agreement process has not ended yet, the other process has):

```
{
  "_index": ...,
  "_type"...,
  "_id": ...,
  "_version": ...,
  "_score": ...,
  "_source": {
    "type": ...,
    ...,
    "process": [
      {
        "id": "130707",
        "created": "2017-09-11T09:50:52.000+02:00",
        "ended": "2017-09-13T10:16:43.000+02:00",
        "processDefinitionKey": "so_ondertekenproces",
        "processDefinitionName": "Samenwerkingsovereenkomst",
        "incidents": []
      },
      {
        "id": "c2a83c07-15f7-11e7-a20e-0242ac120004",
        "created": "2017-03-31T11:52:32.000+02:00",
        "processDefinitionKey": "service_agreement",
        "processDefinitionName": "Service Agreement",
        "incidents": []
      }
    ]
  },
  "fields": {
    "process.created": [
      "2017-03-31T09:52:32.000Z",
      "2017-09-11T07:50:52.000Z"
    ],
    "process.ended": [
      "2017-09-13T08:16:43.000Z"
    ]
  }
}

```

Example of a document where the scripted field should return `false`:

```
{
  "_index": ...,
  "_type": ...,
  "_id": ...,
  "_score": ...,
  "_source": {
    "type": ...,
    ...,
    "process": [
      {
        "id": "17154",
        "created": "2017-05-24T13:21:40.000+02:00",
        "ended": "2017-05-24T13:23:24.000+02:00",
        "processDefinitionKey": "so_ondertekenproces",
        "processDefinitionName": "Samenwerkingsovereenkomst",
        "incidents": []
      },
      {
        "id": "17263",
        "created": "2017-05-24T13:23:29.000+02:00",
        "processDefinitionKey": "so_ondertekenproces",
        "processDefinitionName": "Samenwerkingsovereenkomst",
        "incidents": []
      },
      {
        "id": "f1cd056e-15f7-11e7-a20e-0242ac120004",
        "created": "2017-03-31T11:53:51.000+02:00",
        "ended": "2017-10-10T11:35:47.000+02:00",
        "processDefinitionKey": "service_agreement",
        "processDefinitionName": "Service Agreement",
        "incidents": []
      }
    ]
  },
  "fields": {
    "process.created": [
      "2017-03-31T09:53:51.000Z",
      "2017-05-24T11:21:40.000Z",
      "2017-05-24T11:23:29.000Z"
    ],
    "process.ended": [
      "2017-05-24T11:23:24.000Z",
      "2017-10-10T09:35:47.000Z"
    ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [December 20, 2018, 6:55pm UTC](https://discuss.elastic.co/t/combine-nested-fields-in-scripted-field/161500/2 "2018-12-20T18:55:25Z")

</div>

Do you mind providing a sample document that you'd want to return `true` for, as well as a document that you don't want to return `true` for that currently does?

---

<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:** [January 17, 2019, 6:55pm UTC](https://discuss.elastic.co/t/combine-nested-fields-in-scripted-field/161500/3 "2019-01-17T18:55:26Z")

</div>

Closed on January 17, 2019, 6:55pm UTC

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