typesense

typesense facet products by categories


I'm a bit new to type sense, but what I want to do is to get all the categories with the number of products for each category, I think it can be done with the usage of facet, right?

Let's say my product document schema is like this:

{
   "name" : "products",
   "fields" : [
       {
           "name" : "id",
           "type" : "string"
       },
       {
           "name": "name",
           "type": "string"
       }, 
       {
           "name": "categories",
           "type": "object[]",
           "facet": true,
           "fields" : [
               {
                   "name":"id",
                   "type": "string"
               },
               {
                   "name":"label",
                   "type": "string"
               }
           ]
       },
       {
           "name": "options",
           "type": "string[]",
           "facet": true
       }
      
   ] 
}

so here each product can belong to one or to multiple categories (if category product belong to is subcategory that means the product also belongs to parent category) and categories field set to be facet as I want to get all the categories with the amount of products in each.

When I'm running search query like this:

client.collections('products').documents().search(
   {
      "q" : "*",
      "facet_by" : "categories.id",
      "per_page" : 0 # I do not need products just the facet part
   }
)

typesense returns random 9 facet fields like this:

"facet_counts" => array:1 [
    0 => array:4 [
      "counts" => array:10 [
        0 => array:3 [
          "count" => 106581
          "highlighted" => "ab58454c-def8-431f-ad78-b435e390d095"
          "value" => "ab58454c-def8-431f-ad78-b435e390d095"
        ]
        1 => array:3 [
          "count" => 106580
          "highlighted" => "1b4dfa36-c66d-4cc0-a61d-3fee6b878299"
          "value" => "1b4dfa36-c66d-4cc0-a61d-3fee6b878299"
        ]
        2 => array:3 [
          "count" => 106550
          "highlighted" => "fe1633c0-7cd6-4f7c-88ed-f6b9de34d94d"
          "value" => "fe1633c0-7cd6-4f7c-88ed-f6b9de34d94d"
        ]
        3 => array:3 [
          "count" => 106547
          "highlighted" => "9e794fee-b3fd-4f83-a41c-cbf629bec8cf"
          "value" => "9e794fee-b3fd-4f83-a41c-cbf629bec8cf"
        ]
        4 => array:3 [
          "count" => 106543
          "highlighted" => "b94f2647-f850-48b3-861e-d9fd97cfe577"
          "value" => "b94f2647-f850-48b3-861e-d9fd97cfe577"
        ]
        5 => array:3 [
          "count" => 106542
          "highlighted" => "96e281f4-3f63-411a-a517-0250e2491cf5"
          "value" => "96e281f4-3f63-411a-a517-0250e2491cf5"
        ]
        6 => array:3 [
          "count" => 106530
          "highlighted" => "455a8b50-2dc5-4683-837e-8119217b9ee1"
          "value" => "455a8b50-2dc5-4683-837e-8119217b9ee1"
        ]
        7 => array:3 [
          "count" => 106523
          "highlighted" => "7215ec95-db47-4c90-b915-349fa05191d1"
          "value" => "7215ec95-db47-4c90-b915-349fa05191d1"
        ]
        8 => array:3 [
          "count" => 106507
          "highlighted" => "59171caf-cc00-4b7b-92dd-0da54b925a95"
          "value" => "59171caf-cc00-4b7b-92dd-0da54b925a95"
        ]
        9 => array:3 [
          "count" => 106506
          "highlighted" => "4128454d-2c26-4ed4-b194-021426a5f99e"
          "value" => "4128454d-2c26-4ed4-b194-021426a5f99e"
        ]
      ]
      "field_name" => "categories.id"
      "sampled" => false
      "stats" => array:1 [
        "total_values" => 1696
      ]
    ]
  ]

but in the collection there are many more categories, what I'm doing wrong? How can I get all the categories with product amounts, or it's not possible with facets?

UPD: I also have option that is a list of product-related filter ids, and I want a separate query to do the same (get all filters with product amounts), it's probably another story, just explaining why it's there.


Solution

  • The number of facet values returned is 10 by default.

    So you want to set max_facet_values: 999999 as a search parameter to get additional facet values.

    This is documented under the table here.