Sorting facets

Note

For basic examples, see Using faceted search.

If you want to sort facets in a different order from the rest of the response, use the facet.sort key. The specified sorting order applies to all facets enclosed within the object.

To overwrite facet.sort for a particular facet, add a sort object to that facet, as the Using different sorting for different facets examples shows.

The following examples continue the getting facet values from each facet type example.

Sorting all facets by text alphabetically

The following shows an example of sorting all facets by text. In this example, you request three facets from each <facet_type> and then sort all facets alphabetically.

Here is the request:

RequestResponse
{
  "facet": {
    "all": true,
    "max": 3,
    "sort": {
      "type": "text",
      "order": "desc"
    }
  }
}

Here is the response:

RequestResponse
{
  "ts": 1480977544,
  "rid": "response_id_fo1",
  "facet": {
    "colors": {
      "value": [
        {"id": "color_id_blue", "text": "Blue", "count": 11},
        {"id": "color_id_red", "text": "Red", "count": 23},
        {"id": "color_id_yellow", "text": "Yellow", "count": 10}
      ]  
    },
    "product_type": {
      "value": [
        {"id": "prod_type_id_earring", "text": "Earring", "count": 535},
        {"id": "prod_type_id_necklace", "text": "Necklace", "count": 45},
        {"id": "prod_type_id_ring", "text": "Ring", "count": 105}
      ]
    },
    "primary_stone_type": {
      "value": [
        {"id": "stone_id_crystal", "text": "Crystal", "count": 779},
        {"id": "stone_id_diamond", "text": "Diamond", "count": 2374},
        {"id": "stone_id_emerald", "text": "Emerald", "count": 50}
      ]
    },
    "primary_metal_type": {
      "value": [
        {"id": "stone_id_aluminum", "text": "Aluminum", "count": 4},
        {"id": "stone_id_brass", "text": "Brass", "count": 80},
        {"id": "stone_id_gold", "text": "Gold", "count": 45}
      ]
    }
  },
  "facet_names": ["colors", "product_type", "primary_stone_type", "primary_metal_type"]
}

Using different sorting for different facets

The following shows a request that uses different types of sorting for different facets. Here, you want to:

  • Sort colors facets by count in descending order

  • Sort all other facets by text in ascending order

  • Get the total for all facet types.

In this example, you request three facets from each <facet_type> . Then, you sort the colors facets by count in descending order.

Here is the request:

RequestResponse
{
  "facet": {
    "all": true,
    "max": 3,
    "total": true,
    "sort": {
      "type": "text",
      "order": "desc"
    },
    "colors": {
      "sort": {
        "type": "count",
        "order": "desc"
      }
    }
  }
}

Here is the response:

RequestResponse
{
  "ts": 1480977544,
  "rid": "response_id_f02",
  "facet": {
    "colors": {
      "total": 11, 
      "value": [
        {"id": "color_id_red", "text": "Red", "count": 23},
        {"id": "color_id_blue", "text": "Blue", "count": 11},
        {"id": "color_id_yellow", "text": "Yellow", "count": 10}
      ]  
    },
    "product_type": {
      "total": 30,
      "value": [
        {"id": "prod_type_id_earring", "text": "Earring", "count": 535},
        {"id": "prod_type_id_necklace", "text": "Necklace", "count": 45},
        {"id": "prod_type_id_ring", "text": "Ring", "count": 105}
      ]
    },
    "primary_stone_type": {
      "total": 80,
      "value": [
        {"id": "stone_id_crystal", "text": "Crystal", "count": 779},
        {"id": "stone_id_diamond", "text": "Diamond", "count": 2374},
        {"id": "stone_id_emerald", "text": "Emerald", "count": 50}
      ]
    },
    "primary_metal_type": {
      "total": 36,
      "value": [
        {"id": "stone_id_aluminum", "text": "Aluminum", "count": 4},
        {"id": "stone_id_brass", "text": "Brass", "count": 80},
        {"id": "stone_id_gold", "text": "Gold", "count": 45}
      ]
    }
  },
  "facet_names": ["colors", "product_type", "primary_stone_type", "primary_metal_type"]
}

Do you have some feedback for us?

If you have suggestions for improving this article,