Search code examples
scalacypheranormcypher

Cypher: find path using Scala AnormCypher?


AnormCypher doc provides an example how to retriev data using the Stream API: http://anormcypher.org/

"The first way to access the results of a return query is to use the Stream API.

When you call apply() on any Cypher statement, you will receive a lazy Stream of CypherRow instances, where each row can be seen as a dictionary:

 // Create Cypher query
 val allCountries = Cypher("start n=node(*) where n.type = 'Country' return n.code as code, n.name as name")

 // Transform the resulting Stream[CypherRow] to a List[(String,String)]
 val countries = allCountries.apply().map(row => 
   row[String]("code") -> row[String]("name")
 ).toList

I am trying to use the same aproach to get path with the following Cypher query:

MATCH p = (n {id: 'n5'})-[*]-(m) RETURN p;

Yet, when running this code:

Cypher("MATCH p = (n {id: 'n5'})-[*]-(m) RETURN p;")().map {row  =>
  println(row[Option[org.anormcypher.NeoRelationship]]("p"))
}

I get exception (see below). How to get path info from CypherRow in this case?

 Exception in thread "main" java.lang.RuntimeException: TypeDoesNotMatch(Unexpected type while building a relationship)
    at org.anormcypher.MayErr$$anonfun$get$1.apply(Utils.scala:21)
    at org.anormcypher.MayErr$$anonfun$get$1.apply(Utils.scala:21)
    at scala.util.Either.fold(Either.scala:97)
    at org.anormcypher.MayErr.get(Utils.scala:21)
    at org.anormcypher.CypherRow$class.apply(AnormCypher.scala:303)
    at org.anormcypher.CypherResultRow.apply(AnormCypher.scala:309)
    at bigdata.test.n4j.Simple$$anonfun$main$1.apply(Simple.scala:31)
    at bigdata.test.n4j.Simple$$anonfun$main$1.apply(Simple.scala:29)
    at scala.collection.immutable.Stream.map(Stream.scala:376)
    at bigdata.test.n4j.Simple$.main(Simple.scala:29)

Solution

  • Paths in Cypher were changed as of 2.0, so you can't work with them easily directly, as they're not collections. There probably should be a new Path type of some sort in AnormCypher, but for now you can use paths along with relationships() or nodes().

    For example, you could do this to extract the relationships:

    Cypher("MATCH p = (n {id: 'n5'})-[*]-(m) RETURN relationships(p);")().map {row  =>
      println(row[Seq[NeoRelationship]]("relationships(p)"))
    }