On this page本页内容
Note
Keyword search is not the same as text search or full text search, and does not provide stemming or other text-processing features. 关键字搜索与文本搜索或全文搜索不同,不提供词干分析或其他文本处理功能。See the Limitations of Keyword Indexes section for more information.有关更多信息,请参阅关键字索引的限制部分。
In 2.4, MongoDB provides a text search feature. 在2.4中,MongoDB提供了文本搜索功能。See Text Indexes for more information.有关更多信息,请参阅文本索引。
If your application needs to perform queries on the content of a field that holds text you can perform exact matches on the text or use 如果应用程序需要对包含文本的字段的内容执行查询,则可以对文本执行精确匹配,或者使用$regex
to use regular expression pattern matches. $regex
来使用正则表达式模式匹配。However, for many operations on text, these methods do not satisfy application requirements.然而,对于文本上的许多操作,这些方法不能满足应用要求。
This pattern describes one method for supporting keyword search using MongoDB to support application search functionality, that uses keywords stored in an array in the same document as the text field. 该模式描述了一种支持关键字搜索的方法,使用MongoDB支持应用程序搜索功能,该方法使用与文本字段存储在同一文档中的数组中的关键字。Combined with a multi-key index, this pattern can support application’s keyword search operations.结合多键索引,该模式可以支持应用程序的关键字搜索操作。
To add structures to your document to support keyword-based queries, create an array field in your documents and add the keywords as strings in the array. 要向文档中添加结构以支持基于关键字的查询,请在文档中创建一个数组字段,并将关键字作为字符串添加到数组中。You can then create a multi-key index on the array and create queries that select values from the array.然后可以在数组上创建多键索引,并创建从数组中选择值的查询。
Example
Given a collection of library volumes that you want to provide topic-based search. 给定要提供基于主题搜索的库卷集合。For each volume, you add the array 对于每个卷,添加数组topics
, and you add as many keywords as needed for a given volume.topics
,并根据需要为给定卷添加任意多的关键字。
For the 对于《白鲸》一书,你可能有以下文档:Moby-Dick
volume you might have the following document:
You then create a multi-key index on the 然后在topics
array:topics
数组上创建多键索引:
The multi-key index creates separate index entries for each keyword in the 多关键字索引为topics
array. topics
数组中的每个关键字创建单独的索引项。For example the index contains one entry for 例如,索引包含一个捕鲸(whaling
and another for allegory
.whaling
)条目和另一个寓言(allegory
)条目。
You then query based on the keywords. 然后根据关键字进行查询。For example:例如:
Note
An array with a large number of elements, such as one with several hundreds or thousands of keywords will incur greater indexing costs on insertion.包含大量元素的数组(例如包含数百或数千个关键字的数组)在插入时会产生更大的索引成本。
MongoDB can support keyword searches using specific data models and multi-key indexes; however, these keyword indexes are not sufficient or comparable to full-text products in the following respects:MongoDB支持使用特定数据模型和多键索引进行关键词搜索;然而,这些关键字索引在以下方面不足以与全文产品相比: