Script filtering by nested object size problem


#1

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

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

$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


#2

This seems to be a bug. When using script filter

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


(Ryan Ernst) #3

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.


#4

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

 ctx._source 

it returned null pointer exception
and

params[ '_source' ] 

returned empty result


(Ryan Ernst) #5

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?


#6

The script above is search script


(system) #7

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