# Reuse/cache script fields to use other parts of the query

**URL:** <https://discuss.elastic.co/t/reuse-cache-script-fields-to-use-other-parts-of-the-query/167371>\
**Category:** Elasticsearch\
**Created:** [February 7, 2019, 12:14am UTC](https://discuss.elastic.co/t/reuse-cache-script-fields-to-use-other-parts-of-the-query/167371 "2019-02-07T00:14:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anam](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anam](https://discuss.elastic.co/u/anam)\
**Post date:** [February 7, 2019, 12:14am UTC](https://discuss.elastic.co/t/reuse-cache-script-fields-to-use-other-parts-of-the-query/167371/1 "2019-02-07T00:14:00Z")

</div>

I am working on a project where we need to show distance the listings are from a user location. To show the distance, when lat/lon is given in the input, we calculate distance using a script field called "distance"

```
"script_fields" : {
        "distance" : {
            "script" : {
                "lang": "painless",
                "source": "Math.round((doc['location'].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
                "params" : {
                    "lat" : -33.8152,
                    "lon" : 151.0012
                }
            }
        }
    }

```

However, for relevancy search, we want to rank the nearest listing higher and we are calculating the distance again inside the function score query. This is inefficient. I have searched the internet to find a solution but no luck.

**Is there any way to reuse a script field in the other parts of the query or filter or sorting?**

Full Query:

```
GET /listings/_doc/_search
{
    "_source" : true,
    "query": {
        "function_score": {
            "score_mode": "sum",
            "query": { "match": {
          "source_id": 1
        } },
            "functions": [
              {
                "script_score": {
                  "script": {
                    "params": {
                      "dealer": {"a": 5, "b": 6},
                      "photo": {"a": 4, "b": 5},
                      "location": {"lat": -33.8152, "lon": 151.0012}
                    },
                    "source": "(doc['location'].planeDistanceWithDefault(params.location.lat, params.location.lon, 1000) * 0.001 < 25 ? 200000 : 0) + (doc['is_dealer'].value == 1 ? Math.pow(params.dealer.a, params.dealer.b) : 0) + (doc['hasPhoto'].value == 1 ? Math.pow(params.photo.a, params.photo.b) : 0)"
                  }
                }
              }
            ]
        }
    },
    "script_fields" : {
        "distance" : {
            "script" : {
                "lang": "painless",
                "source": "Math.round((doc['location'].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
                "params" : {
                    "lat" : -33.8152,
                    "lon" : 151.0012
                }
            }
        }
    }
}
```

---

<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:** [March 7, 2019, 12:14am UTC](https://discuss.elastic.co/t/reuse-cache-script-fields-to-use-other-parts-of-the-query/167371/2 "2019-03-07T00:14:07Z")

</div>

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