# Retrieve only array elements matching a predicate

**URL:** <https://forum.opensearch.org/t/retrieve-only-array-elements-matching-a-predicate/27943>\
**Category:** OpenSearch\
**Created:** [March 20, 2026, 3:14pm UTC](https://forum.opensearch.org/t/retrieve-only-array-elements-matching-a-predicate/27943 "2026-03-20T15:14:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danilopiazza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/danilopiazza/32/9971_2.png) [@danilopiazza](https://forum.opensearch.org/u/danilopiazza)\
**Post date:** [March 20, 2026, 3:14pm UTC](https://forum.opensearch.org/t/retrieve-only-array-elements-matching-a-predicate/27943/1 "2026-03-20T15:14:18Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):

- OpenSearch version: 3.5.0
- Server OS version: Amazon Linux 2023.10.20260302
- Opensearch Dashboards version: 3.5.0

**Describe the issue** : Given an array of object in my search results, how can I extract only the elements matching a given condition?

- Steps to reproduce
  - Index a document, creating a new index at the same time  
`PUT testindex1/_doc/100`  
`{`  
` "patients": [`  
` {"name" : "John Doe", "age" : 56, "smoker" : true},`  
` {“name” : "Mary Major", "age" : 85, "smoker" : false}`  
` ]`  
`}`
  - Run a query, returning one (or more) hits:  
GET testindex1/\_search  
`{`  
` "query": {`  
` "term": {`  
` "patients.smoker": {`  
` "value": true`  
` }`  
` }`  
` }`  
`}`
  - Get some hits:  
`{`  
` ...,`  
` "hits": {`  
` ...,`  
` "hits: [`  
` {`  
` ...,`  
` "_source": {`  
` "patients": [`  
` {`  
` "name": "John Doe",`  
` "age": 56,`  
` "smoker": true`  
` },`  
` {`  
` "name": "Mary Major",`  
` "age": 85,`  
` "smoker": false`  
` }`  
` ]`  
` }`  
` }`  
` ]`  
` }`  
`}`

Is there any way to filter the `"patients"` array in the returned search hits, for example by only showing the array elements having `patients.smoker = true`?

In my specific example, this is the same condition used as a search term (I would be okay with a solution which imposed a similar restriction).

**Configuration** : I started my OpenSearch instance from a Docker image:  
`docker run -d --name opensearch -p 9200:9200 -e 'DISABLE_SECURITY_PLUGIN=true' -e 'discovery.type=single-node' opensearchproject/opensearch`

---

<div class="post-metadata">

**Author:** ![Anthony](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/anthony/32/9939_2.png) [@Anthony](https://forum.opensearch.org/u/Anthony)\
**Post date:** [March 23, 2026, 10:42am UTC](https://forum.opensearch.org/t/retrieve-only-array-elements-matching-a-predicate/27943/2 "2026-03-23T10:42:04Z")

</div>

@danilopiazza Welcome to the forum and thanks for the question.

Have you explored using `script_fields`, see example query below:

```auto
GET testindex1/_search
{
  "query": {
    "term": {
      "patients.smoker": {
        "value": true
      }
    }
  },
  "_source": false,
  "script_fields": {
    "matching_patients": {
      "script": {
        "lang": "painless",
        "source": """
          def result = [];
          for (def p : params._source['patients']) {
            if (p['smoker'] == true) {
              result.add(p);
            }
          }
          return result;
        """
      }
    }
  }
}

```
