出于某种原因,我在mongodb中的关键字搜索适用于一个关键字但不适用于另一个关键字,如何修复此错误?

时间:2017-04-28 08:42:20

标签: json mongodb attributes word-frequency keyword-search

我从Yelp的数据集挑战中将5个巨大的json文件数据导入到Ubuntu上的mongodb中。这5个文件包含许多记录。我想搜索MySQL或其他结构化查询语言之类的东西,所以我可以在“text”下找到关键字“UFC”,在属性下查找“Alcohol:full_bar”,并至少返回它们的计数。我还想看看提到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
}
> 

1 个答案:

答案 0 :(得分:0)

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

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

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

这不仅限于"文本"领域;它正在搜索整个文本索引,其中包含" text"和"属性"字段。

所以,如果你想在"属性"中搜索一些文字。字段,您以相同的方式构造查询:

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