【问题标题】:Is there any way to see all the indexed terms in a Mongodb text index?有什么方法可以查看 Mongodb 文本索引中的所有索引术语?
【发布时间】:2016-04-27 06:19:57
【问题描述】:

我正在尝试使我的 mongodb 集合可搜索。我可以在按文本索引集合后进行文本搜索

db.products.createIndex({title: 'text'})

我想知道是否可以检索此集合的所有索引词的列表。当人们编写搜索查询时,这对于自动完成和拼写检查/更正非常有用.

【问题讨论】:

    标签: mongodb search indexing


    【解决方案1】:

    MongoDB 中没有内置函数。但是,您可以通过聚合查询轻松获取此信息。

    假设您的集合包含以下文档:

    { "_id" : ObjectId("5874dbb1a1b342232b822827"), "title" : "title" }
    { "_id" : ObjectId("5874dbb8a1b342232b822828"), "title" : "new title" }
    { "_id" : ObjectId("5874dbbea1b342232b822829"), "title" : "hello world" }
    { "_id" : ObjectId("5874dbc6a1b342232b82282a"), "title" : "world title" }
    { "_id" : ObjectId("5874dbcaa1b342232b82282b"), "title" : "world meta" }
    { "_id" : ObjectId("5874dbcea1b342232b82282c"), "title" : "world meta title" }
    { "_id" : ObjectId("5874de7fa1b342232b82282e"), "title" : "something else" }
    

    这个查询会给我们关于单词的信息:

    db.products.aggregate([
       {
          $project:{
             words:{
                $split:["$title"," "]
             }
          }
       },
       {
          $unwind:"$words"
       },
       {
          $group:{
             _id:"$words",
             count:{
                $sum:1
             }
          }
       },
       {
          $sort:{
             count:-1
          }
       }
    ])
    

    这会输出每个单词的出现次数:

    { "_id" : "title", "count" : 4 }
    { "_id" : "world", "count" : 4 }
    { "_id" : "meta", "count" : 2 }
    { "_id" : "else", "count" : 1 }
    { "_id" : "something", "count" : 1 }
    { "_id" : "new", "count" : 1 }
    { "_id" : "hello", "count" : 1 }
    

    如果您使用的是 MongoDB 3.4,则可以使用新的 collation 选项获取单词的不区分大小写/不区分变音符号的统计信息。

    例如,假设我们的集合现在包含以下文档:

    { "_id" : ObjectId("5874e057a1b342232b82282f"), "title" : "title" }
    { "_id" : ObjectId("5874e05ea1b342232b822830"), "title" : "new Title" }
    { "_id" : ObjectId("5874e067a1b342232b822831"), "title" : "hello world" }
    { "_id" : ObjectId("5874e076a1b342232b822832"), "title" : "World Title" }
    { "_id" : ObjectId("5874e085a1b342232b822833"), "title" : "World méta" }
    { "_id" : ObjectId("5874e08ea1b342232b822834"), "title" : "World meta title" }
    { "_id" : ObjectId("5874e0aea1b342232b822835"), "title" : "something else" }
    

    将排序规则选项添加到聚合查询:

    db.products.aggregate([
       {
          $project:{
             words:{
                $split:["$title"," "]
             }
          }
       },
       {
          $unwind:"$words"
       },
       {
          $group:{
             _id:"$words",
             count:{
                $sum:1
             }
          }
       },
       {
          $sort:{
             count:-1
          }
       }
    ],
    {
       collation:{
          locale:"en_US",
          strength:1
       }
    })
    

    这将输出:

    { "_id" : "title", "count" : 4 }
    { "_id" : "world", "count" : 4 }
    { "_id" : "méta", "count" : 2 }
    { "_id" : "else", "count" : 1 }
    { "_id" : "something", "count" : 1 }
    { "_id" : "new", "count" : 1 }
    { "_id" : "hello", "count" : 1 }
    

    强度是要执行的比较水平:

     collation.strength: 1 // case insensitive + diacritic insensitive
     collation.strength: 2 // case insensitive only 
    

    【讨论】:

    • 非常感谢@felix。这是一个很好的答案,对我帮助很大!
    【解决方案2】:

    如果我们假设 autoCompleteTerm 是您的输入值,您可以使用以下查询获取标题列表:

    db.products.distinct('title', { $text: { $search: autoCompleteTerm } } )
    

    【讨论】:

      【解决方案3】:

      db.products.distinct("title"),你在找什么?

      【讨论】:

      • 不完全是。我正在寻找在 mongodb 建立标题索引后可搜索的术语,这是在所有产品的标题字段中找到的一组不同的单词,每个术语以某种方式指向标题包含此的产品列表。无论如何感谢您的回复,该功能可能也有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      相关资源
      最近更新 更多