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. See Text Indexes on Self-Managed Deployments for more information.在2.4版本中,MongoDB提供了文本搜索功能。有关更多信息,请参阅自我管理部署的文本索引。
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. However, for many operations on text, these methods do not satisfy application requirements.$regex使用正则表达式模式匹配。然而,对于文本上的许多操作,这些方法不能满足应用程序的要求。
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.结合多键索引,此模式可以支持应用程序的键搜索操作。
Pattern模式
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:Moby-Dick(白鲸记)一书,您可能有以下文档:
{ title : "Moby-Dick" ,
author : "Herman Melville" ,
published : 1851 ,
ISBN : 0451526996 ,
topics : [ "whaling" , "allegory" , "revenge" , "American" ,
"novel" , "nautical" , "voyage" , "Cape Cod" ]
}
You then create a multi-key index on the 然后在topics array:topics数组上创建一个多键索引:
db.volumes.createIndex( { topics: 1 } )
The multi-key index creates separate index entries for each keyword in the 多键索引为topics array. For example the index contains one entry for whaling and another for allegory.topics数组中的每个键创建单独的索引条目。例如,该索引包含一个whaling(捕鲸)条目和一个allegory(寓言)条目。
You then query based on the keywords. For example:然后根据键进行查询。例如:
db.volumes.findOne( { topics : "voyage" }, { title: 1 } )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.具有大量元素的数组,例如具有数百或数千个键的数组,在插入时将产生更大的索引成本。
Limitations of Keyword Indexes键索引的局限性
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可以使用特定的数据模型和多键索引支持键搜索;然而,这些键索引在以下方面不足以或无法与全文产品相比:
Stemming. Keyword queries in MongoDB can not parse keywords for root or related words.茎。MongoDB中的键查询无法解析根或相关词的键。Synonyms. Keyword-based search features must provide support for synonym or related queries in the application layer.同义词。基于键的搜索功能必须支持应用层中的同义词或相关查询。Ranking. The keyword look ups described in this document do not provide a way to weight results.排名。本文档中描述的键查找不提供对结果进行加权的方法。Asynchronous Indexing. MongoDB builds indexes synchronously, which means that the indexes used for keyword indexes are always current and can operate in real-time. However, asynchronous bulk indexes may be more efficient for some kinds of content and workloads.异步索引。MongoDB同步构建索引,这意味着用于键索引的索引始终是最新的,可以实时运行。然而,对于某些类型的内容和工作负载,异步批量索引可能更有效。