Mongodb:如何索引多个嵌套的文本字段? [英] Mongodb: How to index multiple nested text fields?

查看:147
本文介绍了Mongodb:如何索引多个嵌套的文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following kind of json in my database:

{ 
    "_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/"}
    } 
}

我的目标是为以下查询提供结果:

My goal is to have results for the following query:

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

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

It works when I create an index on all text fields

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

但是当我仅在结果"字段上创建索引时,不是这样:

But not when I create an index only over "results" field:

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

为什么?

推荐答案

问题是结果"不是字段,而是子文档.在MongoDB的文本字段上创建索引的语法要求您正确使用的所有字段"$ * "的符号,或所有文本字段的列表:

The problem is that "results" is not a field, it's a sub-document. The syntax for creating an index on text fields for MongoDB requires either the notation for all fields, "$*", which you are using correctly, or a list of all text fields:

创建文本索引

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

You can create a text index on the field or fields whose value is a string or an array of string elements. When creating a text index on multiple fields, you can specify the individual fields or you can use wildcard specifier ($**).

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

这篇关于Mongodb:如何索引多个嵌套的文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆