# Graduated prices on documents

**URL:** https://discuss.elastic.co/t/graduated-prices-on-documents/226378
**Category:** Elasticsearch
**Created:** [April 3, 2020, 9:57am UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378 "2020-04-03T09:57:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Max\_van\_der\_Sluis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_van_der_sluis/32/45572_2.png) [@Max\_van\_der\_Sluis](https://discuss.elastic.co/u/Max_van_der_Sluis)
#### Post date: [April 3, 2020, 9:57am UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/1 "2020-04-03T09:57:10Z")

</div>

I'm having issues implementing graduated prices in Elasticsearch.  
This is the mapping I use (I can change it, as long as it all stays in the same index):

```auto
PUT /sw_shop1_product/_mapping
    {
        "properties": {
        "cs_filter_prices": {
            "properties": {
                "price": {
                    "type": "double"
                },
                "from" : {
                    "type": "long"
                }
            },
            "type": "nested"
        }
      }
    }

```

So I'm adding multiple prices per document, I need to be able to filter based on amount and price.  
The issue is, how can I get the correct documents based on the amount(highest amount that is lower/equal to input amount, and only that one) then also filter on a price ("lte").

Example with 2 products:

```auto
PUT sw_shop1_product/_doc/product_name_1
{ 
  "cs_filter_prices": [ 
    {
      "price": 1.50,
      "from": 1
    },
    {
      "price": 1.25,
      "from": 100
    },
    {
      "price": 1.00,
      "from": 200
    }
  ]
}

```

```auto
PUT sw_shop1_product/_doc/product_name_2
{ 
  "cs_filter_prices": [ 
    {
      "price": 2.50,
      "from": 1
    },
    {
      "price": 2.25,
      "from": 100
    },
    {
      "price": 2.00,
      "from": 200
    }
  ]
}

```

When I want to search for amount 125 and price "lte" 2.00, I should only get the document "product\_name\_1".  
I have been banging my head against this issue for a bit of time, and I have no clue how to do this.

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [April 3, 2020, 11:37am UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/2 "2020-04-03T11:37:53Z")

</div>

You can use a [nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) query for that, something like this:

```auto
GET /sw_shop1_product/_search
{
    "query": {
        "nested" : {
            "path" : "cs_filter_prices",
            "query" : {
                "bool" : {
                    "must" : [
                        { "range" : {"cs_filter_prices.from" : {"gt" : 125}} },
                        { "range" : {"cs_filter_prices.price" : {"lte" : 2.0}} }
                    ]
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![Max\_van\_der\_Sluis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_van_der_sluis/32/45572_2.png) [@Max\_van\_der\_Sluis](https://discuss.elastic.co/u/Max_van_der_Sluis)
#### Post date: [April 3, 2020, 12:07pm UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/3 "2020-04-03T12:07:17Z")

</div>

Yeah I had something similar but the issue is with the from amount, it's user input so it won't match exactly the from tiers.

```auto
{ "range" : {"cs_filter_prices.from" : {"gt" : 125}} }

```

Sorry, I'll be more exact:  
I need it to match the highest `from` amount that is still lower then the requested amount or equal to it. so if `from` is 125 it would match the `from` 100 instead and only that one.  
This is why I can't use "lte", or can I sort/filter the nested query instead to do what I want?

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [April 3, 2020, 1:39pm UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/4 "2020-04-03T13:39:45Z")

</div>

oh I see now what you are trying to do. May be [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-request-body.html#request-body-search-inner-hits) in your case. For example, in this `inner_hits` you will see the highest `from` and only that one.

```auto
GET /sw_shop1_product/_search
{
  "query": {
    "nested": {
      "path": "cs_filter_prices",
      "query": {
        "bool": {
          "must": [
           { "range" : {"cs_filter_prices.from" : {"lte" : 125}} },
           { "range" : {"cs_filter_prices.price" : {"lte" : 2.0}} }
          ]
        }
      },
      "inner_hits": {
        "size": 1,
        "sort": [{"cs_filter_prices.from": "desc"}]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Max\_van\_der\_Sluis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_van_der_sluis/32/45572_2.png) [@Max\_van\_der\_Sluis](https://discuss.elastic.co/u/Max_van_der_Sluis)
#### Post date: [April 3, 2020, 1:59pm UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/5 "2020-04-03T13:59:33Z")

</div>

Thanks a bunch, I'm new to Elasticsearch and still wrapping my head around all these concepts, so connecting the dots is a bit hard for me.  
I'll try it out and see if works. Will report back.

---

<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: [May 1, 2020, 2:11pm UTC](https://discuss.elastic.co/t/graduated-prices-on-documents/226378/6 "2020-05-01T14:11:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
