由于某种原因,我在 mongodb 中的关键字搜索对一个关键字有效,但对另一个关键字无效,我该如何解决这个错误?

For some reason my keyword search in mongodb works for one keyword but not the other one, how do I fix it this error?

我从 Yelp 的数据集挑战中导入了 5 个巨大的 json 文件数据到我在 Ubuntu 上的 mongodb 中。这 5 个文件包含许多记录。我想搜索 MySQL 或其他结构化查询语言中的内容,因此我可以在 "text" 和 "Alcohol: full_bar" 下的属性和 return 下查找关键字 "UFC"他们的数量,至少。我还想看看提到 UFC 和 MMA 的酒吧是否比其他没有提到这些的酒吧获得更多的评论、签到和提示。我觉得这需要合并 business_id 变量。同样使问题复杂化的是 "tips.json" 也使用变量名称 "text" 就像 reviews.json".

我已经在我的 mongodb 数据库中成功建立了这个索引:

> db.collection.createIndex({"text":"text", "attributes": "text"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

我搜索关键字 UFC 的命令有效:

> db.collection.find({"$text": {"$search": "UFC"}})
{ "_id" : ObjectId("58fd4601051d56ff58e471f2"), "review_id" : "ogdaaLlAhmcyW1ZpGsiEGA", "user_id" : "rNbOmPzfWD1D4V8WOo7lBQ", "business_id" : "AVqjAx6j4HAvUb8t3_lv8Q", "stars" : 4, "date" : "2015-03-29", "text" : "We came here to watch the UFC.  We had fries and wings, and they did not disappoint.\nWe opted to sit in the upstairs area where it was less crowded, and less noisy.\nThe waitress was a total dummy, but her niceness kind of made up for it....\nIf she had an attitude, she would have received zero tip.", "useful" : 0, "funny" : 0, "cool" : 0, "type" : "review" }

......

但是当我试图在属性下找到 alchhol: full_bar 时,我得到以下错误:

> db.collection.find({"$attributes": {"$search": "Alcohol: full_bar"}})
error: {
    "$err" : "Can't canonicalize query: BadValue unknown top level operator: $attributes",
    "code" : 17287
}
> 

您的查询语法错误;您没有指定要搜索的字段名称,而是指定(使用特殊术语 $text)搜索应该通过文本索引 - 这意味着它将搜索您的 "text" 和"attributes" 个字段。

所以当您 运行 这个查询时:

db.collection.find({"$text": {"$search": "UFC"}})

这不仅限于 "text" 字段;它正在搜索整个文本索引,其中涵盖了 "text" 和 "attributes" 字段。

因此,如果您想在 "attributes" 字段中搜索一些文本,您可以采用相同的方式构建查询:

db.collection.find({"$text": {"$search": "Alcohol: full_bar"}})