Text search assigns a score to each document that contains the search term in the indexed fields. 文本搜索为索引字段中包含搜索项的每个文档分配分数。The score determines the relevance of a document to a given search query.分数确定文档与给定搜索查询的相关性。
For a 对于text
index, the weight of an indexed field denotes the significance of the field relative to the other indexed fields in terms of the text search score.text
索引,索引字段的权重表示该字段相对于其他索引字段在文本搜索分数方面的重要性。
For each indexed field in the document, MongoDB multiplies the number of matches by the weight and sums the results. 对于文档中的每个索引字段,MongoDB将匹配数乘以权重并对结果求和。Using this sum, MongoDB then calculates the score for the document. 然后,MongoDB使用该总和计算文档的得分。See 有关按文本分数返回和排序的详细信息,请参阅$meta
operator for details on returning and sorting by text scores.$meta
运算符。
The default weight is 1 for the indexed fields. 索引字段的默认权重为1。To adjust the weights for the indexed fields, include the 要调整索引字段的权重,请在weights
option in the db.collection.createIndex()
method.db.collection.createIndex()
方法中包含weights
选项。
Choose the weights carefully in order to prevent the need to reindex.仔细选择权重,以防需要重新索引。
A collection blog
has the following documents:blog
集合包含以下文档:
{ _id: 1, content: "This morning I had a cup of coffee.", about: "beverage", keywords: [ "coffee" ] } { _id: 2, content: "Who doesn't like cake?", about: "food", keywords: [ "cake", "food", "dessert" ] }
To create a 要为text
index with different field weights for the content
field and the keywords
field, include the weights
option to the createIndex()
method. content
字段和keywords
字段创建具有不同字段权重的text
索引,请在createIndex()
方法中包含权重选项。For example, the following command creates an index on three fields and assigns weights to two of the fields:例如,以下命令在三个字段上创建索引,并为其中两个字段指定权重:
db.blog.createIndex( { content: "text", keywords: "text", about: "text" }, { weights: { content: 10, keywords: 5 }, name: "TextIndex" } )
The text
index has the following fields and weights:text
索引具有以下字段和权重:
content
keywords
about
These weights denote the relative significance of the indexed fields to each other. 这些权重表示索引字段彼此之间的相对重要性。For instance, a term match in the 例如,content
field has:content
字段中的术语匹配具有:
2
times (i.e. 10:5
) the impact as a term match in the keywords
field and2
倍(即10:5
)的影响,作为keywords
字段中的术语匹配,并且10
times (i.e. 10:1
) the impact as a term match in the about
field.10
倍(即10:1
)的影响,作为about
字段的术语匹配。For data hosted on MongoDB Atlas, Atlas Search provides more robust custom scoring than 对于MongoDB Atlas上托管的数据,Atlas搜索提供了比text
indexes. text
索引更强大的自定义评分。To learn more, see the Atlas Search Scoring documentation.要了解更多信息,请参阅Atlas搜索评分文档。