You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by David Svånå <da...@gmail.com> on 2017/07/25 13:48:21 UTC

Json facet sort by subfacet

Hi,

According to http://yonik.com/solr-facet-functions/, we can sort on "any
facet function that appears in each bucket":

$ curl http://localhost:8983/solr/query -d 'q=*:*&
 json.facet={
   categories:{
     type : terms,
     field : cat,
     sort : "x desc",   // can also use sort:{x:desc}
     facet:{
       x : "avg(price)",
       y : "sum(price)"
     }
   }
 }
'

But is it possible to sort by a subfacet's value? Note that I'm not talking
about sorting each subfacet bucket's content, rather sorting the 'cat'
terms by its subfacet results... Example (this doesn't work but should show
my intent):

$ curl http://localhost:8983/solr/query -d 'q=*:*&
 json.facet={
   categories:{
     type : terms,
     field : cat,
     sort:{x.numBuckets:asc},
     facet:{
       x:{
         type:terms,
         field:subcat,
         numBuckets:true
       }
     }
   }
 }
'

Re: Json facet sort by subfacet

Posted by Susheel Kumar <su...@gmail.com>.
Not that i am aware of to sort by numBuckets but how much difference it
make if you sort by count. For e.g. below result is sorted by inner count
and numBuckets in this example has same order.


curl http://localhost:8983/solr/techproducts/query -d 'q=*:*&rows=0&
              json.facet={
                            categories:{
                              type : terms,
                              field : cat,
                              sort : {count :  desc},
                              facet:{
                                manuex:{
                                type : terms,
                                field : manu_id_s,
                                numBuckets: true
                                }
                              }
                            }
                          }'


Result
===
"facets":{
    "count":32,
    "categories":{
      "buckets":[{
          "val":"electronics",
          "count":12,
          "manuex":{
            "numBuckets":8,
            "buckets":[{
                "val":"corsair",
                "count":3},
              {
                "val":"belkin",
                "count":2},
              {
                "val":"canon",
                "count":2},
              {
                "val":"apple",
                "count":1},
              {
                "val":"asus",
                "count":1},
              {
                "val":"ati",
                "count":1},
              {
                "val":"maxtor",
                "count":1},
              {
                "val":"samsung",
                "count":1}]}},
        {
          "val":"currency",
          "count":4,
          "manuex":{
            "numBuckets":4,
            "buckets":[{
                "val":"boa",
                "count":1},
              {
                "val":"eu",
                "count":1},
              {
                "val":"nor",
                "count":1},
              {
                "val":"uk",
                "count":1}]}},
        {
          "val":"memory",
          "count":3,
          "manuex":{
            "numBuckets":1,
            "buckets":[{
                "val":"corsair",
                "count":3}]}},
        {
          "val":"connector",
          "count":2,
          "manuex":{
            "numBuckets":1,
            "buckets":[{
                "val":"belkin",
                "count":2}]}},
        {
          "val":"graphics card",
          "count":2,
          "manuex":{
            "numBuckets":2,
            "buckets":[{
                "val":"asus",
                "count":1},
              {
                "val":"ati",
                "count":1}]}},
        {
          "val":"hard drive",
          "count":2,
          "manuex":{
            "numBuckets":2,
            "buckets":[{
                "val":"maxtor",
                "count":1},
              {
                "val":"samsung",
                "count":1}]}},
        {
          "val":"search",
          "count":2,
          "manuex":{
            "numBuckets":0,
            "buckets":[]}},
        {
          "val":"software",
          "count":2,
          "manuex":{
            "numBuckets":0,
            "buckets":[]}},
        {
          "val":"camera",
          "count":1,
          "manuex":{
            "numBuckets":1,
            "buckets":[{
                "val":"canon",
                "count":1}]}},
        {
          "val":"copier",
          "count":1,
          "manuex":{
            "numBuckets":1,
            "buckets":[{
                "val":"canon",
                "count":1}]}}]}}}

On Tue, Jul 25, 2017 at 9:48 AM, David Svånå <da...@gmail.com> wrote:

> Hi,
>
> According to http://yonik.com/solr-facet-functions/, we can sort on "any
> facet function that appears in each bucket":
>
> $ curl http://localhost:8983/solr/query -d 'q=*:*&
>  json.facet={
>    categories:{
>      type : terms,
>      field : cat,
>      sort : "x desc",   // can also use sort:{x:desc}
>      facet:{
>        x : "avg(price)",
>        y : "sum(price)"
>      }
>    }
>  }
> '
>
> But is it possible to sort by a subfacet's value? Note that I'm not talking
> about sorting each subfacet bucket's content, rather sorting the 'cat'
> terms by its subfacet results... Example (this doesn't work but should show
> my intent):
>
> $ curl http://localhost:8983/solr/query -d 'q=*:*&
>  json.facet={
>    categories:{
>      type : terms,
>      field : cat,
>      sort:{x.numBuckets:asc},
>      facet:{
>        x:{
>          type:terms,
>          field:subcat,
>          numBuckets:true
>        }
>      }
>    }
>  }
> '
>