kclay / rethink-scala

Scala Driver for RethinkDB
Other
100 stars 24 forks source link

filter by string not work #35

Closed shengc closed 9 years ago

shengc commented 9 years ago

Sorry to be a pain in the ass. I was trying the 10 minute tutorial from rethink db website using scala driver, and got stuck in the step of "filter by author's name". What I am doing is as follows,

case class Post(title: String, content: String)

case class Author(
  name: String,
  tvShow: String,
  posts: List[Post]
) extends Document

import com.rethinkscala.Blocking._
import functional._

implicit val connection = Blocking(version)

val table = r.tableAs[Author]("authors")

table.filter({ f =>
  val name = f \ "name"
  name.string.run.map(_ == "William Adama").getOrElse(false)
}).run.match {
  case Failure(error) => throw error
  case Success(cursor) => cursor foreach println
}

it always ends up with this error,

ERROR [nioEventLoopGroup-2-2] (VersionHandler.scala:38) - UnCaught Exception token not resolved
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.rethinkscala.net.Frame] from Integral number (0); no single-int-arg constructor/factory method
 at [Source: {"t":17,"r":["r.row is not defined in this context."],"n":[],"b":[0]}; line: 1, column: 67

I also tried added JsonProperty("type") annotation to the name field of Author, not working either. I know I am probably doing something wrong in the code, but there is very little clue I can find in the readme or quickly start of the project. Could you take a look ?

kclay commented 9 years ago

I think you should do

table.filter(author=> author("name") === "Williams Adama").run

or
table.filter(author=> author\"name"  === "Williams Adama").run

or 
table.filter(author => author("name") eq "Williams Adama" ).run

You don't have to actually return the result inside the the filter.. run actually terminates the query and then executes on the server side.

Note sometimes scala will get confused with method like eq and ne (due to scala adds these methods to every object by default) for that reason there are custom methods that get around this limitation === (eq) =!= (ne)

This should fix your query but there is indeed a error in not being able to map the json error correctly

shengc commented 9 years ago

than you, that works! I actually tried another filter by size of the posts,

filter(author => (author \ "name").toSeq[Post] === 1)

it works too!

kclay commented 9 years ago

Actually surprised the toSeq query worked, seems a bit incorrect.. guess you meant

filter(author => (author \ "posts").toSeq[Post].count() === 1)

In any case glad you got it working