试图找出如何以正确的方式在mongodb中实现此搜索

时间:2017-04-25 05:12:34

标签: mongodb mongodb-query word-frequency keyword-search database

现在我将一个巨大的yelp评论json文件导入到Linux上的mongodb数据库中。我想实现一种搜索关键字的正确方法,因此我可以在“text”下找到关键字“UFC”,在属性下查找“Alcohol:full_bar”。

这是我到目前为止所做的:

db.yelp.find({
  $or: [{
    "text": UFC /.*enjoy.*/i
  }, {
    "attributes": Alcohol: full_bar/.*good.*/i
  }]
})

这是我的json文件的示例:

{"time":["Fri-0:2","Sat-0:1","Sun-0:1","Wed-0:2","Sat-1:2","Thu-1:1","Wed-1:1","Sat-2:1","Sun-2:2","Thu-2:1","Wed-2:1","Fri-3:1","Sun-3:3","Thu-4:1","Tue-4:1","Sun-6:1","Wed-6:1","Fri-10:1","Sat-10:1","Mon-11:1","Wed-11:2","Mon-12:1","Sat-12:1","Tue-12:1","Sat-13:2","Thu-13:1","Tue-13:2","Wed-13:2","Fri-14:2","Sat-14:1","Wed-14:1","Fri-15:1","Sat-15:1","Thu-15:1","Tue-15:1","Fri-16:1","Sat-16:2","Sun-16:1","Tue-16:1","Sat-17:3","Sun-17:1","Fri-18:1","Mon-18:1","Sat-18:2","Sun-18:1","Tue-18:2","Wed-18:1","Fri-19:2","Mon-19:1","Sun-19:2","Thu-19:1","Wed-19:1","Mon-20:1","Sun-20:5","Thu-20:1","Tue-20:1","Wed-20:2","Fri-21:2","Sun-21:1","Thu-21:4","Tue-21:1","Wed-21:1","Fri-22:1","Thu-22:1","Fri-23:1","Mon-23:1","Sat-23:3","Sun-23:1","Thu-23:2","Tue-23:1"],"business_id":"7KPBkxAOEtb3QeIL9PEErg","type":"checkin"}
{"time":["Mon-13:1","Thu-13:1","Sat-16:1","Wed-17:1","Sun-19:1","Thu-20:1","Sat-21:1"],"business_id":"kREVIrSBbtqBhIYkTccQUg","type":"checkin"}
{"time":["Thu-0:1","Mon-1:1","Mon-12:1","Sat-16:1"],"business_id":"tJRDll5yqpZwehenzE2cSg","type":"checkin"}
{"time":["Fri-0:1","Sat-0:1","Sun-0:1","Thu-0:1","Wed-0:1","Fri-1:1","Sat-1:1","Tue-1:1","Mon-2:1","Sun-2:1","Thu-2:1","Sat-3:1","Thu-3:1","Wed-3:1","Mon-17:1","Sat-17:1","Sun-17:2","Tue-17:1","Fri-18:1","Mon-18:1","Sun-18:1","Mon-19:1","Mon-20:3","Sat-20:1","Thu-20:1","Mon-21:1","Thu-21:1","Sat-22:3","Sun-22:1","Tue-22:3","Wed-22:1","Sat-23:2","Tue-23:3"],"business_id":"nhZ1HGWD8lMErdn3FuWuTQ","type":"checkin"}
{"time":["Sat-11:1","Fri-13:1","Thu-14:1"],"business_id":"8bY6M2yiWOF2ilfmGS34Fw","type":"checkin"}
{"time":["Mon-0:1","Fri-1:1","Sun-1:1","Tue-1:1","Wed-11:1","Fri-12:1","Tue-12:1","Fri-13:1","Sun-15:1","Thu-16:1","Sat-17:1","Fri-18:1","Sun-18:1","Fri-20:1","Sat-20:2","Sun-20:1","Thu-20:2","Sat-21:1","Thu-21:1","Wed-23:1"],"business_id":"zNVot5_XHsxwfKdiFjk_aA","type":"checkin"}
{"time":["Fri-0:1","Sun-0:1","Wed-0:1","Sun-1:2","Tue-1:2","Sat-2:1","Sat-15:1","Fri-17:2","Sat-17:1","Thu-17:1","Tue-17:1","Wed-17:1","Sat-18:1","Sun-19:1","Thu-19:2","Fri-22:2","Sun-22:2","Thu-22:1","Tue-22:1","Fri-23:1","Sat-23:1","Thu-23:1","Tue-23:2","Wed-

1 个答案:

答案 0 :(得分:0)

首先,您应为textattributes字段创建text index

db.yelp.createIndex({ text: "text", attributes: "text" })

搜索本身应该使用$text运算符执行。

db.yelp.find({ $text: { $search: "UFC \"Alcohol: full_bar\"" } })

有关详细信息,请参阅docs