I have two collections representing *products* and...
# community-help
t
I have two collections representing products and product categories. Each category in the categories table has an string[] array of product IDs that are linked to the products table as a reference. When I search on the products table with
filter_by:$categories(id:12345)
, I get the products I want (hurray!), but I want to be able to sort the products based on the order of ids stored in the categories table. I get an error when I try to edit the schema and set
sort: true
on the array reference field, so is there any way to accomplish this? For reference, I’m trying to support manual sort orders for product categories (the sort order having been set on Shopify’s backend).
k
We don't yet support alter / update schema of join collections.
t
Sorry, I should have been more clear. I tried creating a new collection with that field as both a reference and a sort, but I get an error message (presumably because arrays aren’t eligible for sorting). I’m not sure what the workaround would be for what I need
To clarify my question, is there any way to sort my products collection by the order of ids in the JOINed categories id field?
$categories(id)
is type
string[]
, containing an array of product IDs.
h
Hi @Tim Mackey > I get an error when I try to edit the schema and set
sort: true
on the array reference field Can you share the specific error you're getting?
To clarify my question, is there any way to sort my products collection by the order of ids in the JOINed categories id field?
$categories(id)
is type
string[]
, containing an array of product IDs.
https://github.com/typesense/typesense/issues/1582 This is a related issue. Can you provide more details for your use case there?
t
Can you share the specific error you’re getting?
With the following schema attribute:
Copy code
{
  "name": "product_ids",
  "type": "string[]",
  "sort": true
}
I get the error:
Copy code
400: Field `product_ids` cannot be a sortable field.
I think this is just because arrays can’t be sortable (since similar to that issue you posted, which value would you sort on?)
This is a related issue. Can you provide more details for your use case there?
Thanks for the suggestion. My issue is different enough to probably be a separate issue I think. Here’s an example
Copy code
// Products
[
  {id: "1"},
  {id: "2"},
  {id: "3"},
  {id: "4"}
]
// Categories
[
  {
    id: "A"
    category_title: "Featured Products",
    product_ids: ["3","1","2"]
  },
  {
    id: "B"
    category_title: "On Sale",
    product_ids: ["4","1"]
  }
]
If I do a lookup on the products table with _*filter_by*_ set to
$categories(id:A)
, I get a response giving me only the products listed in
product_ids
field for Category “A” (this is correct). What I’d like is to have the products sorted by the order specified in
product_ids
(3,1,2). Typesense doesn’t see that order though, so I the products are returned sorted as (1,2,3). Likewise, if I filter by
$categories(id:B)
, I’d expect to get the products in order (4,1) but I get them in order (1,4).
h
I'm assuming you also need to search on the
Products
collection. If not, you can achieve this by:
Copy code
collection: Categories
q: *
filter_by: id:A
include_fields: $Products(*)
t
It still doesn’t sort in the same order as the product_ids field, but that was a great idea! I think I’ve found a workaround using curation overrides—it’s a bit hacky but so far it seems to be working.