# Pre-fetch min and max price field for filtering a query

**URL:** <https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [February 21, 2019, 10:06am UTC](https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370 "2019-02-21T10:06:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsliamjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itsliamjones/32/41183_2.png) [@itsliamjones](https://discuss.elastic.co/u/itsliamjones)\
**Post date:** [February 21, 2019, 10:06am UTC](https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370/1 "2019-02-21T10:06:10Z")

</div>

Hi,

I'm looking at adding mix-max price sliders to our search instance and need to find out the mix and max price available for the initial query.

In Elasticsearch I have the min, max, and stat aggregations that allow me to pre-fetch this data before filtering. Can't find anything similar for Swiftype?

Note: Our catalogue is pretty large and the prices range from pennies to thousands, due to this I can't make a best guess at min and max.

Thanks,

Liam

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [February 21, 2019, 1:33pm UTC](https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370/2 "2019-02-21T13:33:42Z")

</div>

Hi @itsliamjones. To the best of my knowledge, we do not have anything to explicitly fetch a min and max.

One suggestion would be to try a `multi_search` query, sorting on your field, both `asc` and `desc`. Not the most elegant solution but it should work.

Here's an example of finding a min and max for the `visitors` field in our sample data set:

```auto
curl -X POST \
  https://host-2376rb.api.swiftype.com/api/as/v1/engines/sample-engine/multi_search \
  -H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
    "queries": [
        {
          "query": "",
          "sort": {
            "visitors": "desc"
          },
          "page": {
              "size": 1
          },
          "result_fields": {
            "visitors": {
              "raw": {}
            }
          }
        },
        {
          "query": "",
          "sort": {
            "visitors": "asc"
          },
          "page": {
              "size": 1
          },
          "result_fields": {
            "visitors": {
              "raw": {}
            }
          }
        }
    ]
}'

```

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [February 21, 2019, 4:31pm UTC](https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370/3 "2019-02-21T16:31:01Z")

</div>

If you're not concerned so much about exact precision and you have some reasonable estimates for upper and lower bounds, you could use a Range Facet to determine the min and max in a single query.

You'd need to iterate through the resulting facets and look at the first and last facet with count \> 0. You could then use the "from" value of the first matching facet as the min, and the "to" value of the last matching facet as the max.

```auto
curl -X POST \
  https://host-2376rb.api.swiftype.com/api/as/v1/engines/national-parks-demo/search \
  -H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
  "query": "",
  "page": {
	"size": 0
  },
  "facets": {
    "visitors": [
      {
        "type": "range",
        "ranges": [
          {
        	"from": 0,
    		"to": 10000
          },
          {
        	"from": 10000,
    		"to": 100000
          },
          {
        	"from": 100000,
    		"to": 500000
          },
          {
        	"from": 500000,
    		"to": 1000000
          },
          {
        	"from": 1000000,
    		"to": 5000000
          },
          {
        	"from": 5000000,
    		"to": 20000000
          },
          {
        	"from": 20000000,
    		"to": 100000000
          }
        ]
      }
    ]
  }
}'

```

---

<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:** [March 21, 2019, 4:31pm UTC](https://discuss.elastic.co/t/pre-fetch-min-and-max-price-field-for-filtering-a-query/169370/4 "2019-03-21T16:31:10Z")

</div>

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