You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@solr.apache.org by Thomas Redman <th...@gmail.com> on 2023/04/04 14:29:50 UTC

Date Range Facet sort order

I am using the SolrJ Json facet API to retrieve a facet on a range of date.

            DateFacet df = (DateFacet)f;
            RangeFacetMap rfm = new RangeFacetMap(df.getField(), df.getStartDate(), df.getEndDate(),
                    df.getSpan().getKeyword())
                    .setMinCount(1);

I see no way to change the sort order to index (I want the output in date order), I get results sorted on the count. I have tried adding the sort field like so:

            rfm.put("sort","count asc");

but that had no effect. How does one change the sort order of the date range facets using the Json facet API?

Re: Date Range Facet sort order

Posted by Chris Hostetter <ho...@fucit.org>.
: Still one question, is there a way to change the sort order for date 
: range facets? Or do I do that on my end? There are some cases where I 
: want to see results in count order, other cases where I want to see 
: results sort on the button range.

There is no server side option to change the sort of range facets, 
precisely because it is just as easy/efficient to do it on the client 
side.

(Solr has/had a general design ethos of "Only do on the servier side 
things that are not possible, or as efficient, to do on the client side)

field faceting supports sorting because it's not possible (or efficient) 
to do on the client side (w/o fetching every possible term bucket) but 
query & range faceting already return all the buckets to the client, so 
there isn't much point in solr doing the soting -- it's just added time 
spent holding open that socket, and burning "expensive" (limited) Solr CPU 
vs more easily scalable client CPU>



-Hoss
http://www.lucidworks.com/

Re: Date Range Facet sort order

Posted by Thomas Redman <th...@gmail.com>.
Yes indeed, my bad, I didn’t describe this correctly… at all. 

Still one question, is there a way to change the sort order for date range facets? Or do I do that on my end? There are some cases where I want to see results in count order, other cases where I want to see results sort on the button range.

> On Apr 4, 2023, at 3:12 PM, Chris Hostetter <ho...@fucit.org> wrote:
> 
> 
> : I see no way to change the sort order to index (I want the output in 
> : date order), I get results sorted on the count. I have tried adding the 
> : sort field like so:
> 
> This ... doesn't make sense.  
> 
> AFAIK "index" order (or more specifically: The "natural order" of the 
> bucket values) is the only possible sorting returned by JSON Range Facet 
> 
> ie: what you are asking for should be what you are getting, what you say 
> you are getting shouldn't be possible.
> (see examples below)
> 
> 
> What does your solrj code that consumes the QueryResponse look like?
> 
> 
> $ curl 'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d '
> {
>  "query": "*:*",
>  "facet": {
>    "prices": {
>      "type": "range",
>      "field": "price",
>      "start": 0,
>      "end": 100,
>      "gap": 20
>    }
>  }
> }'
> {
>  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
>  },
>  "facets":{
>    "count":32,
>    "prices":{
>      "buckets":[{
>          "val":0.0,
>          "count":5},
>        {
>          "val":20.0,
>          "count":0},
>        {
>          "val":40.0,
>          "count":0},
>        {
>          "val":60.0,
>          "count":1},
>        {
>          "val":80.0,
>          "count":1}]}}}
> 
> 
> $ curl 
> 'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d'
> {
>  "query": "*:*",
>  "facet": {
>    "prices": {
>      "type": "range",
>      "field": "manufacturedate_dt",
>      "start": "1900-01-01T00:00:00Z",
>      "end": "2200-01-01T00:00:00Z",
>      "gap": "+100YEARS"
>    }
>  }
> }'
> {
>  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
>  },
>  "facets":{
>    "count":32,
>    "prices":{
>      "buckets":[{
>          "val":"1900-01-01T00:00:00Z",
>          "count":0},
>        {
>          "val":"2000-01-01T00:00:00Z",
>          "count":11},
>        {
>          "val":"2100-01-01T00:00:00Z",
>          "count":0}]}}}
> 
> Here's a test that uses SollrJ to assert that the execpted buckets come 
> back in the expected order (by bucket value, not by bucket count) ...
> 
> https://github.com/apache/solr/blob/releases/solr/9.0.0/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java#L573-L596
> 
> 
> 
> 
> 
> 
> -Hoss
> http://www.lucidworks.com/


Re: Date Range Facet sort order

Posted by Thomas Redman <th...@gmail.com>.
Yes indeed, my bad, I didn’t describe this correctly… at all. 

Still one question, is there a way to change the sort order for date range facets? Or do I do that on my end? There are some cases where I want to see results in count order, other cases where I want to see results sort on the button range.

> On Apr 4, 2023, at 3:12 PM, Chris Hostetter <ho...@fucit.org> wrote:
> 
> 
> : I see no way to change the sort order to index (I want the output in 
> : date order), I get results sorted on the count. I have tried adding the 
> : sort field like so:
> 
> This ... doesn't make sense.  
> 
> AFAIK "index" order (or more specifically: The "natural order" of the 
> bucket values) is the only possible sorting returned by JSON Range Facet 
> 
> ie: what you are asking for should be what you are getting, what you say 
> you are getting shouldn't be possible.
> (see examples below)
> 
> 
> What does your solrj code that consumes the QueryResponse look like?
> 
> 
> $ curl 'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d '
> {
>  "query": "*:*",
>  "facet": {
>    "prices": {
>      "type": "range",
>      "field": "price",
>      "start": 0,
>      "end": 100,
>      "gap": 20
>    }
>  }
> }'
> {
>  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
>  },
>  "facets":{
>    "count":32,
>    "prices":{
>      "buckets":[{
>          "val":0.0,
>          "count":5},
>        {
>          "val":20.0,
>          "count":0},
>        {
>          "val":40.0,
>          "count":0},
>        {
>          "val":60.0,
>          "count":1},
>        {
>          "val":80.0,
>          "count":1}]}}}
> 
> 
> $ curl 
> 'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d'
> {
>  "query": "*:*",
>  "facet": {
>    "prices": {
>      "type": "range",
>      "field": "manufacturedate_dt",
>      "start": "1900-01-01T00:00:00Z",
>      "end": "2200-01-01T00:00:00Z",
>      "gap": "+100YEARS"
>    }
>  }
> }'
> {
>  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
>  },
>  "facets":{
>    "count":32,
>    "prices":{
>      "buckets":[{
>          "val":"1900-01-01T00:00:00Z",
>          "count":0},
>        {
>          "val":"2000-01-01T00:00:00Z",
>          "count":11},
>        {
>          "val":"2100-01-01T00:00:00Z",
>          "count":0}]}}}
> 
> Here's a test that uses SollrJ to assert that the execpted buckets come 
> back in the expected order (by bucket value, not by bucket count) ...
> 
> https://github.com/apache/solr/blob/releases/solr/9.0.0/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java#L573-L596
> 
> 
> 
> 
> 
> 
> -Hoss
> http://www.lucidworks.com/


Re: Date Range Facet sort order

Posted by Chris Hostetter <ho...@fucit.org>.
: I see no way to change the sort order to index (I want the output in 
: date order), I get results sorted on the count. I have tried adding the 
: sort field like so:

This ... doesn't make sense.  

AFAIK "index" order (or more specifically: The "natural order" of the 
bucket values) is the only possible sorting returned by JSON Range Facet 

ie: what you are asking for should be what you are getting, what you say 
you are getting shouldn't be possible.
(see examples below)


What does your solrj code that consumes the QueryResponse look like?


$ curl 'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d '
{
  "query": "*:*",
  "facet": {
    "prices": {
      "type": "range",
      "field": "price",
      "start": 0,
      "end": 100,
      "gap": 20
    }
  }
}'
{
  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
  },
  "facets":{
    "count":32,
    "prices":{
      "buckets":[{
          "val":0.0,
          "count":5},
        {
          "val":20.0,
          "count":0},
        {
          "val":40.0,
          "count":0},
        {
          "val":60.0,
          "count":1},
        {
          "val":80.0,
          "count":1}]}}}


$ curl 
'http://localhost:8983/solr/techproducts/query?rows=0&omitHeader=true' -d'
{
  "query": "*:*",
  "facet": {
    "prices": {
      "type": "range",
      "field": "manufacturedate_dt",
      "start": "1900-01-01T00:00:00Z",
      "end": "2200-01-01T00:00:00Z",
      "gap": "+100YEARS"
    }
  }
}'
{
  "response":{"numFound":32,"start":0,"numFoundExact":true,"docs":[]
  },
  "facets":{
    "count":32,
    "prices":{
      "buckets":[{
          "val":"1900-01-01T00:00:00Z",
          "count":0},
        {
          "val":"2000-01-01T00:00:00Z",
          "count":11},
        {
          "val":"2100-01-01T00:00:00Z",
          "count":0}]}}}

Here's a test that uses SollrJ to assert that the execpted buckets come 
back in the expected order (by bucket value, not by bucket count) ...

https://github.com/apache/solr/blob/releases/solr/9.0.0/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java#L573-L596






-Hoss
http://www.lucidworks.com/