solrsolr-query-syntax

Range facet with different gaps/steps in Solr


Currently the request is

{
  "query": "*:*",
  "facet": {
    "prices": {
      "type": "range",
      "field": "price",
      "start": 0,
      "end": 100,
      "gap": 20
    }
  }
}

I need to retrieve the counts for the ranges

0 TO 5
5 TO 10
10 TO 20
20 TO 30
30 TO 40
40 TO 50
50 TO 100
100 TO ?(NO END)

How to should be the request to retrieve the counts for these ranges?


Solution

  • The range parameters listing shows you the available parameters and their meaning. To get 5 as the gap, use "gap": 5. To get the entries that lie outside of your ranges, include "other": "after":

      "type": "range",
      "field": "price",
      "start": 0,
      "end": 100,
      "gap": 5,
      "other": "after"
    

    For your custom ranges, you'll need to include the ranges explicitly. You can't use other together with range, but there are ways around that:

      "ranges": [
          {"range": "[0, 5)"},
          {"range": "[5, 10)"},
          {"range": "[10, 20)"},
          {"range": "[20, 30)"},
          {"range": "[30, 40)"},
          {"range": "[40, 50)"},
          {"range": "[50, 100)"},
          {"range": "[100, *]"},
      ]