【发布时间】:2014-07-12 19:02:01
【问题描述】:
我正在尝试提供一个 API 来根据各种条件搜索 MongoDB 集合,包括全文搜索。由于这是一个 Scala 项目(在 Play FWIW 中),我使用的是 Salat,一个围绕 Casbah 的抽象。
以下代码可以正常工作:
MySalatDao
.find(MongoDBObject("$text" -> MongoDBObject("$search" -> "Vidya")), MongoDBObject("score" -> MongoDBObject("$meta" -> "textScore")))
.sort(orderBy = MongoDBObject("score" -> MongoDBObject("$meta" -> "textScore")))
但是,我最终需要根据多个条件进行搜索并按全文搜索分数对结果进行排序,因此我探索了 Casbah 的 MongoDBObject query builder functionality(在底部)。
所以我尝试像这样复制上面的内容:
val builder = MongoDBObject.newBuilder
builder += "$text" -> MongoDBObject("$search" -> "Vidya")
builder += "score" -> MongoDBObject("$meta" -> "textScore")
MySalatDao
.find(a.result())
.sort(orderBy = MongoDBObject("score" -> MongoDBObject("$meta" -> "textScore")))
这给出了以下异常:
com.mongodb.MongoException: Can't canonicalize query: BadValue must have $meta projection for all $meta sort keys
at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214)
at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
at com.mongodb.QueryResultIterator.<init>(QueryResultIterator.java:64)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:86)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
.
.
我以前见过这个错误——当我没有在查询中包含score 组件时。但是一旦我这样做了,它就起作用了(如第一个代码 sn-p 所示),我认为带有查询生成器的版本是等效的。
就此而言,调用builder.result().toString() 会产生以下结果:
{ "$text" : { "$search" : "Vidya"} , "score" : { "$meta" : "textScore"}}
任何有关让查询生成器为我工作的帮助将不胜感激。
【问题讨论】:
-
您所做的是使查询成为查询的一部分(第一个参数),因此它是一个搜索条件,当您希望它成为要查找的第二个参数时。
标签: mongodb scala casbah salat