WildBill
WildBill

Reputation: 9291

Execution engine not printing results

I have a simple Query method that runs cypher queries as noted below. If I run the EXACT same query in the web console (yes, same db instance, correct path), I get a non-empty iterator in the console. Shouldn't I 1) not get that message and 2) get the results I see in my database?

This class has other methods that add data to the database and that functionality works well. This query method is not working...

Class:

import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.IteratorUtil;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.sql.*;

public class NeoProcessor {

    //private GraphDatabaseService handle;
    private static final String DB_PATH = "/usr/local/Cellar/neo4j/2.0.1/libexec/data/new_graph.db";
    static GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );


    public NeoProcessor()
    {
    }


    public void myQuery(String cypherText) 
    {
        //System.out.println("executing the above query");
        cypherText = "MATCH (n:Phone{id:'you'}) MATCH n-[r:calling]->m WHERE n<>m RETURN n, r, m";

        ExecutionEngine engine = new ExecutionEngine( this.graphDb );

        ExecutionResult result;
        try ( Transaction ignored = graphDb.beginTx() )
        {
            result = engine.execute( cypherText + ";");
            System.out.println(result);

            ignored.success();
        }

    }
}

Below is a pic showing how the query rreturns results from the DB:

enter image description here

Upvotes: 1

Views: 296

Answers (3)

phil_20686
phil_20686

Reputation: 4080

You should be calling:

System.out.println(result.dumpToString)

Which will prettify it for you. Of course, there is always the possibility that your match returns no results. You shouuld also close the transaction in a finally block, although that won't matter much here.

EDIT: Taking a second look at this, your Cypher query is wrongly formed, It should be

MATCH (n:Phone) - [r:calling] -> (m) 
WHERE n.id = `you' 
RETURN n, r, m

Upvotes: 1

Unihedron
Unihedron

Reputation: 11051

result = engine.execute(cypherText + ";");
System.out.println(result.dumpToString());

Specified by: http://api.neo4j.org/2.0.3/org/neo4j/cypher/javacompat/ExecutionResult.html#dumpToString()

Upvotes: 1

Stefan Armbruster
Stefan Armbruster

Reputation: 39925

To consume the result you need to use the iterator. If you just want a string representation use the ExecutionResult.dumpToString(). Be aware this method exhausts the iterator.

Upvotes: 1

Related Questions