To retrieve documents from Couchbase by anything other than the document key requires querying a view and views are defined by map and reduce functions written in JavaScript. Consider a view that returns data like this:
1 2 3 4 5 6 |
|
And this Scala case class
to hold the documents retrieved from the view.
1
|
|
It’s a common scenario to retrieve multiple documents at once and the Java driver has a pretty straight forward api for that. The desired keys are simply specified as a json array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The Java driver deals with strings so it’s up to the client application to handle the json parsing. That was an excellent design decision and makes using the Java driver from Scala pretty painless. I’m using the Play Framework json libraries and an extension method _.as[TermOccurrence]
defined on ViewRow
to simplify the mapping of the response documents to Scala objects.
1 2 3 4 5 6 7 8 9 10 11 |
|
In order for this extension method to work, it requires an implicit Format[TermOccurrence]
which is defined on of the TermOccurrence
compainion object.
1 2 3 |
|