You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Algolia实现电商多维度产品搜索查询(品牌/价格等)

Algolia电商产品搜索场景实现方案

背景

我正在电商网站上使用Algolia搭建产品搜索功能,现有如下JSON格式的产品数据:

[{
  "id": 1688,
  "product_detail_id": 2420,
  "name": "BSN AMINO x",
  "brand": "BSN",
  "price": 770000,
  "sku": "530556",
  "variant": "Watermelon 435 gr Exp Date 1-25",
  "slug": "watermelon",
  "stock": 50,
  "image": "https://naturalfarm.sgp1.digitaloceanspaces.com/uploads/products/bsn-amino-x-watermelon-435-gr-exp-date-0524-6535e09243374.jpeg",
  "created_at": "2022-11-11 17:00:55",
  "variant_type": 3,
  "sold_qty": 17,
  "price_disc": 445000,
  "price_disc_vip": 0,
  "price_after_disc": 325000,
  "price_after_disc_vip": 0,
  "badge": "https://naturalfarm.sgp1.digitaloceanspaces.com/uploads/badge/sale.svg",
  "is_member": 0,
  "is_discount": 2,
  "start_date": "2024-04-03",
  "end_date": "2024-04-30",
  "promo_id": 23825,
  "promo_detail_id": 738786,
  "wishlist_status": 0,
  "promo_additional_item": [],
  "rating": 5,
  "rating_float": "5.0"
}]

搜索场景实现

1. 按品牌搜索产品

支持两种方式:全文搜索匹配品牌,或精准过滤品牌字段避免混淆:

// 方式1:全文搜索品牌关键词
index.search('BSN');

// 方式2:精准过滤品牌(适合严格匹配场景)
index.search('', {
  filters: 'brand:"BSN"'
});

2. 按名称和规格搜索产品

直接组合关键词搜索(需确保name和variant在Algolia索引的可搜索属性列表中),也可精准过滤:

// 搜索名称含"AMINO"且规格含"Watermelon"的产品
index.search('AMINO Watermelon');

// 精准过滤(可选)
index.search('', {
  filters: 'name:*AMINO* AND variant:*Watermelon*'
});

3. 按评分搜索产品

通过filters筛选指定评分范围,或按评分排序:

// 筛选评分等于5的产品
index.search('', {
  filters: 'rating:5'
});

// 筛选评分≥4的产品
index.search('', {
  filters: 'rating >= 4'
});

// 按评分从高到低排序
index.search('', {
  sortBy: ['rating:desc']
});

4. 按最低价格搜索产品

设定价格下限筛选,或按价格升序排序:

// 筛选原价≥500000的产品
index.search('', {
  filters: 'price >= 500000'
});

// 按原价从低到高排序
index.search('', {
  sortBy: ['price:asc']
});

5. 按最高价格搜索产品

设定价格上限筛选,或按价格降序排序:

// 筛选原价≤800000的产品
index.search('', {
  filters: 'price <= 800000'
});

// 按原价从高到低排序
index.search('', {
  sortBy: ['price:desc']
});

6. 按库存可用性搜索产品

直接筛选库存大于0的产品:

index.search('', {
  filters: 'stock > 0'
});

7. 搜索当前正在促销的产品

结合促销状态字段is_discount与促销时间范围判断:

const currentDate = new Date().toISOString().split('T')[0]; // 格式化为YYYY-MM-DD

index.search('', {
  filters: `is_discount:2 AND start_date <= "${currentDate}" AND end_date >= "${currentDate}"`
});

注:此处假设is_discount:2代表正在促销,需根据实际业务逻辑调整字段值

8. 搜索当前正在促销且折扣率符合特定值的产品

可提前在索引时计算并存储discount_rate字段,或查询时实时计算:

const currentDate = new Date().toISOString().split('T')[0];

// 方式1:使用预存的discount_rate字段,筛选折扣率≥50%的产品
index.search('', {
  filters: `is_discount:2 AND start_date <= "${currentDate}" AND end_date >= "${currentDate}" AND discount_rate >= 0.5`
});

// 方式2:查询时实时计算折扣率(需Algolia启用表达式筛选)
index.search('', {
  filters: `is_discount:2 AND start_date <= "${currentDate}" AND end_date >= "${currentDate}" AND ((price - price_after_disc)/price) >= 0.5`
});

9. 搜索当前正在促销且折扣后价格符合特定值的产品

筛选促销期内且折扣后价格在指定区间的产品:

const currentDate = new Date().toISOString().split('T')[0];

// 筛选折扣后价格在300000-400000之间的促销产品
index.search('', {
  filters: `is_discount:2 AND start_date <= "${currentDate}" AND end_date >= "${currentDate}" AND price_after_disc >= 300000 AND price_after_disc <= 400000`
});

内容的提问来源于stack exchange,提问作者DenyCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:00:59