# Sorting for price with special offers

**URL:** <https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750>\
**Category:** Elasticsearch\
**Created:** [May 27, 2014, 11:46am UTC](https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750 "2014-05-27T11:46:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_kal](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@chris\_kal](https://discuss.elastic.co/u/chris_kal)\
**Post date:** [May 27, 2014, 11:46am UTC](https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750/1 "2014-05-27T11:46:51Z")

</div>

Hi,

we use an index with products that have a default price. For certain  
periods of time a product can have a special price.

The mapping looks like this at the moment:

```
"mappings" : {
    "products" : {
        "properties" : {
            "defaultPrice" : { "type" : "float" },
            "actionPrice" : {
                "type" : "nested",
                "properties" : {
                    "begin" : { "type" : "date", "format" : 

```

"YYYY-MM-dd" },  
"end" : { "type" : "date", "format" : "YYYY-MM-dd"  
},  
"price" : { "type" : "float" }  
}  
}, ...

The user can sort for the price. So in the query we need the logic that if  
the current time is between a begin and end, then take this price else take  
the defaultPrice. Is that possible? How can I do that?

Thanks

--  
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/8fc717a4-3a3d-4a60-b83b-67f417b47d8d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8fc717a4-3a3d-4a60-b83b-67f417b47d8d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Florentin\_Zorca](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@Florentin\_Zorca](https://discuss.elastic.co/u/Florentin_Zorca)\
**Post date:** [May 27, 2014, 2:51pm UTC](https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750/2 "2014-05-27T14:51:01Z")

</div>

Hi,

I have never tried it, so take my advice with a grain of salt.  
Your problem looks like a good usage of function scoring:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Functions used in scoring can have filters. So theoretically you should  
obtain what you want a first function with a date range filter and the  
value of the time limited price and a second function, without filters  
which has the value of the default price. You should probably use  
"score\_mode": "first" and "boos\_mode": "replace" so that you only sort by  
these functions.

I hope this idea helps a little.

Kind regards,  
Florentin

Am Dienstag, 27. Mai 2014 13:46:52 UTC+2 schrieb Christof Kaleschke:

> Hi,
> 
> we use an index with products that have a default price. For certain  
> periods of time a product can have a special price.
> 
> The mapping looks like this at the moment:
> 
> ```
> "mappings" : {
> "products" : {
> "properties" : {
> "defaultPrice" : { "type" : "float" },
> "actionPrice" : {
> "type" : "nested",
> "properties" : {
> "begin" : { "type" : "date", "format" : 
> 
> ```
> 
> "YYYY-MM-dd" },  
> "end" : { "type" : "date", "format" : "YYYY-MM-dd"  
> },  
> "price" : { "type" : "float" }  
> }  
> }, ...
> 
> The user can sort for the price. So in the query we need the logic that if  
> the current time is between a begin and end, then take this price else take  
> the defaultPrice. Is that possible? How can I do that?
> 
> Thanks

--  
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/219804cb-6cdb-41cc-bbb8-ddd61eda4076%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/219804cb-6cdb-41cc-bbb8-ddd61eda4076%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Florentin\_Zorca](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@Florentin\_Zorca](https://discuss.elastic.co/u/Florentin_Zorca)\
**Post date:** [May 27, 2014, 2:56pm UTC](https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750/3 "2014-05-27T14:56:33Z")

</div>

Hi Christof,

Another Idea, more like a brute force approach: keep a "current\_price"  
field and update it daily with a cron job, either with the default price or  
with the promotional price. This way you only need to change a very little  
in your queries (sort by the new field) and just add a job to "fix the  
prices" daily.

This should be simpler solution than changing your queries to  
function\_score.

Florentin

Am Dienstag, 27. Mai 2014 13:46:52 UTC+2 schrieb Christof Kaleschke:

> Hi,
> 
> we use an index with products that have a default price. For certain  
> periods of time a product can have a special price.
> 
> The mapping looks like this at the moment:
> 
> ```
> "mappings" : {
> "products" : {
> "properties" : {
> "defaultPrice" : { "type" : "float" },
> "actionPrice" : {
> "type" : "nested",
> "properties" : {
> "begin" : { "type" : "date", "format" : 
> 
> ```
> 
> "YYYY-MM-dd" },  
> "end" : { "type" : "date", "format" : "YYYY-MM-dd"  
> },  
> "price" : { "type" : "float" }  
> }  
> }, ...
> 
> The user can sort for the price. So in the query we need the logic that if  
> the current time is between a begin and end, then take this price else take  
> the defaultPrice. Is that possible? How can I do that?
> 
> Thanks

--  
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/9dc4d381-151e-46f6-9f96-1372dee92e42%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9dc4d381-151e-46f6-9f96-1372dee92e42%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, 1:26am UTC](https://discuss.elastic.co/t/sorting-for-price-with-special-offers/17750/4 "2017-07-06T01:26:40Z")

</div>

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


