Model Data to Support Keyword Search支持关键字搜索的模型数据¶

On this page本页内容

  • Pattern
  • Limitations of Keyword Indexes关键字索引的局限性

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.结合多键索引,该模式可以支持应用程序的关键字搜索操作。

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:对于《白鲸》一书,你可能有以下文档:

{ 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 } )

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. MongoDB同步构建索引,这意味着用于关键字索引的索引始终是最新的,并且可以实时运行。However, asynchronous bulk indexes may be more efficient for some kinds of content and workloads.但是,对于某些类型的内容和工作负载,异步批量索引可能更有效。