Mongodb - 多文本索引:索引键模式太大错误代码67 [英] Mongodb - Multiple text index: Index key pattern too large error code 67
问题描述
我有以下Mongodb数据库结构:
I have the following Mongodb database structure:
{
"_id" : "519817e508a16b447c00020e",
"keyword" : "Just an example query",
"rankings" :
{
results:
{
"1" : { "domain" : "example1.com", "href" : "http://www.example1.com/"},
"2" : { "domain" : "example2.com", "href" : "http://www.example2.com/"},
"3" : { "domain" : "example3.com", "href" : "http://www.example3.com/"},
"4" : { "domain" : "example4.com", "href" : "http://www.example4.com/"},
"5" : { "domain" : "example5.com", "href" : "http://www.example5.com/"},
...
...
"99" : { "domain" : "example99.com", "href" : "http://www.example99.com/"}
"100" : {"domain" : "example100.com", "href" : "http://www.example100.com/"}
},
"plus":"many",
"other":"not",
"interesting" : "stuff",
"for": "this question"
}
}
上一个问题中,我询问了如何索引文本,以便我可以使用例如搜索关键字和域名:
In a previous question, I asked how to index the text so that I could search for the keyword and domain using for example:
db.ranking.find({ $text: { $search: "\"example9.com\" \"Just an example query\""}})
John Petrone的精彩回答是:
The awesome answer by John Petrone was:
db.ranking.ensureIndex(
{
"keyword": "text",
"rankings.results.1.domain" : "text",
"rankings.results.2.domain" : "text",
...
...
"rankings.results.99.domain" : "text",
"rankings.results.100.domain" : "text"
}
然而,如果这只是grea当我有10个结果时,当我尝试索引100个结果时,我遇到来自Mongo shell的代码67的索引键模式太大错误。
However, if that works just great when I have 10 results, I run into an "Index key pattern too large" error with code 67 from Mongo shell when I try to index 100 results.
所以最大的问题是:
如何(地狱)我能解决索引键模式太大的错误?
How (the hell) can I resolve that "index key pattern too large" error?
编辑:18/08/2014
澄清的文件结构
18/08/2014 The document structure clarified
{
"_id" : "519817e508a16b447c00020e", #From Mongodb
"keyword" : "Just an example query",
"date" : "2014-03-28"
"rankings" :
{
"1" : { "domain" : "example1.com", "href" : "http://www.example1.com/", "plus" : "stuff1"},
...
"100" : {"domain" : "example100.com", "href" : "http://www.example100.com/"plus" : "stuff100"}"}
},
"plus":"many",
"other":"not",
"interesting" : "stuff",
"for": "this question"
}
推荐答案
所以,这是我的解决方案:
我决定坚持使用嵌入式文档进行过于简单的修改:替换包含实际排名的字典键包含等级的数组,它就是:
So, that's my solution: I decided to stick with the embedded document with an overly simple modification: Replacing dictionary keys containing the actual rank by an array containing the rank and that's it:
{
"_id" : "519817e508a16b447c00020e", #From Mongodb
"keyword" : "Just an example query",
"date" : "2014-03-28"
"rankings" :
[
{
"domain" : "example1.com", "href" : "http://www.example1.com/", "plus" : "stuff1", "rank" : 1
},
...
{
"domain" : "example100.com", "href" : "http://www.example100.com/"plus" : "stuff100", "rank" : 100
}
]
"plus":"many",
"more":"uninteresting",
"stuff" : "for",
"this": "question"
}
然后,我可以使用例如选择整个文档:
Then, I can select an entire document using for example:
> db.ranking.find({"keyword":"how are you doing", "rank_date" : "2014-08-27")
或者通过使用非常棒的投影和Mongodb 2.6中的新功能获得单个结果:-D
Or a single result by using projections which is just awesome and a new feature in Mongodb 2.6 :-D
> db.collection.find({ "rank_date" : "2014-04-09", "rankings.href": "http://www.example100.com/" }, { "rankings.$": 1 })
[
{
"domain" : "example100.com", "href" : "http://www.example100.com/", "plus" : "stuff100", "rank" : 100
},
]
甚至直接获得一个网址排名:
And even get one single url rank directly:
> db.collection.find({"rank_date" : "2014-04-09", "rankings.href": "http://www.example5.com/"}, { "rankings.$": 1 })[0]['rankings'][0]['rank']
5
最后,我还在创建基于url:
And finally, I'm also creating an index based on the url:
> db.collection.ensureIndex( {"rankings.href" : "text"} )
使用索引,我可以搜索单个网址,部分网址,子网域或整个域名,这样就可以了:
With the index, I can either search for a single url, a partial url, a subdomain or the entire domain so that's just great:
> db.collection.find({ $text: { $search: "example5.com"}})
真的就是这样!非常感谢大家的帮助,特别是@JohnBarça:-D
And that's it really! Thanks a lot for everyone's help, especially @JohnBarça :-D
这篇关于Mongodb - 多文本索引:索引键模式太大错误代码67的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!