Return only one element from strings array in elasticsearch Return only one element from strings array in elasticsearch elasticsearch elasticsearch

Return only one element from strings array in elasticsearch


Scripting is only directly possible here.To get an idea on how to use scripting in aggregations, you can refer this blog.

Something like below should work

for(element in doc['strArray'].values){      if(element.startsWith('browser')){          return element;      }};return null;

Both sorting and filtering is done on document level and not element level.On element level if you can make this array as nested , filtering is possible. That is first you need to change the structure to -

strArray: [  { "name" : 'browser:IE' } ,   { "name" : 'device:PC' }  ]

And then make the strArray field as nested.In that case you can do a nested filter based on prefix query ( Using query filter ) and then , do a nested aggregation on the data.