# Access a script field from another script

**URL:** <https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360>\
**Category:** Elasticsearch\
**Created:** [July 9, 2012, 12:40pm UTC](https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360 "2012-07-09T12:40:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_King](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_king/32/2631_2.png) [@William\_King](https://discuss.elastic.co/u/William_King)\
**Post date:** [July 9, 2012, 12:40pm UTC](https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360/1 "2012-07-09T12:40:50Z")

</div>

I'm trying to use a computed geo distance in several places in my search  
(for example, to sort by and to filter by). To avoid calculating it several  
times I wanted to use a script field to calculate and hold the distance and  
then access it in other places in my search. Is there a better way of doing  
this? And, if not, is it currently possible to do this?

For reference, I tried using a geo distance sort and a cached geo distance  
filter and found that with both search times went up considerably.

---

<div class="post-metadata">

**Author:** ![William\_King](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_king/32/2631_2.png) [@William\_King](https://discuss.elastic.co/u/William_King)\
**Post date:** [July 9, 2012, 12:49pm UTC](https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360/2 "2012-07-09T12:49:40Z")

</div>

It looks like putting my sorts AFTER my query like so:

{  
"query" : { ... },  
"sort" : { ... }  
}

makes a difference in query times when using 2 geo distance calculations.  
Is there any reason for this? I was thinking that perhaps since the sort is  
after my cached geo distance query the cache was being using on the second  
geo distance calculation in the sort like so:  
{  
"query" : { geo distance \<-- CACHED },  
"sort" : { geo distance \<-- CACHE USED }

}

versus the sort on the top

{  
"sort" : { geo distance \<-- NO CACHE }  
"query" : { geo distance \<-- CACHED }  
}

On Monday, July 9, 2012 5:40:50 AM UTC-7, William King wrote:

> I'm trying to use a computed geo distance in several places in my search  
> (for example, to sort by and to filter by). To avoid calculating it several  
> times I wanted to use a script field to calculate and hold the distance and  
> then access it in other places in my search. Is there a better way of doing  
> this? And, if not, is it currently possible to do this?
> 
> For reference, I tried using a geo distance sort and a cached geo distance  
> filter and found that with both search times went up considerably.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 13, 2012, 8:51am UTC](https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360/3 "2012-07-13T08:51:57Z")

</div>

On Mon, 2012-07-09 at 05:49 -0700, William King wrote:

> It looks like putting my sorts AFTER my query like so:

The sort process only happens after all docs are returned and scored.  
Including the sort parameters inside sub-queries has no meaning.

clint

> {  
> "query" : { ... },  
> "sort" : { ... }  
> }
> 
> makes a difference in query times when using 2 geo distance  
> calculations. Is there any reason for this? I was thinking that  
> perhaps since the sort is after my cached geo distance query the cache  
> was being using on the second geo distance calculation in the sort  
> like so:  
> {  
> "query" : { geo distance \<-- CACHED },  
> "sort" : { geo distance \<-- CACHE USED }
> 
> }
> 
> versus the sort on the top
> 
> {  
> "sort" : { geo distance \<-- NO CACHE }  
> "query" : { geo distance \<-- CACHED }  
> }
> 
> On Monday, July 9, 2012 5:40:50 AM UTC-7, William King wrote:  
> I'm trying to use a computed geo distance in several places in  
> my search (for example, to sort by and to filter by). To avoid  
> calculating it several times I wanted to use a script field to  
> calculate and hold the distance and then access it in other  
> places in my search. Is there a better way of doing this? And,  
> if not, is it currently possible to do this?
> 
> ```
> For reference, I tried using a geo distance sort and a cached
> geo distance filter and found that with both search times went
> up considerably.
> 
> ```

---

<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, 3:20am UTC](https://discuss.elastic.co/t/access-a-script-field-from-another-script/8360/4 "2017-07-06T03:20:23Z")

</div>


