# Using script fileds in custom score queries

**URL:** <https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898>\
**Category:** Elasticsearch\
**Created:** [August 30, 2012, 2:57pm UTC](https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898 "2012-08-30T14:57:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drcivan](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@drcivan](https://discuss.elastic.co/u/drcivan)\
**Post date:** [August 30, 2012, 2:57pm UTC](https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898/1 "2012-08-30T14:57:39Z")

</div>

Hi,

I have a calculated script field, similar to following example:

"script\_fields" : {  
"script\_field" : {  
"script" : "doc['my\_field\_name'].value \* factor",  
"params" : {  
"factor" : 2.0  
}  
}  
}

I want to use this script field, in my custom score query like:

"custom\_score" : {  
"query" : {  
....  
},  
"script" : "script\_field \* 2" // ?  
}

I can't refer the calculated variable as "script\_field" or  
"doc['script\_field'].value", both are throwing errors.  
Is there any way to reuse the script fields in custom scoring queries?

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [August 30, 2012, 3:11pm UTC](https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898/2 "2012-08-30T15:11:57Z")

</div>

No. As far as I understand "script\_fields" are like "fields",  
"highlight" fields, they are all created pretty much after all document  
(IDs) have been rounded up. Therefore they are not something you can  
refer to in a query or filter, so you can't use them as a 'macro'.

Given that, as it says on the customer score query page, you can put the  
script and parameter values, as you wrote right in your query.

-Paul

On 8/30/2012 7:57 AM, drcivan wrote:

> Hi,
> 
> I have a calculated script field, similar to following example:
> 
> "script\_fields" : {  
> "script\_field" : {  
> "script" : "doc['my\_field\_name'].value \* factor",  
> "params" : {  
> "factor" : 2.0  
> }  
> }  
> }
> 
> I want to use this script field, in my custom score query like:
> 
> "custom\_score" : {  
> "query" : {  
> ....  
> },  
> "script" : "script\_field \* 2" // ?  
> }
> 
> I can't refer the calculated variable as "script\_field" or  
> "doc['script\_field'].value", both are throwing errors.  
> Is there any way to reuse the script fields in custom scoring queries?

--

---

<div class="post-metadata">

**Author:** ![drcivan](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@drcivan](https://discuss.elastic.co/u/drcivan)\
**Post date:** [August 30, 2012, 3:37pm UTC](https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898/3 "2012-08-30T15:37:35Z")

</div>

Thank you Paul. I wasn just trying to avoid the duplicate calculation  
of script. But as you say, there is no other way to do it.

On 30 Ağustos, 18:12, "P.Hill" [parehi...@gmail.com](mailto:parehi...@gmail.com) wrote:

> No. As far as I understand "script\_fields" are like "fields",  
> "highlight" fields, they are all created pretty much after all document  
> (IDs) have been rounded up. Therefore they are not something you can  
> refer to in a query or filter, so you can't use them as a 'macro'.
> 
> Given that, as it says on the customer score query page, you can put the  
> script and parameter values, as you wrote right in your query.
> 
> -Paul
> 
> On 8/30/2012 7:57 AM, drcivan wrote:
> 
> > Hi,
> 
> > I have a calculated script field, similar to following example:
> 
> > "script\_fields" : {  
> > "script\_field" : {  
> > "script" : "doc['my\_field\_name'].value \* factor",  
> > "params" : {  
> > "factor" : 2.0  
> > }  
> > }  
> > }
> 
> > I want to use this script field, in my custom score query like:
> 
> > "custom\_score" : {  
> > "query" : {  
> > ....  
> > },  
> > "script" : "script\_field \* 2" // ?  
> > }
> 
> > I can't refer the calculated variable as "script\_field" or  
> > "doc['script\_field'].value", both are throwing errors.  
> > Is there any way to reuse the script fields in custom scoring queries?

--

---

<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:14am UTC](https://discuss.elastic.co/t/using-script-fileds-in-custom-score-queries/8898/4 "2017-07-06T03:14:31Z")

</div>


