【问题标题】:Mongodb: How to index multiple nested text fields?Mongodb:如何索引多个嵌套的文本字段?
【发布时间】:2014-10-09 06:55:43
【问题描述】:

我的数据库中有以下类型的 json:

{ 
    "_id" : "519817e508a16b447c00020e", "keyword" : "Just an example query", 
    "results" : 
    {
        "1" : {"base_domain" : "example1.com", "href" : "http://www.example1.com/"},
        "2" : { "base_domain" : "example2.com", "href" : "http://www.example2.com/"},
        "3" : { "base_domain" : "example3.com", "href" : "http://www.example3.com/"},
        "4" : { "base_domain" : "example4.com", "href" : "http://www.example4.com/"},
        "5" : { "base_domain" : "example5.com", "href" : "http://www.example5.com/"},
        "6" : { "base_domain" : "example6.com", "href" : "http://www.example6.com/"},
        "7" : { "base_domain" : "example7.com", "href" : "http://www.example7.com/"},
        "8" : { "base_domain" : "example8.com", "href" : "http://www.example8.com/"},
        "9" : { "base_domain" : "example9.com", "href" : "http://www.example9.com/"},
        "10" : { "base_domain" : "example10.com", "href" : "http://www.example10.com/"}
    } 
}

我的目标是获得以下查询的结果:

> db.ranking.find({ $text: { $search: "http://www.example9.com"}})

当我在所有文本字段上创建索引时它会起作用

> db.ranking.ensureIndex({ "$**": "text" }))

但不是当我只在“结果”字段上创建索引时:

> db.ranking.ensureIndex( {"results" : "text"} )

为什么?

【问题讨论】:

    标签: mongodb search indexing


    【解决方案1】:

    问题是“结果”不是一个字段,它是一个子文档。在 MongoDB 的文本字段上创建索引的语法需要所有字段的表示法,“$*”,您使用正确,或者所有文本字段的列表:

    创建文本索引

    您可以在一个或多个值为 a 的字段上创建文本索引 字符串或字符串元素数组。在创建文本索引时 多个字段,您可以指定单个字段,也可以使用 通配符说明符 ($**)。

    http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

    在你的情况下看起来像:

    db.ranking.ensureIndex(
                               {
                                 "keyword": "text",
                                 "results.1.href": "text",
                                 "results.1.href": "text",
                                 "results.2.href": "text",
                                 "results.3.href": "text",
                                 "results.4.href": "text",
                                 "results.5.href": "text",
                                 "results.6.href": "text",
                                 "results.7.href": "text",
                                 "results.8.href": "text",
                                 "results.9.href": "text",
                                 "results.10.href": "text"
                               }
                           )
    

    【讨论】:

    • 如果结果是一个数组,这可能会变得更有效率。
    • 非常有趣!索引第一个 href 也适用于其余部分:db.ranking.ensureIndex({"results.1.href": "text"}) 现在,有没有一种方法可以直接检索键和值而无需遍历所有字典? @JohnBarça 是的,我希望它是一个数组!
    • @JohnBarça 您提到使用数组。您的意思是为此更改数据库结构:{ "_id" : "519817e508a16b447c00020e", "keyword" : "Just an example query", "results" : [{"base_domain" : "example1.com", "href" : "http://www.example1.com/"},{"base_domain" : "example2.com", "href" : "http://www.example2.com/"},...}] 索引也会被散列或文本?
    • @JohnBarça 还有其他提示吗?
    • 我刚刚问了另一个问题,因为我遇到了Index key pattern too large error with code 67,因为我有 100 个结果而不是 10 个!
    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多