Model Data to Support Keyword Search支持键搜索的模型数据
On this page本页内容
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.结合多键索引,此模式可以支持应用程序的键搜索操作。
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.然后,您可以在数组上创建一个多键索引,并创建从数组中选择值的查询。
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. 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:例如:
db.volumes.findOne( { topics : "voyage" }, { title: 1 } )
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.。MongoDB同步构建索引,这意味着用于键索引的索引始终是最新的,并且可以实时操作。However, asynchronous bulk indexes may be more efficient for some kinds of content and workloads.但是,异步大容量索引对于某些类型的内容和工作负载可能更高效。