Database Manual / Self-Managed Deployments / Text Search / Text Search Operators

$text (Self-Managed Deployments)(自我管理部署)

Note

This page describes text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search and a vector search solution, Vector Search.本页介绍自我管理(非Atlas)部署的文本查询功能。对于托管在MongoDB上的数据,MongoDB还提供了改进的全文查询解决方案MongoDB搜索和矢量搜索解决方案矢量搜索

This page describes the $text operator for self-managed deployments.本页介绍用于自我管理部署的$text运算符。

Definition定义

$text
$text performs a text query on the content of the fields indexed with a text index.对用文本索引索引的字段的内容执行文本查询。

Compatibility兼容性

You can use $text for deployments hosted in the following environments:您可以将$text用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

A $text expression has the following syntax:$text表达式具有以下语法:

{
$text: {
$search: <string>,
$language: <string>,
$caseSensitive: <boolean>,
$diacriticSensitive: <boolean>
}
}

The $text operator accepts a text query document with the following fields:$text运算符接受具有以下字段的文本查询文档:

Field字段Type类型Description描述
$searchstring字符串A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR query on the terms unless you specify the terms as an exact string. MongoDB解析并用于查询文本索引的一串术语。MongoDB对术语执行逻辑OR查询,除非您将术语指定为精确的字符串For more information on the field, see Behavior.有关该字段的更多信息,请参阅行为
$languagestring字符串

Optional. 可选。The language that determines the list of stop words for the query and the rules for the stemmer and tokenizer. 确定查询的停用词列表以及词干分析器和标记器规则的语言。If unspecified, MongoDB uses the default language of the index. 如果未指定,MongoDB将使用索引的默认语言。For supported languages, see Text Search Languages on Self-Managed Deployments.有关支持的语言,请参阅自我管理部署上的文本搜索语言

If you specify a default_language value of none, the text index parses through each word in the field, including stop words, and ignores suffix stemming.如果指定default_languagenone,文本索引将解析字段中的每个单词,包括停用词,并忽略后缀词干。

$caseSensitiveboolean布尔值

Optional. 可选。A boolean flag to enable or disable case sensitivity. Defaults to false. 启用或禁用区分大小写的布尔标志。默认为falseIf unspecified, MongoDB defers to the case insensitivity of the text index.如果未指定,MongoDB将遵循text索引不区分大小写的原则。

For more information, see Case Insensitivity.有关更多信息,请参阅大小写不敏感性

$diacriticSensitiveboolean布尔值

Optional. 可选。A boolean flag to enable or disable diacritic sensitivity against version 3 text indexes. 一个布尔标志,用于启用或禁用对版本3文本索引的变音符号敏感性。Defaults to false. If unspecified, MongoDB defers to the diacritic insensitivity of the text index.默认为false。如果未指定,MongoDB将遵循text索引的变音符号不敏感性。

Text queries against earlier versions of the text index are inherently diacritic sensitive and cannot be diacritic insensitive. 针对早期版本的文本索引的文本查询本质上对变音符号敏感,不能对变音字符不敏感。As such, the $diacriticSensitive option has no effect with earlier versions of the text index.因此,$diacriticSensitive选项对早期版本的text索引无效。

For more information, see Diacritic Insensitivity.有关更多信息,请参阅变音符号不敏感

The $text operator, by default, does not return results sorted in terms of the results' scores. 默认情况下,$text运算符不会返回按结果分数排序的结果。For more information on sorting by the results' scores, see the Text Score documentation.有关按结果分数排序的更多信息,请参阅文本分数文档。

Behavior行为

Restrictions限制

  • A query can specify, at most, one $text expression.一个查询最多只能指定一个$text表达式。
  • $text cannot appear in $nor expressions.$text不能出现在$nor表达式中。
  • $text cannot appear in $elemMatch query expressions or $elemMatch projection expressions.$text不能出现在$elemMatch查询表达式或$elemMmatch投影表达式中。
  • To use $text in an $or expression, all clauses in the $or array must be indexed.要在$or表达式中使用$text,必须对$or数组中的所有子句进行索引。
  • If a query includes a $text expression, you cannot use hint() to specify which index to use for the query.如果查询包含$text表达式,则不能使用hint()指定查询使用的索引。
  • You cannot specify the $natural sort order if the query includes a $text expression.如果查询包含$text表达式,则无法指定$natural排序顺序。
  • You cannot combine the $text expression, which requires a special text index, with a query operator that requires a different type of special index. 您不能将需要特殊文本索引$text表达式与需要不同类型特殊索引的查询运算符组合在一起。For example you cannot combine $text expression with the $near operator.例如,您不能将$text表达式和$near运算符组合在一起。
  • Views do not support $text.视图不支持$text
  • $text is unsupported for creating indexes using the Stable API V1.$text不支持使用Stable API V1创建索引。

If using the $text operator in aggregation, the following restrictions also apply.如果在聚合中使用$text运算符,则以下限制也适用。

  • The $match stage that includes a $text must be the first stage in the pipeline.包含$text$match阶段必须是管道中的第一阶段。
  • A $text operator can only occur once in the stage.$text运算符在阶段中只能出现一次。
  • The $text operator expression cannot appear in $or or $not expressions.$text运算符表达式不能出现在$or$not表达式中。
  • $text, by default, does not return the matching documents in order of matching scores. 默认情况下,$text不会按匹配分数的顺序返回匹配的文档。To sort by descending score, use the $meta aggregation expression in the $sort stage.要按得分降序排序,请在$sort阶段使用$meta聚合表达式。

$search Field字段

In the $search field, specify a string of words that the $text operator parses and uses to query the text index.$search字段中,指定$text运算符解析并用于查询文本索引的字符串。

The $text operator treats most punctuation in the string as delimiters, except a hyphen-minus (-) that negates term or an escaped double quotes \" that specifies an exact string.$text运算符将字符串中的大多数标点符号视为分隔符,除了否定术语的连字符减号(-)或指定精确字符串的转义双引号\"

Note

The $search field for the $text expression is different than the $search aggregation stage provided by Atlas Search. $text表达式的$search字段与Atlas search提供的$search聚合阶段不同。The $search aggregation stage performs a full-text search on specified fields and is only available on MongoDB Atlas.$search聚合阶段对指定字段执行全文搜索,仅在MongoDB Atlas上可用。

Exact Strings精确字符串

To match on an exact multi-word string, as opposed to individual terms, enclose the string in escaped double quotes (\"), as in:要匹配精确的多单词字符串,而不是单个术语,请将字符串括在转义双引号(\")中,如:

"\"ssl certificate\""

If the $search string of a $text operation includes a multi-word string and individual terms, $text only matches the documents that include the multi-word string.如果$text操作的$search字符串包括多单词字符串和单个术语,则$text仅匹配包含多单词字符串的文档。

For example, the following $search string returns documents that include the exact string "ssl certificate":例如,以下$search字符串返回包含确切字符串"ssl certificate"的文档:

"\"ssl certificate\" authority key"

Negations否定

Prefixing a word with a hyphen-minus (-) negates a word:在单词前加连字符减(-)可以否定单词:

  • The negated word excludes documents that contain the negated word from the result set.否定词将包含否定词的文档从结果集中排除。
  • When passed a string that only contains negated words, $text does not match any documents.当传递一个只包含否定词的字符串时,$text与任何文档都不匹配。
  • A hyphenated word, such as pre-market, is not a negation. If used in a hyphenated word, the $text operator treats the hyphen-minus (-) as a delimiter. 连字符的单词,如pre-market,不是否定。如果在连字符单词中使用,$text运算符会将连字符减(-)视为分隔符。To negate the word market in this instance, include a space between pre and -market, i.e., pre -market.在这种情况下,为了否定market一词,在pre-market之间加一个空格,即pre -market

The $text operator adds all negations to the operation with the logical AND operator.$text运算符使用逻辑AND运算符将所有否定添加到操作中。

Match Operation匹配操作

Stop Words停用词

The $text operator ignores language-specific stop words, such as the and and in English.$text运算符忽略特定语言的停用词,如英语中的theand

Stemmed Words词干词

When you use case insensitivity and diacritic insensitivity, the $text operator matches on the complete stemmed word. 当您使用不区分大小写和不区分变音符号时,$text运算符会匹配完整的词干单词。If a document field contains the word blueberry, a $text operation with a $search term of blue does not match. 如果文档字段包含单词blueberry(蓝莓),则具有blue$search术语的$text操作不匹配。However, blueberry or blueberries match.然而,blueberryblueberries匹配。

Case Sensitivity and Stemmed Words区分大小写和词干

When you use case sensitivity ($caseSensitive: true), if the suffix stem contains uppercase letters, the $text operator matches on the exact word.当您使用区分大小写$caseSensitive:true)时,如果后缀词干包含大写字母,则$text运算符将与确切的单词匹配。

Diacritic Sensitivity and Stemmed Words变调敏感与词干

When you use diacritic sensitivity ($diacriticSensitive: true), if the suffix stem contains the diacritic mark or marks, the $text operator matches on the exact word.当您使用变音符号敏感性$diacriticSensitive: true)时,如果后缀词干包含一个或多个变音符号标记,则$text运算符将与确切的单词匹配。

Case Insensitivity病例不敏感

The $text operator defaults to the case insensitivity of the text index:$text运算符默认为文本索引不区分大小写:

  • The version 3 text index is case insensitive for Latin characters with or without diacritics and characters from non-Latin alphabets, such as the Cyrillic alphabet. See text index for details.版本3的文本索引对带有或不带有变音符号的拉丁字符以及非拉丁字母(如西里尔字母)的字符不区分大小写。详见文本索引
  • Earlier versions of the text index are case insensitive for Latin characters without diacritic marks; i.e. for [A-z].早期版本的text索引对没有变音符号的拉丁字符不区分大小写;即[A-z]

$caseSensitive Option选项

To support case sensitivity where the text index is case insensitive, specify $caseSensitive: true.要在text索引不区分大小写的情况下支持区分大小写,请指定$caseSensitive:true

Case Sensitivity Process大小写敏感性过程

If $caseSensitive: true and the text index is case insensitive, the $text operator:如果$caseSensitive: true且文本索引不区分大小写,则$text运算符:

  • First queries the text index for case insensitive and diacritic matches.首先查询text索引以查找不区分大小写和变音符号的匹配项。
  • Then, to return just the documents that match the case of the specified terms, the $text operation includes an additional stage to filter out the documents that do not match the specified case.然后,为了只返回与指定术语大小写匹配的文档,$text操作包括一个额外的阶段,用于筛选出与指定大小写不匹配的文档。

If $caseSensitive: true and if the suffix stem contains uppercase letters, the $text operator matches on the exact word.如果$caseSensitive: true,并且后缀词干包含大写字母,则$text运算符与确切的单词匹配。

Specifying $caseSensitive: true may impact performance.指定$caseSensitive: true可能会影响性能。

Diacritic Insensitivity变音符号不敏感

The $text operator defaults to the diacritic insensitivity of the text index:$text运算符默认为文本索引对变音符号不敏感:

  • The version 3 text index is diacritic insensitive. 版本3的text索引对变音符号不敏感。That is, the index does not distinguish between characters that contain diacritical marks and their non-marked counterpart, such as é, ê, and e.也就是说,索引不会区分包含变音符号的字符和它们的非标记对应字符,如éêe
  • Earlier versions of the text index are diacritic sensitive.早期版本的text索引对变音符号敏感。

$diacriticSensitive Option选项

To support diacritic sensitivity with the text index, specify $diacriticSensitive: true.要使用text索引支持变音符号敏感性,请指定$diacriticSensitive: true

Text queries against earlier versions of the text index are inherently diacritic sensitive and cannot be diacritic insensitive. As such, the $diacriticSensitive option for the $text operator has no effect with earlier versions of the text index.针对早期版本的文本索引的文本查询本质上对变音符号敏感,不能对变音字符不敏感。因此,$text运算符的$diacriticSensitive选项对早期版本的文本索引无效。

Diacritic Sensitivity ProcessDiacritic敏感性过程

To use diacritic sensitivity ($diacriticSensitive: true) with a version 3 text index, the $text operator:要将变音符号敏感性($diacriticSensitive: true)与版本3文本索引一起使用,$text运算符:

  • First queries the text index, which is diacritic insensitive.首先查询text索引,该索引对变音符号不敏感。
  • Then, to return just the documents that match the diacritic marked characters of the specified terms, the $text operation includes an additional stage to filter out the documents that do not match.然后,为了只返回与指定术语的变音符号标记字符匹配的文档,$text操作包括一个额外的阶段来筛选不匹配的文档。

Specifying $diacriticSensitive: true may impact performance.指定$diacriticSensitive: true可能会影响性能。

If you use $diacriticSensitive: true with an earlier version of the text index, the $text operator queries the text index, which is diacritic sensitive.如果将$diacriticSensitive: true与早期版本的text索引一起使用,则$text运算符会查询文本索引,该索引对变音符号敏感。

If $diacriticSensitive: true and if the suffix stem contains the diacritic mark or marks, the $text operator matches on the exact word.如果$diacriticSensitive: true,并且后缀词干包含一个或多个变音符号,则$text运算符将与确切的单词匹配。

Text Score文本得分

The $text operator assigns a score to each result document. The score represents the relevance of a document to a given query. $text运算符为每个结果文档分配一个分数。分数表示文档与给定查询的相关性。The score can be part of a sort() method specification as well as part of the projection expression. 分数可以是sort()方法规范的一部分,也可以是投影表达式的一部分。The { $meta: "textScore" } expression provides information on the processing of the $text operation. { $meta: "textScore" }表达式提供了关于$text操作处理的信息。See $meta projection operator for details on accessing the score for projection or sort.有关访问投影或排序分数的详细信息,请参阅$meta投影运算符。

Examples示例

The following examples assume a collection articles that has a version 3 text index on the field subject:以下示例假设一个集合articles在字段subject上具有版本3text索引:

db.articles.createIndex( { subject: "text" } )

Populate the collection with the following documents:用以下文档填充集合:

db.articles.insertMany( [
{ _id: 1, subject: "coffee", author: "xyz", views: 50 },
{ _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
{ _id: 3, subject: "Baking a cake", author: "abc", views: 90 },
{ _id: 4, subject: "baking", author: "xyz", views: 100 },
{ _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
{ _id: 6, subject: "Сырники", author: "jkl", views: 80 },
{ _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
{ _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
] )

$text with a Single Word用一个单词

The following example specifies a $search string of coffee:以下示例指定了coffee$search字符串:

db.articles.find( { $text: { $search: "coffee" } } )

This operation returns the documents that contain the term coffee in the indexed subject field, or more precisely, the stemmed version of the word:此操作返回索引subject字段中包含术语coffee的文档,或者更确切地说,返回单词的词干版本:

{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }

Match Any of the $search Terms匹配任何$search术语

If the $search string is a space-delimited string, $text performs a logical OR operation on each term and returns documents that contain any of the terms.如果$search字符串是空格分隔的字符串,则$text对每个术语执行逻辑OR运算,并返回包含任何术语的文档。

The following example specifies a $search string of three terms delimited by space, "bake coffee cake":以下示例指定了一个由空格分隔的三个术语组成的$search字符串"bake coffee cake"

db.articles.find( { $text: { $search: "bake coffee cake" } } )

This operation returns documents that contain either bake or coffee or cake in the indexed subject field, or more precisely, the stemmed version of these words:此操作返回索引subject字段中包含bakecoffeecake的文档,或者更确切地说,是这些单词的词干版本:

{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }
{ "_id" : 3, "subject" : "Baking a cake", "author" : "abc", "views" : 90 }
{ "_id" : 4, "subject" : "baking", "author" : "xyz", "views" : 100 }

$text with an Exact String带有精确字符串

To match an exact multi-word string as a single term, escape the quotes.要将精确的多单词字符串匹配为单个术语,请转义引号。

The following example matches the exact string coffee shop:以下示例与字符串coffee shop完全匹配:

db.articles.find( { $text: { $search: "\"coffee shop\"" } } )

This operation returns documents that contain the string coffee shop:此操作返回包含字符串coffee shop的文档:

{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }

The following example matches the strings coffee shop and Cafe con Leche. This is a logical OR of the two strings.以下示例匹配字符串coffee shopCafe con Leche。这是两个字符串的逻辑OR

db.articles.find( { $text: { $search: "\'coffee shop\' \'Cafe con Leche\'" } } )

This operation returns documents that contain both the strings, including documents that contain terms from both the strings:此操作返回同时包含这两个字符串的文档,包括包含两个字符串中的术语的文档:

[
{ _id: 8, subject: 'Cafe con Leche', author: 'xyz', views: 10 },
{ _id: 5, subject: 'Café Con Leche', author: 'abc', views: 200 },
{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }
]

Exclude Documents That Contain a Term排除包含术语的文档

A negated term is a term that is prefixed by a minus sign -. If you negate a term, the $text operator excludes the documents that contain those terms from the results.否定项是以减号-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。

The following example matches documents that contain the word coffee but do not contain the term shop, or more precisely the stemmed version of the words:以下示例匹配包含单词coffee但不包含单词shop的文档,或者更确切地说是单词的词干版本:

db.articles.find( { $text: { $search: "coffee -shop" } } )

The operation returns the following documents:该操作返回以下文档:

{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }

Query a Different Language查询其他语言

Use the optional $language field in the $text expression to specify a language that determines the list of stop words and the rules for the stemmer and tokenizer for the $search string.使用$text表达式中的可选$language字段指定一种语言,该语言决定了$search字符串的停用词列表以及词干分析器和标记器的规则。

If you specify a default_language value of none, the text index parses through each word in the field, including stop words, and ignores suffix stemming.如果指定default_languagenone,文本索引将解析字段中的每个单词,包括停用词,并忽略后缀词干。

The following example specifies es, i.e. Spanish, as the language that determines the tokenization, stemming, and stop words:以下示例指定es(即西班牙语)作为确定标记化、词干和停用词的语言:

db.articles.find(
{ $text: { $search: "leche", $language: "es" } }
)

The example returns the following documents:该示例返回以下文档:

{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }

The $text expression can also accept the language by name, spanish. See Text Search Languages on Self-Managed Deployments for the supported languages.$text表达式还可以按名称接受语言,spanish。有关支持的语言,请参阅自我管理部署上的文本搜索语言

Case and Diacritic Insensitivity案例与Diacritic不敏感

The $text operator defers to the case and diacritic insensitivity of the text index. $text运算符遵循text索引对大小写和变音符号的不敏感性。The version 3 text index is diacritic insensitive and expands its case insensitivity to include the Cyrillic alphabet as well as characters with diacritics. 版本3的text索引对变音符号不敏感,并将其不区分大小写的特性扩展到包括西里尔字母以及带有变音符号的字符。For details, see text Index Case Insensitivity and text Index Diacritic Insensitivity.有关详细信息,请参阅文本索引大小写不敏感文本索引变音符号不敏感

The following example performs a case and diacritic insensitive text query for the terms сы́рники or CAFÉS:以下示例对术语сы́рникиCAFÉS执行不区分大小写和变音符号的文本查询:

db.articles.find( { $text: { $search: "сы́рники CAFÉS" } } )

Using the version 3 text index, the operation matches the following documents.使用版本3text索引,该操作将匹配以下文档。

{ "_id" : 6, "subject" : "Сырники", "author" : "jkl", "views" : 80 }
{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }

With previous versions of the text index, the query would not match any document.使用以前版本的text索引,查询将不匹配任何文档。

Case Sensitivity大小写敏感

To enable case sensitivity, specify $caseSensitive: true. Specifying $caseSensitive: true may impact performance.要启用区分大小写,请指定$caseSensitive: true。指定$caseSensitive: true可能会影响性能。

Case Sensitivity with a Term带有术语的案例敏感性

The following example performs a case sensitive query for the term Coffee:以下示例对术语Coffee执行区分大小写的查询:

db.articles.find( { $text: { $search: "Coffee", $caseSensitive: true } } )

The operation matches just the following document:该操作仅匹配以下文档:

{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }

Case Sensitivity with an Exact String使用精确字符串区分大小写

The following example performs a case sensitive query for the exact multi-word string Café Con Leche:以下示例对确切的多单词字符串Café Con Leche执行区分大小写的查询:

db.articles.find( {
$text: { $search: "\"Café Con Leche\"", $caseSensitive: true }
} )

The operation matches just the following document:该操作仅匹配以下文档:

{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }

Case Sensitivity with Negated Term带否定项的案例敏感性

A negated term is a term that is prefixed by a minus sign -. If you negate a term, the $text operator will exclude the documents that contain those terms from the results. You can also specify case sensitivity for negated terms.否定项是以减号-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。您还可以为否定项指定区分大小写。

The following example performs a case sensitive query for documents that contain the word Coffee but do not contain the lower-case term shop, or more precisely the stemmed version of the words:以下示例对包含单词Coffee但不包含小写单词shop的文档执行区分大小写的查询,或者更确切地说,不包含单词的词干版本:

db.articles.find( { $text: { $search: "Coffee -shop", $caseSensitive: true } } )

The operation matches the following document:该操作与以下文档匹配:

{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg" }

Diacritic Sensitivity变音符号敏感

To enable diacritic sensitivity with a version 3 text index, specify $diacriticSensitive: true. Specifying $diacriticSensitive: true may impact performance.要使用版本3text索引启用变音符号敏感性,请指定$diacriticSensitive: true。指定$diacriticSensitive: true可能会影响性能。

Diacritic Sensitivity with a Term带有术语的变音符号敏感性

The following example performs a diacritic sensitive text query on the term CAFÉ, or more precisely the stemmed version of the word:以下示例对CAFÉ一词,或者更确切地说是该词的词干版本,执行对变音符号敏感的文本查询:

db.articles.find( { $text: { $search: "CAFÉ", $diacriticSensitive: true } } )

The operation only matches the following document:该操作仅匹配以下文档:

{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc" }

Diacritic Sensitivity with Negated Term否定条件下的辩证敏感性

The $diacriticSensitive option applies also to negated terms. A negated term is a term that is prefixed by a minus sign -. If you negate a term, the $text operator will exclude the documents that contain those terms from the results.$diacriticSensitive选项也适用于被否定的条款。否定项是以减号-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。

The following example performs a diacritic sensitive text query for documents that contain the term leches but not the term cafés, or more precisely the stemmed version of the words:以下示例对包含术语leches但不包含术语cafés的文档执行对变音符号敏感的文本查询,或者更确切地说,是单词的词干版本:

db.articles.find(
{ $text: { $search: "leches -cafés", $diacriticSensitive: true } }
)

The operation matches the following document:该操作与以下文档匹配:

{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz" }

Relevance Score Examples相关性得分示例

Return the Relevance Score返回相关性得分

The following example performs a text query for the term cake and uses the $meta operator in the projection document to append the relevance score to each matching document:以下示例对术语cake执行文本查询,并在投影文档中使用$meta运算符将相关性得分附加到每个匹配的文档中:

db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
)

The returned document includes an additional field score that contains the document's relevance score:返回的文档包括一个额外的字段score,其中包含文档的相关性分数:

{ "_id" : 3, "subject" : "Baking a cake", "author" : "abc", "views" : 90, "score" : 0.75 }

Tip

$meta

Sort by Relevance Score按相关性得分排序

  • You can specify the { $meta: "textScore" } expression in the sort() without also specifying the expression in the projection. For example:您可以在sort()中指定{ $meta: "textScore" }表达式,而无需在投影中指定表达式。例如:

    db.articles.find(
    { $text: { $search: "cake" } }
    ).sort( { score: { $meta: "textScore" } } )

    As a result, you can sort the resulting documents by their relevance without projecting the textScore.因此,您可以按相关性对生成的文档进行排序,而无需投影textScore

  • If you include the { $meta: "textScore" } expression in both the projection and sort(), the projection and sort documents can have different field names for the expression.如果在投影sort()中都包含{ $meta: "textScore" }表达式,则投影和排序文档可以为表达式使用不同的字段名。

    For example, in the following operation, the projection uses a field named score for the expression and the sort() uses the field named ignoredName.例如,在以下操作中,投影使用名为score的字段作为表达式,sort()使用名为ignoredName的字段。

    db.articles.find(
    { $text: { $search: "cake" } } ,
    { score: { $meta: "textScore" } }
    ).sort( { ignoredName: { $meta: "textScore" } } )

Tip

$meta

Return Top 2 Matching Documents返回前2个匹配文档

Use the limit() method in conjunction with a sort() to return the top n matching documents.limit()方法与sort()结合使用,返回前n个匹配的文档。

The following example queries for the term coffee and sorts the results by the descending score, limiting the results to the top two matching documents:以下示例查询术语coffee,并按降序对结果进行排序,将结果限制在前两个匹配的文档中:

db.articles.find(
{ $text: { $search: "coffee" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } ).limit(2)

Tip

$meta

$text with Additional Query and Sort Expressions带有附加查询和排序表达式的$text

The following example matches documents where the author equals "xyz" and the indexed field subject contains the terms coffee or bake. The operation also specifies a sort order of ascending date, then descending relevance score:以下示例匹配author等于"xyz"且索引字段subject题包含术语coffeebake的文档。该操作还指定了date升序、相关性得分降序的排序顺序:

db.articles.find(
{ author: "xyz", $text: { $search: "coffee bake" } },
{ score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )