On this page本页内容
This tutorial describes how to specify the default language associated with the text index and also how to create text indexes for collections that contain documents in different languages.本教程介绍如何指定与文本索引关联的默认语言,以及如何为包含不同语言文档的集合创建文本索引。
text Indextext索引的默认语言The default language associated with the indexed data determines the rules to parse word roots (i.e. stemming) and ignore stop words. 与索引数据相关联的默认语言确定解析词根(即词干)和忽略停止词的规则。The default language for the indexed data is 索引数据的默认语言为english.english。
To specify a different language, use the 要指定不同的语言,请在创建文本索引时使用default_language option when creating the text index. default_language选项。See Text Search Languages for the languages available for 请参阅文本搜索语言以了解可用于default_language.default_language的语言。
The following example creates for the 以下示例为quotes collection a text index on the content field and sets the default_language to spanish:quotes集合在content字段上创建text索引,并将default_language设置为西班牙语spanish:
db.quotes.createIndex(
{ content : "text" },
{ default_language: "spanish" }
)
text Index for a Collection in Multiple Languagestext索引If a collection contains documents or embedded documents that are in different languages, include a field named 如果集合包含不同语言的文档或嵌入文档,请在文档或嵌入的文档中包含名为language in the documents or embedded documents and specify as its value the language for that document or embedded document.language的字段,并指定该文档或嵌入文件的语言作为其值。
MongoDB will use the specified language for that document or embedded document when building the 在构建text index:text索引时,MongoDB将使用该文档或嵌入文档的指定语言:
text index.text索引的默认语言。See Text Search Languages for a list of supported languages.有关支持的语言列表,请参阅文本搜索语言。
For example, a collection 例如,quotes contains multi-language documents that include the language field in the document and/or the embedded document as needed:quotes集合包含多语言文档,这些文档根据需要包含文档和/或嵌入文档中的language字段:
{
_id: 1,
language: "portuguese",
original: "A sorte protege os audazes.",
translation:
[
{
language: "english",
quote: "Fortune favors the bold."
},
{
language: "spanish",
quote: "La suerte protege a los audaces."
}
]
}
{
_id: 2,
language: "spanish",
original: "Nada hay más surrealista que la realidad.",
translation:
[
{
language: "english",
quote: "There is nothing more surreal than reality."
},
{
language: "french",
quote: "Il n'y a rien de plus surréaliste que la réalité."
}
]
}
{
_id: 3,
original: "is this a dagger which I see before me.",
translation:
{
language: "spanish",
quote: "Es este un puñal que veo delante de mí."
}
}
If you create a 如果在text index on the quote field with the default language of English.quote字段上创建默认语言为英语的text索引。
db.quotes.createIndex( { original: "text", "translation.quote": "text" } )
Then, for the documents and embedded documents that contain the 然后,对于包含language field, the text index uses that language to parse word stems and other linguistic characteristics.language字段的文档和嵌入文档,text索引使用该语言分析词干和其他语言特征。
For embedded documents that do not contain the 对于不包含language field,language字段的嵌入文档,
language field, then the index uses the document's language for the embedded document.language字段,则索引将使用文档的语言作为嵌入文档。For documents that do not contain the 对于不包含language field, the index uses the default language, which is English.language字段的文档,索引使用默认语言,即英语。
To use a field with a name other than 要使用名称不是language, include the language_override option when creating the index.language的字段,请在创建索引时包括language_override选项。
For example, give the following command to use 例如,给出以下命令以使用idioma as the field name instead of language:idioma作为字段名而不是language作为字段名:
db.quotes.createIndex( { quote : "text" },
{ language_override: "idioma" } )
The documents of the quotes collection may specify a language with the idioma field:quotes集合的文档可以使用idioma字段指定语言:
{ _id: 1, idioma: "portuguese", quote: "A sorte protege os audazes" }
{ _id: 2, idioma: "spanish", quote: "Nada hay más surrealista que la realidad." }
{ _id: 3, idioma: "english", quote: "is this a dagger which I see before me" }