Rigoberto Peña
Rigoberto Peña

Reputation: 480

Why Cypher query returns a null value in average function?

I am working with a Neo4j (2.2.0 M2) embedded graph application. For query this graph I use the Cypher way, using the GraphDatabaseService class and its method execute(query, params), wich returns a org.neo4j.graphdb.Result instance. The problem is when I use a aggregated function (avg) in the query:

MATCH 
(e:Person {person_id: {id} }) <-[:ASIGNED_TO]-(t:Task) 
WHERE t.start_date >= {cuttof_date} 
RETURN AVG(t.estimated_time) as avgte

For briefly understand of this query: id and cuttof_date are parameters I put then in a Mapset, last of them is a date in UTC (long) and comparable. The result of this query, using the Neo4j browser manager, and giving some values to the specified parameters, is the following:

avgte

6.166666666666667

Returned 1 row in 665 ms.

Perfect!, now, when I expect the same result using Cypher in the Java code (I have avoided "try", and "with resources"):

    //Same params
    int id  = 187;
    long longDate = 1357016400000L;

    Map<String,Object> params= new HashMap<>();
    params.put("id", id);
    params.put("cuttof_date", longDate);

    //same query
    String query = "MATCH"+
      "(e:Person {person_id: {id} }) <-[:ASIGNED_TO]-(t:Task) " +
      "WHERE t.start_date >= {cuttof_date} " +
      "RETURN AVG(t.estimated_time) as avgte"; 

    GraphDatabaseService gdbs = new 
                GraphDatabaseFactory().
                newEmbeddedDatabase(DB_NAME) ;

    Result result = gdbs.execute(query, params);
    Map<String, Object> firstRow = result.next();
    Object avgteObject = firstRow.get("avgte");

    //Using the most basic transformation: as String
    System.out.println("AVGTE: "+String.valueOf(avgteObject));

The result is: "AVGTE: null". How can this be possible!? Also I've used other classes like ExtendedExecutionResult, and ExecutionResult from org.neo4j.cypher package, but also returns the same output. The problem is not the Query, it works perfectly so I think it is when using the Cypher classes, it doesn't load the value of the aggregated function.

Any ideas??

Upvotes: 2

Views: 509

Answers (2)

Rigoberto Pe&#241;a
Rigoberto Pe&#241;a

Reputation: 480

My bad: creating a empty database. The snipet code: DB_NAME takes the following value: "gespro_graph_db"

GraphDatabaseService gdbs = new 
                GraphDatabaseFactory().
                newEmbeddedDatabase(DB_NAME) ;

It works if the graph database location is in the same project path, but it is in folder structure like this: /project_path/gespro_graph/gespro_graph_db/, where last folder is the real location. My project was pointing to /project_path/gespro_graph_db, and this is empty.

Upvotes: 1

Michael Hunger
Michael Hunger

Reputation: 41676

Perhaps you have an node without estimated_time property in your db?

Try to return t or t.estimated_time to see the raw data

Upvotes: 2

Related Questions