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

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

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:

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": {}
            }
          }
        }
    ]
}'

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.

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
          }
        ]
      }
    ]
  }
}'

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