MongoDB Logo
  • Data Modeling >
  • Model Data to Support Keyword Search

Model Data to Support Keyword Search¶

注意

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 Search 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. 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. 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.

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:

db.volumes.ensureIndex( { 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.

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:

  • Stemming. Keyword queries in MongoDB can not parse keywords for root or related words.
  • 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.
  • <   Model Tree Structures with Nested Sets
  • /\  Data Modeling
  • Aggregation  >

MongoDB 手册 2.4

Contents
  • Install MongoDB
  • 管理
  • 安全
  • Core MongoDB Operations (CRUD)
  • Data Modeling
    • Data Modeling Considerations for MongoDB Applications
    • Model Embedded One-to-One Relationships Between Documents
    • Model Embedded One-to-Many Relationships Between Documents
    • Model Referenced One-to-Many Relationships Between Documents
    • Model Data for Atomic Operations
    • Model Tree Structures with Parent References
    • Model Tree Structures with Child References
    • Model Tree Structures with an Array of Ancestors
    • Model Tree Structures with Materialized Paths
    • Model Tree Structures with Nested Sets
    • Model Data to Support Keyword Search
  • 聚合
  • 索引
  • 复制
  • 分片
  • 应用程序开发
  • The mongo Shell
  • 用例
  • 常见问题
  • 参考
  • 版本注释
  • 关于 MongoDB 文档
索引

格式

  • MongoDB 手册,单个 HTML 页面
  • MongoDB 手册,PDF 格式
  • MongoDB 手册,ePub 格式

关于 MongoDB

  • 简介
  • User Community
  • MongoDB 主复制
  • Planet MongoDB

MongoDB Ecosystem

  • Drivers and Client libraries
    • C (docs)
    • C++ (docs)
    • C# (docs)
    • Erlang (docs)
    • Haskell (docs)
    • Java (docs)
    • JavaScript (docs)
    • Perl (docs)
    • PHP (docs)
    • Python (docs)
    • Ruby (docs)
    • Scala (docs)
  • Tools and Integration
  • Platform Integration

MongoDB Resources

  • 下载
  • MongoDB 事件
  • 幻灯片及视频
  • MongoDB 监控服务 (docs)
  • 论坛
  • 博客
  • 下载
  • 驱动
  • 事件
  • 翻译
  • 编辑本页面
  • GitHub
  • Report a Problem

© Copyright 2011-2013, 10gen, Inc. MongoDB®, Mongo®, and the leaf logo are registered trademarks of 10gen, Inc.