# Assistance with Choosing the Correct Query Structure

**URL:** https://discuss.elastic.co/t/assistance-with-choosing-the-correct-query-structure/23271
**Category:** Elasticsearch
**Created:** [April 16, 2015, 9:44am UTC](https://discuss.elastic.co/t/assistance-with-choosing-the-correct-query-structure/23271 "2015-04-16T09:44:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Dyball](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@David\_Dyball](https://discuss.elastic.co/u/David_Dyball)
#### Post date: [April 16, 2015, 9:44am UTC](https://discuss.elastic.co/t/assistance-with-choosing-the-correct-query-structure/23271/1 "2015-04-16T09:44:35Z")

</div>

Hi All,

TL;DR: Doing dynamic currency conversion via function\_score works great for  
scenarios where I want to sort by prices,  
but I want the same functionality in queries that will be sorted by  
relevance score when using terms while still retaining  
a dynamically calulated field for converted price.

Long version:

I'm looking to deploy Elasticsearch as a primary search engine for a  
catalog of products. Each product has a  
provider\_price and provider\_currency field. The "provider\_price" is always  
in the currency specified by  
"provider\_currency". When a user gets their search results they want to  
display it in their local currency. To  
do this I've come up with two different queries depending on how the  
results will be sorted for the end user.

1. If the user requests a price-based sorting (asc/desc) then I'm using a  
function\_score query like so:

{  
"query": {  
"function\_score": {  
"functions": [  
"script\_score": {  
"script": "( rates[\_source.provider\_currency] \*  
\_source.provider\_price )",  
"params": {  
"rates": {  
"USD": 1,  
"AUD": 0.75,  
...  
...  
...  
}  
}  
}  
],  
"query": {  
"filtered": {  
........  
}  
}  
},  
"sort": {  
{  
"\_score": "asc/desc"  
}  
}  
}

1. When sorting by terms or other fields in the document I still need to  
calculate the converted price, so I'm using script\_fields like so:

{  
"fields": [  
"\_source"  
],  
"query": {  
......  
},  
"script\_fields": {  
"user\_price": {  
"script": "( rates[\_source.provider\_currency] \*  
\_source.provider\_price )",  
"params": {  
"rates": {  
"USD": 1,  
"AUD": 0.75,  
...  
...  
...  
}  
}  
}  
},  
"sort": [  
{  
"\_score": "desc"  
}  
]  
}

A few queries with the above:

1. Does this generally seem like a sane implementation?

2. These both work perfectly for doing on-the-fly conversions of currency  
and returing it with the documents,  
but at the moment my app-side code has to make the distinction  
between price-sorting queries and term  
queries and issue the correct one, as well extract the converted  
price from the correct field depending  
(\_score or \_source.user\_price respectively).

Cheers for any assistance the community can provide.

David.

--  
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/aef94d97-dad6-43c4-866f-af749101ce6a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/aef94d97-dad6-43c4-866f-af749101ce6a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![David\_Dyball](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@David\_Dyball](https://discuss.elastic.co/u/David_Dyball)
#### Post date: [April 16, 2015, 9:47am UTC](https://discuss.elastic.co/t/assistance-with-choosing-the-correct-query-structure/23271/2 "2015-04-16T09:47:59Z")

</div>

Sorry, I missed out "boost\_mode": "replace" in my function\_score example  
above. I want the score to be the exact converted  
currency, so I can make use of it in code.

On Thursday, April 16, 2015 at 10:44:35 AM UTC+1, David Dyball wrote:

> Hi All,
> 
> TL;DR: Doing dynamic currency conversion via function\_score works great  
> for scenarios where I want to sort by prices,  
> but I want the same functionality in queries that will be sorted by  
> relevance score when using terms while still retaining  
> a dynamically calulated field for converted price.
> 
> Long version:
> 
> I'm looking to deploy Elasticsearch as a primary search engine for a  
> catalog of products. Each product has a  
> provider\_price and provider\_currency field. The "provider\_price" is  
> always in the currency specified by  
> "provider\_currency". When a user gets their search results they want to  
> display it in their local currency. To  
> do this I've come up with two different queries depending on how the  
> results will be sorted for the end user.
> 
> 1. If the user requests a price-based sorting (asc/desc) then I'm using a  
> function\_score query like so:
> 
> {  
> "query": {  
> "function\_score": {  
> "functions": [  
> "script\_score": {  
> "script": "( rates[\_source.provider\_currency] \*  
> \_source.provider\_price )",  
> "params": {  
> "rates": {  
> "USD": 1,  
> "AUD": 0.75,  
> ...  
> ...  
> ...  
> }  
> }  
> }  
> ],  
> "query": {  
> "filtered": {  
> ........  
> }  
> }  
> },  
> "sort": {  
> {  
> "\_score": "asc/desc"  
> }  
> }  
> }
> 
> 1. When sorting by terms or other fields in the document I still need to  
> calculate the converted price, so I'm using script\_fields like so:
> 
> {  
> "fields": [  
> "\_source"  
> ],  
> "query": {  
> ......  
> },  
> "script\_fields": {  
> "user\_price": {  
> "script": "( rates[\_source.provider\_currency] \*  
> \_source.provider\_price )",  
> "params": {  
> "rates": {  
> "USD": 1,  
> "AUD": 0.75,  
> ...  
> ...  
> ...  
> }  
> }  
> }  
> },  
> "sort": [  
> {  
> "\_score": "desc"  
> }  
> ]  
> }
> 
> A few queries with the above:
> 
> 1. Does this generally seem like a sane implementation?
> 
> 2. These both work perfectly for doing on-the-fly conversions of  
> currency and returing it with the documents,  
> but at the moment my app-side code has to make the distinction  
> between price-sorting queries and term  
> queries and issue the correct one, as well extract the converted  
> price from the correct field depending  
> (\_score or \_source.user\_price respectively).
> 
> Cheers for any assistance the community can provide.
> 
> David.

--  
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/4fcdbf81-a081-4eb9-a292-35a506ee9c65%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4fcdbf81-a081-4eb9-a292-35a506ee9c65%40googlegroups.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, 12:19am UTC](https://discuss.elastic.co/t/assistance-with-choosing-the-correct-query-structure/23271/3 "2017-07-06T00:19:21Z")

</div>


