# Using script\_field loses all indexed data

**URL:** <https://discuss.elastic.co/t/using-script-field-loses-all-indexed-data/11671>\
**Category:** Elasticsearch\
**Created:** [April 23, 2013, 5:21pm UTC](https://discuss.elastic.co/t/using-script-field-loses-all-indexed-data/11671 "2013-04-23T17:21:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_Raminfar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amir_raminfar/32/2268_2.png) [@Amir\_Raminfar](https://discuss.elastic.co/u/Amir_Raminfar)\
**Post date:** [April 23, 2013, 5:21pm UTC](https://discuss.elastic.co/t/using-script-field-loses-all-indexed-data/11671/1 "2013-04-23T17:21:25Z")

</div>

I am trying to use elasticsearch with script\_field to return distance in  
miles. The distance works but when I use script\_field, all the data is lost  
that would have been returned if I didn't use script\_field. So here is an  
example. If I run:

{  
"script\_fields": {  
"distance": {  
"script": "doc['lat\_lon'].distanceInKm(0,50.3)"  
}  
},  
"query": {  
"match\_all": {}  
}  
}

The returned data is  
{  
took: 1,  
timed\_out: false,  
\_shards: {  
total: 5,  
successful: 5,  
failed: 0  
},  
hits: {  
total: 1,  
max\_score: 1,  
hits: [  
{  
\_index: tilofy\_test\_channels,  
\_type: channel,  
\_id: 1,  
\_score: 1,  
fields: {  
distance: 19629.025705718916  
}  
}  
]  
}  
}

However, if I remove script\_fields like the following:  
{  
"query": {  
"match\_all": {}  
}  
}

Then I get the correct data which is  
{  
took: 0,  
timed\_out: false,  
\_shards: {  
total: 5,  
successful: 5,  
failed: 0,  
}  
hits: {  
total: 1,  
max\_score: 1,  
hits: [  
{  
\_index: "tilofy\_test\_channels",  
\_type: "channel",  
\_id: 1,  
\_score: 1,  
\_source: {  
created\_at: "2013-04-23T15:50:54Z",  
description: "Odit nam vitae facere velit eum  
similique. Voluptatem sed voluptas necessitatibus architecto molestiae  
voluptates possimus. Quo necessitatibus doloribus aut. Omnis nam non  
ratione. Nihil harum esse eaque sunt error consequuntur quia quasi."  
id: 1,  
image\_file\_name: null,  
latitude: 37.3320132,  
longitude: -122.0289113,  
name: "Repellat inventore architecto et similique quo  
in.",  
lat\_lon: {  
lat: 37.3320132,  
lon: -122.0289113  
}  
}  
}  
]  
}  
}

What am I doing wrong? I don't think this behavior make sense to me. Has  
anybody else ran to this problem?

--  
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:39am UTC](https://discuss.elastic.co/t/using-script-field-loses-all-indexed-data/11671/2 "2017-07-06T02:39:55Z")

</div>


