|
MongoDB 2.6 includes a $text query operator. There's a new projection syntax for including each result's relevance in the output. There's also a new syntax for sorting by relevance descending:
> db.test.createIndex({s: 'text'})
|
> db.test.insert({s: 'spam'})
|
> db.test.insert({s: 'spam eggs and spam'})
|
> db.test.insert({s: 'sausage and eggs'})
|
>
|
> db.test.find(
|
... // query
|
... {$text: {$search: 'spam'}},
|
... // new projection syntax
|
... {_id: false, s: true, score: {$meta: 'textScore'}}
|
... ).sort(
|
... // new sort syntax
|
... {score: {$meta: 'textScore'}}
|
)
|
{ "s" : "spam eggs and spam", "score" : 1.25 }
|
{ "s" : "spam", "score" : 1.1 }
|
Drivers probably already support the new projection syntax, since it's a similar structure to the $elemMatch projection operator.
If a driver has been validating that the value portion of a sort specifier is 1, -1, or a string, it should now also allow a subdocument as the value, possibly with $-prefixed field names.
|