# Composing function scores

**URL:** <https://discuss.elastic.co/t/composing-function-scores/19498>\
**Category:** Elasticsearch\
**Created:** [August 27, 2014, 4:17pm UTC](https://discuss.elastic.co/t/composing-function-scores/19498 "2014-08-27T16:17:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 27, 2014, 4:17pm UTC](https://discuss.elastic.co/t/composing-function-scores/19498/1 "2014-08-27T16:17:26Z")

</div>

I have a couple of questions regarding function scores. I probably already  
know the answers, but just wanted to double-check with the community. I am  
still on version 1.1.1, so perhaps things have changed since then.

First question is regarding ordering and efficiency. Currently, my query is  
a standard filtered query with a function score surrounding the inner most  
query:

POST /\_search  
{  
"query": {  
"filtered": {  
"query": {  
"function\_score": {  
"query": {  
...  
},  
"script\_score": {  
"script": "..."  
},  
"boost\_mode": "multiply"  
}  
},  
"filter": {  
...  
}  
}  
}  
}

While attempting to fine-tune further my queries, I was wondering if it  
made sense to move the function\_score higher up the chain:

POST /\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"filtered": {  
"query": {  
...  
},  
"filter": {  
...  
}  
}  
},  
"script\_score": {  
"script": "..."  
},  
"boost\_mode": "multiply"  
}  
}  
}

Both work as intended. I would suspect that the efficiency is the same  
since the function is still scoring the same pre-filtered documents. Not  
sure how this change would affect caching, if at all.

Next question is regarding applying different function\_scores. Beside using  
the script\_score, I know also want to use boost functions. Using version  
1.1.1, I cannot apply both script\_scores and functions:

POST /\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"filtered": {  
"query": {  
...  
},  
"filter": {  
...  
}  
}  
},  
"script\_score": {  
"script": "..."  
},  
"functions": [  
{  
"filter": {  
...  
},  
"boost\_factor": 1  
}  
]  
"boost\_mode": "multiply"  
}  
}  
}

Chaining the function scores does work. The original script\_score function  
is now wrapped by the boost functions.

POST /\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"function\_score": {  
"query": {  
"filtered": {  
"query": {  
...  
},  
"filter": {  
...  
}  
}  
},  
"script\_score": {  
"script": "..."  
},  
"boost\_mode": "multiply"  
}  
},  
"functions": [  
{  
"filter": {  
...  
},  
"boost\_factor": 1  
}  
]  
}  
}  
}

Should I have been able to use both types of scripts at the same time? I  
also do not think there will be any difference in the ordering, but did I  
perhaps overlook something?

Cheers,

Ivan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBpX9Ux4AVaBcAVZc3b3cmusR7vKuG85%2BBKwr8yXqwK4w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBpX9Ux4AVaBcAVZc3b3cmusR7vKuG85%2BBKwr8yXqwK4w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:06am UTC](https://discuss.elastic.co/t/composing-function-scores/19498/2 "2017-07-06T01:06:00Z")

</div>

Closed on July 6, 2017, 1:06am UTC


