Cassie
Cassie

Reputation: 3099

Passing parameters with Map to Neo4j query Scala

I have a query where I need to pass a lot of parameters using Neo4jDriver with Scala. I have not found any possibility to pass them as a map, but I only managed to pass them as a part of the query String. So it looks like this:

val arr = Array("18731", "41.84000015258789", "-87.62999725341797")
val query1 = "MATCH ()-relations traversal  WHERE mt.topic_id = gt.topic_id AND distance(point({ longitude: " + data(2) +  ", latitude: " + data(1) + "}),point({ longitude: v.lon, latitude: v.lat })) < 4000 AND mt.member_id = " + data(0) + " RETURN distinct g.group_name " +
          "as group_name, e.event_name as event_name, v.venue_name as venue_name"

And then I can run this query like neo4jSession.run(query1)

Is there any other way to pass these values from the array as parameters?

By using parameters I mean something like this:

val paramsMap = Map("lat" -> data(1).toDouble, "lon" -> data(2).toDouble, "id" -> data(0).toInt)

UPDATE:

In Scala, there is neo4jSession.run(query,paramsMap) the map should be of type Map[String,AnyRef], when my map is of type [String, Double] because I need to pass double and int values. So I do not know how I can use this method in my case.

Upvotes: 1

Views: 445

Answers (2)

Cassie
Cassie

Reputation: 3099

I managed to pass the parameters by changing them to AnyRef explicitly and the query works perfectly fine.

val paramsMap = Map("lat" -> data(1).toDouble.asInstanceOf[AnyRef], "lon" -> data(2).toDouble.asInstanceOf[AnyRef], "id" -> data(0).toInt.asInstanceOf[AnyRef])


    def toRow(record: Record):Seq[String] =
      fieldsToRetrieve.map(record.get(_).toString
      )

    neo4jSession.run(neo4jQueries.searchQuery, paramsMap.asJava)

Upvotes: 1

Avinash Anand
Avinash Anand

Reputation: 695

Try this example project readme page. Here it passes data as map.

Example code given there is:

Driver driver = GraphDatabase.driver("bolt://localhost");
String query = "MATCH (:Movie {title:{title}})<-[:ACTED_IN]-(a:Person) RETURN a.name as actor";

try (Session session = driver.session()) {

    StatementResult result = session.run(query, singletonMap("title", "The Matrix"));
    while (result.hasNext()) {
        System.out.println(result.next().get("actor"));
    }
}

Hope this helps.

EDIT: This example is in Java, but should be trivial to make it work in scala.

Upvotes: 1

Related Questions