Elaticsearch script, is it possible to pass in field names as paramter?


We had requirement that based on doc field and doc value, we want to hide/show return field for other column.
for example,
doc 1
name: mac, category : hardware, active=true, manager=Sam
doc 2:
name: windows, category: hardware, active=true, manager=Bill
doc 3:
name: outlook, category: software, active=true, manager=Bill

and when the user search, we want to show/hide manager field based on conditions like category = software or active=true or both, these are dynamically by the user. We still want the record in our result, but we just want to show/hide that particular manager field.

After researching, i found the script_field is the way to go. IF YOU SUGGESTED ALTERNATIVE, PLEASE LET ME KNOW!!
But if put every thing in the query, the performance would be slow and the code is messy.

So, im thinking pass in fieldnames and field values to the script, and
do a loop of on fieldnames for sth like doc[fieldName] = fieldValue, then return empty. otherwise, return the field value.

Is it possible? I tried pass in that, for a script,
but seems the below script always return false, although the if condition is met.
if (doc[fieldName] == fieldValue) return true; return false;

Also, when i tried to use Debug.explain(); it throws exception with no reason, but just following stack trace:
"script_stack": [
" ^---- HERE"

Thanks a lot in advance!

A couple general things about how painless works:

  • doc[fieldName] does not return a field value, but instead an accessor for the field values. You need to call doc[fieldName].value to get a value for the field
  • If you pass fieldName in params for a script, then you can access it with params['fieldName']. You could set this into a local variable with String fieldName = params['fieldName'].

With that all said, what you are doing is already available in the default distribution of Elasticsearch. Have you looked at Field Level Security?

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