Docs 主页 → 启动和管理 MongoDB → MongoDB Atlas
修改分数
所有操作符都可以使用以下分数修改选项。有关详细信息和示例,请单击以下任一选项:
boost
boost
选项将结果的基本分数乘以给定的数字或文档中数值字段的值。例如,您可以使用 boost
提高某些匹配文档在结果中的重要性。
注意
不能同时使用
boost
和constant
选项。boost
选项与path
的乘法与使用function
选项与路径表达式的乘法相同。
字段
boost
选项包含以下字段:
字段 | 类型 | 必要性 | 说明 |
---|---|---|---|
value | 浮点 | 可选的 | 与默认基本分数相乘的数字。值必须是正数。 value 或 path 是必填项,但无法同时指定。 |
path | 字符串 | 可选的 | 数字字段的名称,其值要乘以默认基数分数。 path 或 value 是必填项,但无法同时指定。 |
undefined | 浮点 | Optional | 用来替换 path 的数值,如果在文档中找不到通过 path 指定的数字字段。如果省略,则默认值为 0 。仅当指定 path 时才可以指定此项。 |
举例
以下示例使用 sample_mflix
数据库中的 movies
集合。如果集群有示例数据集,则可以创建 Atlas Search default
索引并在集群上运行示例查询。
复合查询示例展示了如何提高一种搜索条件相对于另一种搜索条件的重要性。查询包括一个 $project
阶段,以排除 title
和 score
以外的所有字段。
constant
constant
选项将基数分数替换为指定的数字。
注意
不得同时使用 constant
和 boost
选项。
举例
以下示例使用 sample_mflix.movies
集合上的默认索引以使用复合运算符查询 plot
和 title
字段。在查询中,文本运算符使用 score
和 constant
选项,将所有得分结果替换为 5
,以获得仅与 title
字段的查询匹配的结果。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "should": [{ 6 "text": { 7 "query": "tower", 8 "path": "plot" 9 } 10 }, 11 { 12 "text": { 13 "query": "tower", 14 "path": "title", 15 "score": { "constant": { "value": 5 } } 16 } 17 }] 18 } 19 } 20 }, 21 { 22 "$limit": 5 23 }, 24 { 25 "$project": { 26 "_id": 0, 27 "title": 1, 28 "plot": 1, 29 "score": { "$meta": "searchScore" } 30 } 31 } 32 ])
上述查询返回以下结果,其中仅与 title
字段的查询匹配的文档的分数将替换为指定的 constant
值:
1 [ 2 { 3 plot: 'Several months after witnessing a murder, residents of Tower Block 31 find themselves being picked off by a sniper, pitting those lucky enough to be alive into a battle for survival.', 4 title: 'Tower Block', 5 score: 8.15460205078125 6 }, 7 { 8 plot: "When a group of hard-working guys find out they've fallen victim to their wealthy employer's Ponzi scheme, they conspire to rob his high-rise residence.", 9 title: 'Tower Heist', 10 score: 5 11 }, 12 { 13 plot: 'Toru Kojima and his friend Koji are young student boys with one thing in common - they both love to date older women. Koji is a playboy with several women, young and older, whereas Toru is a romantic with his heart set on on certain lady.', 14 title: 'Tokyo Tower', 15 score: 5 16 }, 17 { 18 plot: 'A middle-aged mental patient suffering from a split personality travels to Italy where his two personalities set off all kinds of confusing developments.', 19 title: 'The Leaning Tower', 20 score: 5 21 }, 22 { 23 plot: 'A documentary that questions the cost -- and value -- of higher education in the United States.', 24 title: 'Ivory Tower', 25 score: 5 26 } 27 ]
embedded
注意
该选项只能与嵌入式文档运算符一起使用。
embedded
选项允许您配置如何:
聚合多个匹配嵌入式文档的分数。
聚合匹配嵌入式文档的分数后,修改 embeddedDocument 操作符的分数。
注意
Atlas Search embeddedDocuments索引选项、 embeddedDocument操作符和embedded
评分选项均处于预览状态。当副本集或单个 MongoDB 分片上的 Atlas Search 索引达到 Lucene 的 20 亿个文档限制时,Atlas Search 不会为该索引的新文档编制索引或对现有文档进行更新。当此功能正式可用时,将制定适应此限制的解决方案。要解决与使用此功能相关的任何问题,请联系支持部门。
字段
embedded
选项包含以下字段:
字段 | 类型 | 必要性 | 说明 |
---|---|---|---|
aggregate | 字符串 | Optional | 配置如何合并匹配嵌入式文档的分数。值必须是以下聚合策略之一:
如果省略,该字段默认值为 |
outerScore | Optional | 指定应用聚合策略后要应用的分数修改。 |
举例
以下示例将对 sample_analytics.transactions
集合使用名为 default
的索引。该索引为 transactions
数组中的文档配置 embeddedDocuments
类型,并为 transactions.symbol
和 transactions.transaction_code
字段配置 string
类型。
例子
{ "mappings": { "dynamic": true, "fields": { "transactions": { "dynamic": true, "fields": { "symbol": { "type": "string" }, "transaction_code": { "type": "string" } }, "type": "embeddedDocuments" } } } }
transactions
字段的示例查询包含一个文档数组,其中的 $limit
阶段将输出限制为 10
个结果,$project
阶段用于执行以下操作:
排除除
account_id
和transaction_count
之外的所有字段。添加名为
score
的字段,该字段应用聚合策略并进一步修改分数。注意
您可以在 embeddedDocument 操作符的
function
的 选项内使用score
operator
}。但是,对于需要 选项的函数分数 表达式path
,您为表达式指定为path
的数字字段必须位于 embeddedDocument 操作符的path
内。例如,在以下示例查询中,在transactions.amount
操作符的score
选项内指定的第 行字段compound
24位于embeddedDocument
transactions
行 操作符的路径4 中。 。
该查询会搜索买入 AMD
的帐户,并将最大的单笔 AMD
买入(按股数计算)乘以该帐户在此期间进行的交易数量的 log1p
值。该操作根据以下内容对帐户进行排名:
单次交易中购买的
AMD
数量上一周期的交易数
1 db.transactions.aggregate({ 2 "$search": { 3 "embeddedDocument": { 4 "path": "transactions", 5 "operator": { 6 "compound": { 7 "must": [ 8 { 9 "text": { 10 "path": "transactions.symbol", 11 "query": "amd" 12 } 13 }, 14 { 15 "text": { 16 "path": "transactions.transaction_code", 17 "query": "buy" 18 } 19 } 20 ], 21 "score": { 22 "function": { 23 "path": { 24 "value": "transactions.amount", 25 "undefined": 1.0 26 } 27 } 28 } 29 } 30 }, 31 "score": { 32 "embedded": { 33 "aggregate": "maximum", 34 "outerScore": { 35 "function": { 36 "multiply": [ 37 { 38 "log1p": { 39 "path": { 40 "value": "transaction_count" 41 } 42 } 43 }, 44 { 45 "score": "relevance" 46 } 47 ] 48 } 49 } 50 } 51 } 52 } 53 } 54 }, 55 { "$limit": 10 }, 56 { 57 "$project": { 58 "_id": 0, 59 "account_id": 1, 60 "transaction_count": 1, 61 "score": { $meta: "searchScore" } 62 } 63 })
Atlas Search 对帐户进行排名,在上一时期进行过多次交易并在单笔交易中购买了许多 AMD
的帐户排名较高。
1 [ 2 { account_id: 467651, transaction_count: 99, score: 19982 }, 3 { account_id: 271554, transaction_count: 96, score: 19851.822265625 }, 4 { account_id: 71148, transaction_count: 99, score: 19840 }, 5 { account_id: 408143, transaction_count: 100, score: 19836.76953125 }, 6 { account_id: 977774, transaction_count: 98, score: 19822.64453125 }, 7 { account_id: 187107, transaction_count: 96, score: 19754.470703125 }, 8 { account_id: 492843, transaction_count: 97, score: 19744.998046875 }, 9 { account_id: 373169, transaction_count: 93, score: 19553.697265625 }, 10 { account_id: 249078, transaction_count: 89, score: 19436.896484375 }, 11 { account_id: 77690, transaction_count: 90, score: 19418.017578125 } 12 ]
function
function
选项允许您使用数值字段更改文档的最终分数。您可以通过表达式指定用于计算最终分数的数字字段。如果函数得分的最终结果小于0
,Atlas Search 会将得分替换为0
。
注意
您可以在 embeddedDocument 运算符的 operator
的 score
选项中使用 function
。但是,对于需要 path
选项的函数分数表达式,可为表达式指定为 path
的数值字段必须位于 embeddedDocument 运算符的 path
内。
表达式
使用以下带有 function
选项的表达式来更改文档的最终分数:
算术表达式,用于将一系列数字相加或相乘。
常量表达式,允许在函数得分中使用常量。
高斯衰减表达式,通过以指定速率相乘来衰减或减少分数。
路径表达式,将索引数字字段值合并到函数分数中。
分数表达式,返回 Atlas Search 分配的相关性分数。
一元表达式,即采用单个参数的表达式。在 Atlas Search 中,您可以计算指定数字的 log10(x) 或 log10(x+1)。
举例
以下示例使用 sample_mflix.movies
集合上的默认索引来查询 title
字段。示例查询包括一个 $limit
阶段,用于将输出限制为 5
结果;以及一个 $project
阶段,用于排除除 title
和 score
之外的所有字段。