我对类型感有点陌生,但我想做的是获取所有类别以及每个类别的产品数量,我认为这可以通过使用facet来完成,对吧?
假设我的产品文档架构如下:
{
"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
}
]
}
所以这里每个产品可以属于一个或多个类别(如果产品所属的类别是子类别,则意味着该产品也属于父类别)并且categories
字段设置为facet
我想要获取每个类别中的产品数量的所有类别。
当我运行这样的搜索查询时:
client.collections('products').documents().search(
{
"q" : "*",
"facet_by" : "categories.id",
"per_page" : 0 # I do not need products just the facet part
}
)
typesense 返回随机 9 个方面字段,如下所示:
"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
]
]
]
但在集合中有更多类别,我做错了什么?我怎样才能获得所有类别的产品数量,或者不可能获得方面?
UPD:我还有一个选项是与产品相关的过滤器 ID 列表,我想要一个单独的查询来执行相同的操作(获取具有产品数量的所有过滤器),这可能是另一个故事,只是解释为什么它在那里。