# Server-side ("preloaded") script return value

**URL:** https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218
**Category:** Elasticsearch
**Created:** [November 1, 2013, 4:36pm UTC](https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218 "2013-11-01T16:36:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rastro](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@rastro](https://discuss.elastic.co/u/rastro)
#### Post date: [November 1, 2013, 4:36pm UTC](https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218/1 "2013-11-01T16:36:45Z")

</div>

I have a query that calls a python script. That part is working finally,  
which is confirmed by the script's output to a log file.

Now, I need to get the value back to the query, but I only ever see  
"my\_field" : null  
in the response.

How am I supposed to return the value from the script to the query? I'm  
currently sending it back on stdout.

```
    "script_fields" : {
                    "my_field": {
                            "script": "testscript",
                            "params": {
                                    "field": "value1",
                                    "tag" : "value2"
                            },
                            "type": "float",
                            "lang": "python"
                    }
    }

```

But the response is always:

{  
"\_index" : "logstash-2013.11.01",  
"\_type" : "app",  
"\_id" : "rV5JRvZySaS6gwdT6pPvyw",  
"\_score" : 0.36469176, "\_source" : {...},  
"fields" : {  
"my\_field" : null  
}  
}

The end of the python script:

sys.stdout.write(ret)  
sys.stdout.write("\n")  
sys.stdout.flush()

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)
#### Post date: [November 4, 2013, 5:41pm UTC](https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218/2 "2013-11-04T17:41:06Z")

</div>

Hi,  
script fields are meant to create on-the-fly new fields which can be  
returned for each document that matches a query. Can you elaborate on why  
you need to send the result of the script back to the query?

On Friday, November 1, 2013 5:36:45 PM UTC+1, rastro wrote:

> I have a query that calls a python script. That part is working finally,  
> which is confirmed by the script's output to a log file.
> 
> Now, I need to get the value back to the query, but I only ever see  
> "my\_field" : null  
> in the response.
> 
> How am I supposed to return the value from the script to the query? I'm  
> currently sending it back on stdout.
> 
> ```
> "script_fields" : {
> "my_field": {
> "script": "testscript",
> "params": {
> "field": "value1",
> "tag" : "value2"
> },
> "type": "float",
> "lang": "python"
> }
> }
> 
> ```
> 
> But the response is always:
> 
> {  
> "\_index" : "logstash-2013.11.01",  
> "\_type" : "app",  
> "\_id" : "rV5JRvZySaS6gwdT6pPvyw",  
> "\_score" : 0.36469176, "\_source" : {...},  
> "fields" : {  
> "my\_field" : null  
> }  
> }
> 
> The end of the python script:
> 
> sys.stdout.write(ret)  
> sys.stdout.write("\n")  
> sys.stdout.flush()

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![rastro](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@rastro](https://discuss.elastic.co/u/rastro)
#### Post date: [November 6, 2013, 4:52pm UTC](https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218/3 "2013-11-06T16:52:39Z")

</div>

Luca pointed out that ES calls python with an 'eval', so multi-line scripts  
won't work on the server side, either. There's some info in this jython  
ticket:

```
      http://bugs.jython.org/issue1713 .

```

I switched to javascript and am able to get the field back for facets and  
script\_fields.

Thanks, Luca.

On Monday, November 4, 2013 9:41:06 AM UTC-8, Luca Cavanna wrote:

> Hi,  
> script fields are meant to create on-the-fly new fields which can be  
> returned for each document that matches a query. Can you elaborate on why  
> you need to send the result of the script back to the query?
> 
> On Friday, November 1, 2013 5:36:45 PM UTC+1, rastro wrote:
> 
> > I have a query that calls a python script. That part is working finally,  
> > which is confirmed by the script's output to a log file.
> > 
> > Now, I need to get the value back to the query, but I only ever see  
> > "my\_field" : null  
> > in the response.
> > 
> > How am I supposed to return the value from the script to the query? I'm  
> > currently sending it back on stdout.
> > 
> > ```
> > "script_fields" : {
> > "my_field": {
> > "script": "testscript",
> > "params": {
> > "field": "value1",
> > "tag" : "value2"
> > },
> > "type": "float",
> > "lang": "python"
> > }
> > }
> > 
> > ```
> > 
> > But the response is always:
> > 
> > {  
> > "\_index" : "logstash-2013.11.01",  
> > "\_type" : "app",  
> > "\_id" : "rV5JRvZySaS6gwdT6pPvyw",  
> > "\_score" : 0.36469176, "\_source" : {...},  
> > "fields" : {  
> > "my\_field" : null  
> > }  
> > }
> > 
> > The end of the python script:
> > 
> > sys.stdout.write(ret)  
> > sys.stdout.write("\n")  
> > sys.stdout.flush()

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [July 6, 2017, 2:08am UTC](https://discuss.elastic.co/t/server-side-preloaded-script-return-value/14218/4 "2017-07-06T02:08:31Z")

</div>


