$text
On this page本页内容
This page describes text search capabilities for self-managed (non-Atlas) deployments. 本页介绍了用于自我管理(非Atlas)部署的文本搜索功能。For data hosted on MongoDB Atlas, MongoDB offers an improved full-text search solution, Atlas Search.对于托管在MongoDB Atlas上的数据,MongoDB提供了一个改进的全文搜索解决方案Atlas search。
This page describes 本页介绍用于自我管理部署的$text
operator for self-managed deployments.$text
运算符。
Definition定义
$text
-
$text
performs a text search on the content of the fields indexed with a text index.对使用文本索引进行索引的字段的内容执行文本搜索。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描述$search
string A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logicalMongoDB解析并用于查询文本索引的一组术语。MongoDB对术语执行逻辑OR搜索,除非指定为短语。OR
search of the terms unless specified as a phrase.See Behavior for more information on the field.有关该字段的详细信息,请参阅行为。$language
string Optional.可选的。The language that determines the list of stop words for the search and the rules for the stemmer and tokenizer.确定搜索的停止词列表以及词干生成器和标记生成器规则的语言。If not specified, the search uses the default language of the index.如果未指定,则搜索将使用索引的默认语言。For supported languages, see Text Search Languages.有关支持的语言,请参阅文本搜索语言。
If you specify a如果指定default_language
value ofnone
, then the text index parses through each word in the field, including stop words, and ignores suffix stemming.default_language
值none
,则文本索引将解析字段中的每个单词,包括停止词,并忽略后缀词干。$caseSensitive
boolean Optional.可选的。A boolean flag to enable or disable case sensitive search.用于启用或禁用区分大小写搜索的布尔标志。Defaults to默认为false
; i.e. the search defers to the case insensitivity of the text index.false
;即,搜索遵循文本索引不区分大小写的原则。
For more information, see Case Insensitivity.有关详细信息,请参阅不区分大小写。$diacriticSensitive
boolean Optional.可选的。A boolean flag to enable or disable diacritic sensitive search against version 3 text indexes.一个布尔标志,用于启用或禁用对版本3文本索引敏感的变音符号搜索。Defaults to默认为false
; i.e. the search defers to the diacritic insensitivity of the text index.false
;即,搜索遵循文本索引的变音符号不敏感。
Text searches 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 thetext
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 text search scores, see the Text Score documentation.有关按文本搜索分数排序的更多信息,请参阅文本分数文档。
Behavior行为
Restrictions限制
A query can specify, at most, one查询最多可以指定一个$text
expression.$text
表达式。The$text
query can not appear in$nor
expressions.$text
查询不能出现在$nor
表达式中。The$text
query can not appear in$elemMatch
query expressions or$elemMatch
projection expressions.$text
查询不能出现在$elemMatch
查询表达式或$elemMatch
投影表达式中。To use a若要在$text
query 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 usehint()
to specify which index to use for the query.$text
表达式,则不能使用hint()
指定用于查询的索引。You cannot specify如果查询包含$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 search.视图不支持文本搜索。$text
is unsupported for creating indexes using the Stable API V1.不支持使用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
表达式中。The text search, by default, does not return the matching documents in order of matching scores.默认情况下,文本搜索不会按匹配分数的顺序返回匹配文档。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 a phrase.$text
运算符将字符串中的大多数标点符号视为分隔符,但否定术语的连字符减号(-
)或指定短语的转义双引号\"
除外。
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 full-text search on specified fields and is only available on MongoDB Atlas.$search
聚合阶段对指定字段执行全文搜索,并且仅在MongoDB Atlas上可用。
Phrases短语
To match on a phrase, as opposed to individual terms, enclose the phrase in escaped double quotes (要匹配短语,而不是单个术语,请将短语用转义双引号(\"
), as in:\"
)括起来,如:
"\"ssl certificate\""
If the 如果$search
string includes a phrase and individual terms, text search will only match the documents that include the phrase.$search
字符串包含短语和单个术语,则文本搜索将只匹配包含该短语的文档。
For example, passed a 例如,传递了一个$search
string:$search
字符串:
"\"ssl certificate\" authority key"
The $text
operator searches for the phrase "ssl certificate"
.$text
运算符搜索短语"ssl certificate"
。
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 search string that only contains negated words, text search will not match any documents.当传递一个只包含否定单词的搜索字符串时,文本搜索将不匹配任何文档。A hyphenated word, such as连字符的单词,例如pre-market
, is not a negation. If used in a hyphenated word,$text
operator treats the hyphen-minus (-
) as a delimiter.pre-market
,不是否定词。如果在连字符单词中使用,$text
运算符将连字符减号(-
)视为分隔符。To negate the word在这种情况下,要否定market
in this instance, include a space betweenpre
and-market
, i.e.,pre -market
.market
这个词,需要在pre
和-market
之间留出一个空间,即pre -market
。
The $text
operator adds all negations to the query 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
运算符忽略特定于语言的停止词,例如英语中的the
和and
。
Stemmed Words词干单词
For case insensitive and diacritic insensitive text searches, the 对于不区分大小写和不区分重音符号的文本搜索,$text
operator matches on the complete stemmed word. $text
运算符会在完整词干上匹配。So if a document field contains the word 因此,如果文档字段包含单词blueberry
, a search on the term blue
will not match. blueberry
,则对单词蓝色的搜索将不匹配。However, 然而,blueberry
or blueberries
will match.blueberry
或blueberries
会匹配。
Case Sensitive Search and Stemmed Words区分大小写的搜索和隐藏词
For case sensitive search (i.e. 对于区分大小写的搜索(即$caseSensitive: true
), if the suffix stem contains uppercase letters, the $text
operator matches on the exact word.$caseSensitive:true
),如果后缀词干包含大写字母,$text
运算符将与确切的单词匹配。
Diacritic Sensitive Search and Stemmed WordsDiacritic敏感搜索和隐藏词
For diacritic sensitive search (i.e. 对于区分变音符号的搜索(即$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
运算符默认为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的文本索引对带或不带变音符号的拉丁字符以及非拉丁字母的字符(如西里尔字母)不区分大小写。有关详细信息,请参阅text
索引。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 sensitive search where the 若要支持text
index is case insensitive, specify $caseSensitive: true
.text
索引不区分大小写的区分大小写搜索,请指定$caseSensitive:true
。
Case Sensitive Search Process区分大小写的搜索过程
When performing a case sensitive search (在$caseSensitive: true
) where the text
index is case insensitive, the $text
operator:text
索引不区分大小写的情况下执行区分大小写搜索($caseSensitive: true
)时,$text
运算符:
First searches the首先在text
index for case insensitive and diacritic matches.text
索引中搜索不区分大小写和变音符号的匹配项。Then, to return just the documents that match the case of the search terms, the然后,为了只返回与搜索词大小写匹配的文档,$text
query operation includes an additional stage to filter out the documents that do not match the specified case.$text
查询操作包括一个额外的阶段,用于筛选出与指定大小写不匹配的文档。
For case sensitive search (i.e. 对于区分大小写的搜索(即$caseSensitive: true
), 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
可能会影响性能。
See also: 另请参阅:
Diacritic Insensitivity变音符号不敏感
The $text
operator defaults to the diacritic insensitivity of the text index:$text
运算符默认为text
索引的变音符号不敏感:
The version 3 text index is diacritic insensitive.版本3的文本索引不区分重音符号。That is, the index does not distinguish between characters that contain diacritical marks and their non-marked counterpart, such as也就是说,索引不区分包含变音标记的字符和未标记的对应字符,如é
,ê
, ande
.é
、ê
和e
。Earlier versions of the早期版本的text
index are diacritic sensitive.text
索引对变音符号敏感。
$diacriticSensitive
Option选项
To support diacritic sensitive text search against the 若要支持对text
index, specify $diacriticSensitive: true
.text
索引进行区分重音符号的文本搜索,请指定$diacriticSensitive: true
。
Text searches against earlier versions of the 针对早期版本的text
index are inherently diacritic sensitive and cannot be diacritic insensitive. text
索引的文本搜索本质上是区分重音符号的,不能不区分重音符号。As such, the 因此,$diacriticSensitive
option for the $text
operator has no effect with earlier versions of the text
index.$text
运算符的$diacriticSensitive
选项对早期版本的text
索引没有影响。
Diacritic Sensitive Search ProcessDiacritic敏感搜索过程
To perform a diacritic sensitive text search (要对版本3的文本索引执行区分重音符号的文本搜索($diacriticSensitive: true
) against a version 3 text
index, the $text
operator:$diacriticSensitive: true
),$text
运算符:
First searches the首先搜索不区分重音符号的text
index, which is diacritic insensitive.text
索引。Then, to return just the documents that match the diacritic marked characters of the search terms, the然后,为了只返回与搜索词的变音符号标记字符匹配的文档,$text
query operation includes an additional stage to filter out the documents that do not match.$text
查询操作包括一个额外的阶段来筛选出不匹配的文档。
Specifying 指定$diacriticSensitive: true
may impact performance.$diacriticSensitive: true
可能会影响性能。
To perform a diacritic sensitive search against an earlier version of the 若要对早期版本的text
index, the $text
operator searches the text
index, which is diacritic sensitive.text
索引执行区分重音符号的搜索,$text
运算符将搜索区分重音字符的文本索引。
For diacritic sensitive search, if the suffix stem contains the diacritic mark or marks, the 对于区分变音符号的搜索,如果后缀词干包含一个或多个变音符号标记,$text
operator matches on the exact word.$text
运算符将与确切的单词匹配。
See also: 另请参阅:
Text Score文本分数
The $text
operator assigns a score to each document that contains the search term in the indexed fields. $text
运算符为索引字段中包含搜索词的每个文档指定一个分数。The score represents the relevance of a document to a given text search query. 分数表示文档与给定文本搜索查询的相关性。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
字段上具有版本3的文本索引:
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 }
] )
Search for a Single Word搜索单个单词
The following query specifies a 以下查询指定$search
string of coffee
:coffee
的$search
字符串:
db.articles.find( { $text: { $search: "coffee" } } )
This query 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 }
See also: 另请参阅:
Match Any of the Search Terms匹配任何搜索词
If the search string is a space-delimited string, 如果搜索字符串是空格分隔的字符串,$text
operator performs a logical OR
search on each term and returns documents that contains any of the terms.$text
运算符将对每个术语执行逻辑OR搜索,并返回包含任何术语的文档。
The following query 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 query returns documents that contain either 此查询返回的文档在索引主题字段中包含bake
or coffee
or cake
in the indexed subject
field, or more precisely, the stemmed version of these words:bake
、coffee
或cake
,或者更准确地说,这些单词的词干版本:
{ "_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 }
See also: 另请参阅:
Search for a Phrase搜索短语
To match the exact phrase as a single term, escape the quotes.要将确切的短语匹配为单个术语,请转义引号。
The following query searches for the phrase 以下查询搜索短语coffee shop
:coffee shop
:
db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
This query returns documents that contain the phrase 此查询返回包含短语coffee shop
:coffee shop
:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
The following query searches for the phrases 以下查询搜索短语coffee shop
and Cafe con Leche
. This is a logical OR of the two phrases.coffee shop
和Cafe con Leche
。这是两个短语的逻辑或。
db.articles.find( { $text: { $search: "\'coffee shop\' \'Cafe con Leche\'" } } )
This query returns documents that contain both the phrases, including documents that contain terms from both the phrases:此查询返回同时包含这两个短语的文档,包括包含两个短语中的术语的文档:
[
{ _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 }
]
See also: 另请参阅:
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 will exclude the documents that contain those terms from the results.$text
运算符将从结果中排除包含这些术语的文档。
The following example searches for documents that contain the words 以下示例搜索包含单词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 query returns the following documents:查询返回以下文档:
{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }
See also: 另请参阅:
Search 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
字段可以指定一种语言,用于确定搜索字符串的停止词列表以及词干生成器和标记生成器的规则。
If you specify a 如果指定default_language
value of none
, then the text index parses through each word in the field, including stop words, and ignores suffix stemming.default_language
值none
,则文本索引将解析字段中的每个单词,包括停止词,并忽略后缀词干。
The following query 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 query 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 for the supported languages.$text
表达式也可以接受spanish
的名称。有关支持的语言,请参阅文本搜索语言。
See also: 另请参阅:
Case and Diacritic Insensitive Search大小写和变音不敏感搜索
The $text
operator defers to the case and diacritic insensitivity of the text
index. $text
运算符遵循text
索引不区分大小写和变音符号的特性。The version 3 版本3的text
index is diacritic insensitive and expands its case insensitivity to include the Cyrillic alphabet as well as characters with diacritics. text
索引不区分变音符号,并扩展了不区分大小写的功能,包括西里尔字母以及带有变音符号的字符。For details, see text Index Case Insensitivity and text Index Diacritic Insensitivity.有关详细信息,请参阅文本索引大小写不敏感度和文本索引音调不敏感度。
The following query performs a case and diacritic insensitive text search for the terms 以下查询对术语сы́рники
or CAFÉS
:сы́рники
或CAFÉS
执行不区分大小写和变音符号的文本搜索:
db.articles.find( { $text: { $search: "сы́рники CAFÉS" } } )
Using the version 3 使用版本3的text
index, the query matches the following documents.text
索引,查询将匹配以下文档。
{ "_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
索引,查询将不匹配任何文档。
Perform Case Sensitive Search执行区分大小写的搜索
To enable case sensitive search, specify 要启用区分大小写的搜索,请指定$caseSensitive: true
. $caseSensitive: true
。Specifying 指定$caseSensitive: true
may impact performance.$caseSensitive: true
可能会影响性能。
Case Sensitive Search for a Term区分大小写搜索术语
The following query performs a case sensitive search for the term 以下查询对术语Coffee
:Coffee
执行区分大小写的搜索:
db.articles.find( { $text: { $search: "Coffee", $caseSensitive: true } } )
The search matches just the document:搜索仅匹配文档:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
Case Sensitive Search for a Phrase区分大小写搜索短语
The following query performs a case sensitive search for the phrase 以下查询对短语Café Con Leche
:Café Con Leche
执行区分大小写的搜索:
db.articles.find( {
$text: { $search: "\"Café Con Leche\"", $caseSensitive: true }
} )
The search matches just the 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. $text
运算符将从结果中排除包含这些术语的文档。You can also specify case sensitivity for negated terms.您还可以为否定项指定区分大小写。
The following example performs a case sensitive search 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 query matches the following document:查询与以下文档匹配:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg" }
See also: 另请参阅:
Diacritic Sensitive Search变音敏感搜索
To enable diacritic sensitive search against a version 3 text index, specify 要对版本3的$diacriticSensitive: true
. text
索引启用区分重音符号的搜索,请指定$diacriticSensitive: true
。Specifying 指定$diacriticSensitive: true
may impact performance.$diacriticSensitive: true
可能会影响性能。
Diacritic Sensitive Search for a Term术语的变音敏感搜索
The following query performs a diacritic sensitive text search on the term 以下查询对术语CAFÉ
, or more precisely the stemmed version of the word:CAFÉ
或更准确地说是单词的词干版本执行变音敏感文本搜索:
db.articles.find( { $text: { $search: "CAFÉ", $diacriticSensitive: true } } )
The query 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. $diacriticSensitive
选项也适用于否定项。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.$text
运算符将从结果中排除包含这些术语的文档。
The following query performs a diacritic sensitive text search for document that contains 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 query matches the following document:查询与以下文档匹配:
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz" }
Text Search Score Examples文本搜索分数示例
Return the Text Search Score返回文本搜索分数
The following query performs a text search 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 }
See also: 另请参阅:
Sort by Text Search Score按文本搜索分数排序
Starting in MongoDB 4.4, you can specify the从MongoDB 4.4开始,您可以在{ $meta: "textScore" }
expression in thesort()
without also specifying the expression in the projection.sort()
中指定{ $meta: "textScore" }
表达式,而无需在投影中指定表达式。For example,例如:db.articles.find(
{ $text: { $search: "cake" } }
).sort( { score: { $meta: "textScore" } } )As a result, you can sort the resulting documents by their search relevance without projecting the因此,您可以根据搜索相关性对结果文档进行排序,而无需投影textScore
.textScore
。
In earlier versions, to include在早期版本中,要在{ $meta: "textScore" }
expression in thesort()
, you must also include the same expression in the projection.sort()
中包含{ $meta: "textScore" }
表达式,还必须在投影中包含相同的表达式。Starting in MongoDB 4.4, if you include the从MongoDB 4.4开始,如果在{ $meta: "textScore" }
expression in both the projection andsort()
, the projection and sort documents can have different field names for the expression.projection
和sort()
中都包含{ $meta: "textScore" }
表达式,则投影和排序文档可以为该表达式使用不同的字段名。
For example, in the following operation, the projection uses a field named例如,在下面的操作中,投影使用名为score
for the expression and thesort()
uses the field namedignoredName
.score
的字段作为表达式,sort()
使用名为ignoredName
的字段。db.articles.find(
{ $text: { $search: "cake" } } ,
{ score: { $meta: "textScore" } }
).sort( { ignoredName: { $meta: "textScore" } } )In previous versions of MongoDB, if在MongoDB的早期版本中,如果投影和排序中都包含{ $meta: "textScore" }
is included in both the projection and sort, you must specify the same field name for the expression.{ $meta: "textScore" }
,则必须为表达式指定相同的字段名。
In MongoDB 4.2, to sort by the text score, include the same在MongoDB 4.2中,要根据文本得分进行排序,请在投影文档和排序表达式中都包含相同的$meta
expression in both the projection document and the sort expression. The following query searches for the termcoffee
and sorts the results by the descending score:$meta
表达式。以下查询搜索术语coffee
,并按降序对结果进行排序:db.articles.find(
{ $text: { $search: "coffee" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )The query returns the matching documents sorted by descending score.查询返回按降序排列的匹配文档。
See also: 另请参阅:
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 query searches 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)
See also: 另请参阅:
Text Search with Additional Query and Sort Expressions带有附加查询和排序表达式的文本搜索
The following query searches for documents where the 以下查询搜索author
equals "xyz"
and the indexed field subject
contains the terms coffee
or bake
. author
等于"xyz"
并且索引字段subject
包含术语coffee
或bake
的文档。The operation also specifies a sort order of ascending 该操作还指定了按date
, then descending text search score:date
升序,然后按文本搜索分数降序排列的排序顺序:
db.articles.find(
{ author: "xyz", $text: { $search: "coffee bake" } },
{ score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )