# Script filtering by nested object size problem

**URL:** <https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622>\
**Category:** Elasticsearch\
**Created:** [March 22, 2017, 5:07pm UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622 "2017-03-22T17:07:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![psu](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@psu](https://discuss.elastic.co/u/psu)\
**Post date:** [March 22, 2017, 5:07pm UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/1 "2017-03-22T17:07:51Z")

</div>

I have a personal messages system. Each message contains MEMBERS nested object

```auto
"MEMBERS" : {
            "type" : "nested",
            "properties" : {
              "ID" : {
                "type" : "long"
              },
              "LEFT" : {
                "type" : "long"
              },
              "NICK" : {
                "type" : "keyword"
              },
              "READ" : {
                "type" : "long"
              }
            }
          },

```

For system notifications MEMBERS is an array of one item - destination user. The goal is to exclude system notifications ( display only messages with MEMBERS.size()\>1 ). I'm trying to use the following query with script filter but the result is empty. It also does not work even for doc.containsKey("MEMBERS") like there is no document containing that field ( there are 1000x of them )  
PHP code:

```auto
$q['query'] = array( 'bool' => array(
			'must' => array(
				'nested' => array(
					'path' => 'MEMBERS',
					'query' => array(
						'bool' => array(
							'must' => array(
								'term' => array(
									'MEMBERS.ID' => $uid
								)
							),
							'filter' => array(
								'script' => array(
									'script' => array(
										'inline' => 'if( doc.containsKey("MEMBERS") && doc["MEMBERS"].size()>1 ) return true'
									)
								)
							)
						)
					)
				),
			)
		));

```

Please advise.  
I'm using v5.1.x

---

<div class="post-metadata">

**Author:** ![psu](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@psu](https://discuss.elastic.co/u/psu)\
**Post date:** [March 23, 2017, 10:07am UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/2 "2017-03-23T10:07:32Z")

</div>

This seems to be a bug. When using script filter

```auto
doc.containsKey('LAST_MESSAGE_DATE')

```

(where LAST\_MESSAGE\_DATE type is long) query returns documents, but if I replace it with MEMBERS (which is nested field) result is empty

---

<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:** [March 23, 2017, 4:05pm UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/3 "2017-03-23T16:05:41Z")

</div>

Search scripts do not have access to nested fields. You would need to access the \_source to see if the field existed (which is an expensive operation to do on every doc matching the query for a script filter). If you want to see if the nested field exists, it would be better to do this during ingestion. You could use an ingest processor to add a boolean field which indicates whether the source contains anything for the nested field.

---

<div class="post-metadata">

**Author:** ![psu](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@psu](https://discuss.elastic.co/u/psu)\
**Post date:** [March 29, 2017, 9:25am UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/4 "2017-03-29T09:25:42Z")

</div>

Could you provide a syntax to access \_source in the script above? I've tried

```auto
 ctx._source 

```

it returned null pointer exception  
and

```auto
params['_source'] 

```

returned empty result

---

<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:** [March 31, 2017, 4:45am UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/5 "2017-03-31T04:45:52Z")

</div>

> Could you provide a syntax to access \_source in the script above?

In what type of script? Are you doing an update script, or an ingest script?

---

<div class="post-metadata">

**Author:** ![psu](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@psu](https://discuss.elastic.co/u/psu)\
**Post date:** [April 21, 2017, 9:21am UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/6 "2017-04-21T09:21:05Z")

</div>

The script above is search script

---

<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:** [May 19, 2017, 9:34am UTC](https://discuss.elastic.co/t/script-filtering-by-nested-object-size-problem/79622/7 "2017-05-19T09:34:25Z")

</div>

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