作者:来自 Elastic Alexander Marquardt
了解如何在 Elasticsearch 中使用 function_score query,将 BM25 相关性与利润率和受欢迎程度信号相结合,从而优化电商搜索。
测试 Elastic 领先的开箱即用能力。深入了解我们的示例 notebooks,开始免费的云试用,或立即在你的本地机器上试用 Elastic。
概述
在本文中,你将学习如何使用 Elasticsearch 的 function_score query,将 BM25 相关性与利润率和受欢迎程度等真实业务指标相结合。本分步指南展示了如何通过对数提升来控制缩放,并为每一次排序计算提供完整的可解释性。
引言
在许多使用场景中,搜索结果侧重于词法(关键词)和语义(基于含义)的分析,以找到最准确、最权威地回答用户查询的内容。然而,电商搜索要复杂一些。
结果必须反映购物者的意图,并结合诸如利润率、产品受欢迎程度或其他并不总是与纯词法或语义匹配直接一致的业务目标。
文本相关性确保客户满意度,而按盈利能力和受欢迎程度进行排序,则让搜索成为一个业务优化引擎。
为了演示如何将业务信号纳入搜索结果,在本文中我们将探讨:
- 如何按利润率提升产品排名(在下方演示数据中,盈利能力为 0% 到 200%)。
- 如何将相同的逻辑扩展到受欢迎程度(销售数量)。
一旦你理解了如何按利润率和受欢迎程度进行提升,将搜索扩展到纳入其他信号就会变得很直接。
设置
下面是一个你可以直接粘贴到 Dev Tools 中的小型数据集,用于跟随示例操作。
`
1. POST _bulk
2. { "index": { "_index": "blog_food_products" } }
3. { "product_id": "MCC-HOME-500", "description": "McCain Home Chips 500g - High Margin", "margin": 200, "popularity": 100 }
4. { "index": { "_index": "blog_food_products" } }
5. { "product_id": "MCC-HOME-1000", "description": "McCain Home Chips 1kg", "margin": 100, "popularity": 640 }
6. { "index": { "_index": "blog_food_products" } }
7. { "product_id": "MCC-HOME-1500", "description": "McCain Home Chips 1.5kg", "margin": 50, "popularity": 10000 }
8. { "index": { "_index": "blog_food_products" } }
9. { "product_id": "BIR-CHIPS-450", "description": "BirdsEye Crispy Chips 450g", "margin": 9, "popularity": 880 }
10. { "index": { "_index": "blog_food_products" } }
11. { "product_id": "BIR-CHIPS-900", "description": "BirdsEye Crispy Chips 900g", "margin": 12, "popularity": 720 }
12. { "index": { "_index": "blog_food_products" } }
13. { "product_id": "TRE-MINT-33", "description": "Trebor Peppermint 33g", "margin": 5, "popularity": 1100 }
14. { "index": { "_index": "blog_food_products" } }
15. { "product_id": "TRE-MINT-4X38", "description": "Trebor Peppermint 4x38g", "margin": 8, "popularity": 680 }
16. { "index": { "_index": "blog_food_products" } }
17. { "product_id": "TIC-MINT-16", "description": "TicTac Mint 16g", "margin": 3.5, "popularity": 980 }
18. { "index": { "_index": "blog_food_products" } }
19. { "product_id": "TIC-MINT-6X16", "description": "TicTac Mint 6x16g", "margin": 7, "popularity": 640 }
`AI写代码
每个文档代表一个产品,包含:
- margin:利润率(百分比)
- popularity:相对销量(例如每周平均值,或上周总量)
不按利润率排序
我们可以通过执行一个不考虑利润率的简单查询 “McCain chips”,来查看基础结果,如下所示:
`
1. POST blog_food_products/_search
2. {
3. "size": 5,
4. "_source": ["description", "margin"],
5. "query": {
6. "match": {
7. "description" : "McCain Chips" }
8. }
9. }
`AI写代码
本文中的示例是在单节点测试集群( Elasticsearch 8.18.1 )上运行的,并且该索引只有一个主分片。如果你使用 Elastic Cloud Serverless,默认会为你的索引分配三个主分片。因此,如果你想复现本文中展示的相同 BM25 分数,可以通过在搜索请求中添加 ?search_type=dfs_query_then_fetch 来启用全局评分计算。该参数有助于复现分数,但通常并不需要,也不建议在生产系统中使用。包含该参数后,上述查询将变为:
` 1. POST blog_food_products/_search?search_type=dfs_query_then_fetch 2. { 3. "size": 5, 4. "_source": ["description", "margin"], 5. "query": { 6. "match": { 7. "description": "McCain Chips" 8. } 9. } 10. } `AI写代码dfs_query_then_fetch 会在索引级别而不是按分片计算 BM25 词项统计信息,从而使不同环境中的分数更加一致。
这将返回以下结果:
`
1. {
2. "_index": "blog_food_products",
3. "_id": "GKO3MJoBBtzDfCS5JfQM",
4. "_score": 1.6089411,
5. "_source": {
6. "description": "McCain Home Chips 1kg",
7. "margin": 100
8. }
9. },
10. {
11. "_index": "blog_food_products",
12. "_id": "GaO3MJoBBtzDfCS5JfQM",
13. "_score": 1.6089411,
14. "_source": {
15. "description": "McCain Home Chips 1.5kg",
16. "margin": 50
17. }
18. },
19. {
20. "_index": "blog_food_products",
21. "_id": "F6O3MJoBBtzDfCS5JfQM",
22. "_score": 1.3280699,
23. "_source": {
24. "description": "McCain Home Chips 500g - High Margin",
25. "margin": 200
26. }
27. },
28. {
29. "_index": "blog_food_products",
30. "_id": "GqO3MJoBBtzDfCS5JfQM",
31. "_score": 0.5837885,
32. "_source": {
33. "description": "BirdsEye Crispy Chips 450g",
34. "margin": 9
35. }
36. },
37. {
38. "_index": "blog_food_products",
39. "_id": "G6O3MJoBBtzDfCS5JfQM",
40. "_score": 0.5837885,
41. "_source": {
42. "description": "BirdsEye Crispy Chips 900g",
43. "margin": 12
44. }
45. }
`AI写代码
从上述结果可以看到,高利润率版本的薯条排在第 3 位,因为排序并未考虑利润率。
按利润率排序
在没有额外上下文的情况下,所有 “McCain chips” 的不同规格看起来同样相关 —— 但从业务角度来看,更高利润率的商品可能应该排在更靠前的位置。
| Product | Margin (%) | Description |
|---|---|---|
| McCain Home Chips 500g – High Margin | 200% | small pack |
| McCain Home Chips 1kg | 100% | mid pack |
| McCain Home Chips 1.5kg | 50% | family pack |
我们将使用 Elasticsearch 的 function_score query 来应用基于利润率的提升。
``
1. POST blog_food_products/_search
2. {
3. "size": 5,
4. "explain": false, // keep only for tuning
5. "_source": ["description", "margin"],
6. "query": {
7. "function_score": {
10. /* ───────────────────────────────────────────────
11. * Base query
12. * Replace with your actual BM25 or semantic query.
13. * ─────────────────────────────────────────────── */
14. "query": {
15. "match": {
16. "description" : "McCain Chips" }
17. },
20. /* ───────────────────────────────────────────────
21. * Margin-driven boost
22. * ------------------------------------------------
23. * Elasticsearch computes (for the ln1p modifier):
24. *
25. * log_margin = ln(1 + margin * factor)
26. * boost = 1 + log_margin // +1 baseline via explicit { "weight": 1 }
27. * final_score = BM25 * boost
28. *
29. * Picking `factor` to cap around 2× at max_margin ≈ 200:
30. *
31. * 1 + ln(1 + factor * 200) ≈ 2
32. * ln(1 + 200*factor) = 1
33. * 1 + 200*factor = e
34. * factor = (e - 1) / 200 ≈ 0.00859
35. *
36. * You can keep a little headroom, e.g. use 0.0085.
37. * ─────────────────────────────────────────────── */
38. "functions": [
39. {
40. "filter": { "range": { "margin": { "gt": 0 } } },
41. "field_value_factor": {
42. "field" : "margin",
43. "modifier": "ln1p", // natural log of (1 + margin * factor)
44. "factor" : 0.0085, // ≈ (e - 1) / 200
45. "missing" : 0
46. }
47. },
48. { "weight": 1 } // explicit neutral baseline (keeps zero/small margins neutral)
49. ],
51. "score_mode": "sum", // boost = 1 + ln(1 + margin*factor) (sum of the two functions)
52. "boost_mode": "multiply" // final_score = BM25 × boost
53. // "max_boost": 2.0 // optional: clamp hard ceiling
54. }
55. }
56. }
``AI写代码收起代码块
上面的查询结果如下,反映了边际提升(margin boosting)对得分的影响。注意,正如我们所期望的,高利润的 McCain 薯片已被提升到结果的第一位。
`
1. {
2. "_index": "blog_food_products",
3. "_id": "F6O3MJoBBtzDfCS5JfQM",
4. "_score": 2.6471777,
5. "_source": {
6. "description": "McCain Home Chips 500g - High Margin",
7. "margin": 200
8. }
9. },
10. {
11. "_index": "blog_food_products",
12. "_id": "GKO3MJoBBtzDfCS5JfQM",
13. "_score": 2.5987387,
14. "_source": {
15. "description": "McCain Home Chips 1kg",
16. "margin": 100
17. }
18. },
19. {
20. "_index": "blog_food_products",
21. "_id": "GaO3MJoBBtzDfCS5JfQM",
22. "_score": 2.1787827,
23. "_source": {
24. "description": "McCain Home Chips 1.5kg",
25. "margin": 50
26. }
27. },
28. {
29. "_index": "blog_food_products",
30. "_id": "G6O3MJoBBtzDfCS5JfQM",
31. "_score": 0.64049,
32. "_source": {
33. "description": "BirdsEye Crispy Chips 900g",
34. "margin": 12
35. }
36. },
37. {
38. "_index": "blog_food_products",
39. "_id": "GqO3MJoBBtzDfCS5JfQM",
40. "_score": 0.62682253,
41. "_source": {
42. "description": "BirdsEye Crispy Chips 450g",
43. "margin": 9
44. }
45. }
`AI写代码
理解公式
function_score 查询允许我们基于 margin 应用平滑且可解释的提升,同时不会压过 BM25 的词汇相关性。
它的工作原理如下:
`
1. margin_boost = ln(1 + margin × factor)
2. boost = 1 + margin_boost
3. final_score = BM25 × boost
`AI写代码
查询中使用的字段说明:
-
field_value_factor – 使用文档字段来影响得分,无需额外脚本开销。
-
modifier: “ln1p” – 计算 ln(1 + margin × factor)
注:ln1p(x) 是 ln(1 + x) 的简写。
-
factor – 控制比例;0.0085 在 margin=200 时将提升限制在约 2 倍。
-
weight: 1 – 确保中性项的最小提升为 1。
-
score_mode: “sum” – 将独立 weight:1 与 margin_boost 相加。
-
boost_mode: “multiply” – 用计算出的 boost 乘以 BM25。
为什么选择这个公式?
对真实数据,对数(ln1p)缩放表现良好:
-
对小 margin 增长快(奖励增量收益)。
-
对大 margin 趋于平缓(防止得分失控)。
-
连续且可解释——无阈值或不连续点。
| Margin | ln(1 + margin × 0.0085) | Boost (≈1+ln1p) | Boost Multiplier |
|---|---|---|---|
| 5 | 0.042 | 1.04 | ×1.04 |
| 50 | 0.35 | 1.35 | ×1.35 |
| 100 | 0.63 | 1.63 | ×1.63 |
| 200 | 0.99 | 1.99 | ×1.99 |
按 margin 和 popularity 排序
我们可以用相同的逻辑增加 popularity 提升。这里,我们调整 popularity factor,使得在 popularity 为 10,000 时,提升约增加 +1.0。(这些阈值取决于你的数据集规模。)
`
1. POST blog_food_products/_search
2. {
3. "size": 5,
4. "_source": ["product_id", "description", "margin", "popularity"],
5. "query": {
6. "function_score": {
7. "query": {
8. "match": {
9. "description": "McCain Chips" }
10. },
11. "functions": [
12. {
13. // calculate margin_boost
14. "filter": { "range": { "margin": { "gt": 0 } } },
15. "field_value_factor": {
16. "field": "margin",
17. "modifier":"ln1p", // ln(1 + margin * margin_f)
18. "factor": 0.008591, // ≈ (e - 1) / 200
19. "missing": 0
20. },
21. "weight": 1 // full impact from margin
22. },
23. {
24. // calculate popularity_boost
25. "filter": { "range": { "popularity": { "gt": 0 } } },
26. "field_value_factor": {
27. "field": "popularity",
28. "modifier":"ln1p", // ln(1 + popularity * popularity_f)
29. "factor": 0.0001718, // ≈ (e - 1) / 10,000
30. "missing": 0
31. },
32. "weight": 0.5 // popularity counts for half the impact of margin
33. },
34. {
35. "weight": 1 // ensures minimum boost of 1
36. }
37. ],
38. "score_mode": "sum", // boost = 1 + margin_boost + 0.5×popularity_boost
39. "boost_mode": "multiply" // final_score = BM25 * boost
40. // "max_boost": 4.0 // optional: clamp hard ceiling
41. }
42. }
43. }
`AI写代码
这样返回的结果会将最受欢迎的产品排在第一位,即使它不是利润最高的,如下所示——在这个例子中,popularity 提升的影响将 McCain 家庭装薯片 1.5kg 提升到了第一位。
`
1. {
2. "_index": "blog_food_products",
3. "_id": "IqPBMJoBBtzDfCS5CvRg",
4. "_score": 2.988299,
5. "_source": {
6. "product_id": "MCC-HOME-1500",
7. "description": "McCain Home Chips 1.5kg",
8. "margin": 50,
9. "popularity": 10000
10. }
11. },
12. {
13. "_index": "blog_food_products",
14. "_id": "IaPBMJoBBtzDfCS5CvRg",
15. "_score": 2.6905532,
16. "_source": {
17. "product_id": "MCC-HOME-1000",
18. "description": "McCain Home Chips 1kg",
19. "margin": 100,
20. "popularity": 640
21. }
22. },
23. {
24. "_index": "blog_food_products",
25. "_id": "IKPBMJoBBtzDfCS5CvRg",
26. "_score": 2.667411,
27. "_source": {
28. "product_id": "MCC-HOME-500",
29. "description": "McCain Home Chips 500g - High Margin",
30. "margin": 200,
31. "popularity": 100
32. }
33. },
34. {
35. "_index": "blog_food_products",
36. "_id": "JKPBMJoBBtzDfCS5CvRg",
37. "_score": 0.67510986,
38. "_source": {
39. "product_id": "BIR-CHIPS-900",
40. "description": "BirdsEye Crispy Chips 900g",
41. "margin": 12,
42. "popularity": 720
43. }
44. },
45. {
46. "_index": "blog_food_products",
47. "_id": "I6PBMJoBBtzDfCS5CvRg",
48. "_score": 0.66836256,
49. "_source": {
50. "product_id": "BIR-CHIPS-450",
51. "description": "BirdsEye Crispy Chips 450g",
52. "margin": 9,
53. "popularity": 880
54. }
55. }
`AI写代码收起代码块
最终的提升效果
这些 “factors” 被调整,使得在假定的最大值时,提升增加约 +1.0。它们是根据以下公式计算的:
`
1. ln(1 + 200 × margin_f) = 1.0
2. i.e. margin_f = 0.008592
3. ln(1 + 10 000 × popularity_f) = 1.0
4. i.e. popularity_f = 0.0001718
`AI写代码
然后:
`
1. margin_boost = ln(1 + margin × margin_f)
2. popularity_boost = ln(1 + popularity × popularity_f)
3. boost = 1 + margin_boost + 0.5 × popularity_boost
4. final_score = BM25 × boost
`AI写代码
下表中的每个单元格表示不同 margin 和 popularity 值对应的 BM25 总乘数。
如何阅读表格:
-
第一列(popularity = 0)仅体现 margin 的影响。
-
向右移动,popularity 会增加提升——但由于其 weight 为 0.5,对总提升的贡献减半。
-
即使在极端值(popularity = 100,000)下,由于对数缩放,提升也会趋于平缓。
调优
如果你发现 popularity 可能非常高(例如 100k+),且不希望提升超过某个上限,可以:
-
进一步降低 popularity factor,或
-
在 function_score 中添加 “max_boost”: ,或
-
分开权重,例如对 popularity 使用 “weight”: 0.25,对 margin 使用 “weight”: 1(仍使用 score_mode: “sum”),如果你希望某一项影响较小。
使用 rank_feature 实现类似场景
乍一看,rank_feature 和 rank_features 似乎是引入数值信号(如 popularity、recency,甚至利润 margin)的自然选择。它们速度快、压缩良好且易于操作 —— 这也是许多团队首先使用它们的原因。
然而,它们并不适合这种类型的评分模型,原因如下:
1)rank_feature 的贡献严格是加法的
得分形式如下:
`final_score = BM25 + feature_boost`AI写代码
这意味着提升的效果会随着 BM25 分数的大小而发生巨大变化:
-
当 BM25 较小时,提升主导排名。
-
当 BM25 较大时,相同的提升几乎无效。
我们需要一致的、成比例的行为。
2)无法表达 “百分比” 或乘法逻辑
本文的模型需要表达类似:
-
“Popularity 提高相关性约 20%。”
-
“Margin 增强相关性,但绝不覆盖它。”
rank_feature 做不到这一点,它不支持对 BM25 分数的乘法调整。
3)多信号组合变得不稳定且难以调优
如果通过 rank_features 组合 margin、popularity、availability 或其他业务指标,每个特征都会增加一个独立的加法项。它们以不透明的方式相互作用,使得调优脆弱且不可预测。
4)总结
rank_feature 适合简单的加法数值提升,但不适合需要:
-
跨查询保持稳定的行为
-
成比例 / 乘法效果
-
可解释的多信号融合
因此,本文使用 function_score,因为它提供了明确、可控的评分方式,无论 BM25 分数大小如何,都能保持一致的行为。
总结
Elastic 的 function_score 查询可以轻松将搜索排名从内容相关性转变为业务优化。
通过将 BM25 相关性与经济信号(如 margin 和 popularity)结合,你可以:
-
使搜索结果与实际业务目标对齐。
-
通过单一参数(factor)调节缩放。
-
通过 _explain 保持完整的可解释性。
在建立了这一基础后,你可以扩展到库存水平(降低低库存产品的排名)、新鲜度(优先显示新产品)或其他关键业务信号。每个新信号只需加入提升值,然后再乘以基础 BM25 相关性得分。