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

**URL:** <https://discuss.elastic.co/t/elaticsearch-script-is-it-possible-to-pass-in-field-names-as-paramter/194922>\
**Category:** Elasticsearch\
**Created:** [August 12, 2019, 9:36pm UTC](https://discuss.elastic.co/t/elaticsearch-script-is-it-possible-to-pass-in-field-names-as-paramter/194922 "2019-08-12T21:36:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qiang\_gui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qiang_gui/32/47687_2.png) [@qiang\_gui](https://discuss.elastic.co/u/qiang_gui)\
**Post date:** [August 12, 2019, 9:36pm UTC](https://discuss.elastic.co/t/elaticsearch-script-is-it-possible-to-pass-in-field-names-as-paramter/194922/1 "2019-08-12T21:36:47Z")

</div>

Hi,

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": [  
"Debug.explain(fieldName);",  
" ^---- HERE"  
],

Thanks a lot in advance!

---

<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:** [August 13, 2019, 3:19am UTC](https://discuss.elastic.co/t/elaticsearch-script-is-it-possible-to-pass-in-field-names-as-paramter/194922/2 "2019-08-13T03:19:19Z")

</div>

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](https://www.elastic.co/guide/en/elastic-stack-overview/current/field-level-security.html)?

---

<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:** [September 10, 2019, 3:19am UTC](https://discuss.elastic.co/t/elaticsearch-script-is-it-possible-to-pass-in-field-names-as-paramter/194922/3 "2019-09-10T03:19:20Z")

</div>

Closed on September 10, 2019, 3:19am UTC

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