# Preserving \_score when using function\_score with boost\_mode="replace"

**URL:** https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937
**Category:** Elasticsearch
**Created:** [September 24, 2015, 6:51pm UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937 "2015-09-24T18:51:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rlvoyer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rlvoyer/32/4928_2.png) [@rlvoyer](https://discuss.elastic.co/u/rlvoyer)
#### Post date: [September 24, 2015, 6:51pm UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937/1 "2015-09-24T18:51:53Z")

</div>

Hi folks,

I am successfully using function\_score to customize result scoring. Presently, I am setting `boost_mode="replace"`. But I'm wondering if it's possible to preserve the internal ES `_score` field in someway before doing that replacement. I thought I could achieve this by adding a `script_field` script that simply returns `_score`, but it seems as though I cannot reference `_score` from a `script_field` script:

`ExpressionScriptCompilationException[Unknown variable [_score] in expression]`

Is there another way to achieve this?

Thanks for your help!

Best,  
Robert

---

<div class="post-metadata">

### Author: ![rlvoyer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rlvoyer/32/4928_2.png) [@rlvoyer](https://discuss.elastic.co/u/rlvoyer)
#### Post date: [September 28, 2015, 4:53pm UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937/2 "2015-09-28T16:53:28Z")

</div>

Hi folks,

Here's some additional context around this... We are combining the internal `_score` with a few other scores produced with `function_score` scripts. We are adding all of those scores together (hence the use of boost\_mode="replace"). For debugging purposes, as well as learning a more optimal way to combine each of the `function_score` outputs, we would like to (optionally) return each of the values used as the basis of scoring. So I am wondering if there's a way to copy the `_score` field to another field before replacing the internal score with the output of the `function_score` computation.

Any insight is greatly appreciated.

Best,  
Robert

---

<div class="post-metadata">

### Author: ![softwaredoug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/softwaredoug/32/22681_2.png) [@softwaredoug](https://discuss.elastic.co/u/softwaredoug)
#### Post date: [September 29, 2015, 9:33am UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937/3 "2015-09-29T09:33:17Z")

</div>

I don't think that's currently possible. Related, by colleague wanted to multiply the scores of two queries, here's the [best we got](http://stackoverflow.com/questions/31755642/how-can-i-multiply-the-score-of-two-queries-together-in-elasticsearch) as an answer. At the risk of being tarred and feathered, Solr's function queries currently allow more flexibility in this area (as shown in that stackoverflow answer). Solr's function queries though can be a royal pain to work with compared to writing an honest-to-goodness script that ES gives you.

Sorry that's not a real answer, but hopefully someone will chime in because I would also like to do more with function\_score\_queries 😄

---

<div class="post-metadata">

### Author: ![rlvoyer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rlvoyer/32/4928_2.png) [@rlvoyer](https://discuss.elastic.co/u/rlvoyer)
#### Post date: [October 2, 2015, 4:12pm UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937/4 "2015-10-02T16:12:39Z")

</div>

Thanks for your response, Doug. Sounds like it might be worth digging into the ES source to see if I can contribute this myself.

---

<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:47pm UTC](https://discuss.elastic.co/t/preserving--score-when-using-function-score-with-boost-mode-replace/29937/5 "2017-07-05T23:47:01Z")

</div>


