# Decay scores based on value of a script field

**URL:** <https://discuss.elastic.co/t/decay-scores-based-on-value-of-a-script-field/38982>\
**Category:** Elasticsearch\
**Created:** [January 12, 2016, 12:55pm UTC](https://discuss.elastic.co/t/decay-scores-based-on-value-of-a-script-field/38982 "2016-01-12T12:55:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Pawsey](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Patrick\_Pawsey](https://discuss.elastic.co/u/Patrick_Pawsey)\
**Post date:** [January 12, 2016, 12:55pm UTC](https://discuss.elastic.co/t/decay-scores-based-on-value-of-a-script-field/38982/1 "2016-01-12T12:55:23Z")

</div>

Is it possible to decay the scores of documents based on the value of a script field?

I've got an index of documents which include an array of dates as unixtimestamps

`"dates": [1447372800, 1447459200, 1447545600, 1447718400, 1447804800, 1447891200]`

and I've created a groovy script to return the first timestamp which is greater than or equal to today

`soonestFutureDate = doc['dates'].find { it >= todayTimestamp}; return soonestFutureDate;`

which returns a 'fields' object when queried

`"fields": { "soonestFutureDate": [1447372800] }`

This is the value (the soonest future date) which I'm trying to decay the document scores with.

I've tried to access fields.sooonestFutureDate in a function score query

`{ "query": { "function_score": { "functions": [{ "gauss": { "fields.soonestFutureDate": { "origin": "0", "scale": "20" } } }], "query": { "match_all": {} }, "score_mode": "multiply" } } }`

but get a query parsing error in return

`nested: QueryParsingException[[di_events] Unknown field [fields.soonestFutureDate]]`

I've also tried to use a script\_score to return the value of soonestFutureDate, and then access that score in the decay function

`{ "query": { "function_score": { "functions": [{ "script_score": { "script" : "sort_by_date", "params" : { "factor" : 1442448000 } } }, { "gauss": { "_score": { "origin": "0", "scale": "20" } } }], "query": { "match_all": {} }, "score_mode": "multiply" } } }`

which also returns a query parsing error

`nested: QueryParsingException[[di_events] Unknown field [_score]]; }]"`

So I am clearly doing it wrong! Is what I'm trying to do possible?

Thanks.

---

<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 5, 2017, 11:25pm UTC](https://discuss.elastic.co/t/decay-scores-based-on-value-of-a-script-field/38982/2 "2017-07-05T23:25:12Z")

</div>


