NoSQL Zone is brought to you in partnership with:

Andreas Kollegger is a leading speaker and writer on graph databases and Neo4j and the bridge between community and developer efforts. He works actively in the community, speaking around the world and promoting the larger Neo4j ecosystem of projects. Author of Fair Trade Software, and the lead for Neo4j in the cloud, Andreas plays a valuable role for progressive happenings within Neo4j. Andreas is a DZone MVB and is not an employee of DZone and has posted 75 posts at DZone. You can read more from them at their website. View Full User Profile

Neo4j 2.0 Milestone 3

05.30.2013
| 3288 views |
  • submit to reddit
The latest M03 milestone release of Neo4j 2.0 is as you expected all about improvements to Cypher. This blog post also discusses some changes made in the last milestone (M02) which we didn’t fully cover.


MERGECypher now contains a MERGE clause which is pretty big: It will be replacing CREATE UNIQUE as it also takes indexes and labels into accounts and can even be used for single node creation. MERGE either matches the graph and returns what is there (one or more results) or if it doesn’t find anything it creates the path given. So after the MERGE operation completes, Neo4j guarantees that the declared pattern is there.


We also added additional clauses to the MERGE statement which allow you to create or update properties as a function of whether the node was matched or created. Please note that -- as patterns can contain multiple named nodes and relationships -- you will have to specify the element for which you want to trigger an update operation upon creation or match.
MERGE (keanu:Person { name:'Keanu Reeves' })
ON CREATE keanu SET keanu.created = timestamp()
ON MATCH  keanu SET keanu.lastSeen = timestamp()
RETURN keanu
We put MERGE out to mainly collect feedback on the syntax and usage, there are still some caveats, like not grabbing locks for unique creation so you might end up with duplicate nodes for now. That will all be fixed by the final release.


Going along with MERGE, MATCH now also supports single node patterns, both with and without labels.


Cypher ChangesTwo new functions startNode(rel) and endNode(rel) allow quick access to both ends of a relationship.Besides the existing USING index hints, you can now also require Cypher to scan the given labels for nodes (if it doesn’t do so automatically).The Cypher ExecutionResult is now closeable and will immediately release resources upon closing. It’s no longer necessary to exhaust it.We fixed an issue with UNION and textual output, close readonly index results and fixed an issue where index lookups failed with literal collections.
Transactional Cypher HTTP endpointAlready with Milestone 2 we added a new Cypher HTTP endpoint for allowing transactions to span multiple HTTP requests. This endpoint only works with Cypher statements, of which you can post multiple in a single go. It streams data from and to the server and has a much more concise format for returning the data. Returned Nodes and relationships are just represented by their properties (a JSON map). Aside from improved support for transactionality, this API should perform better than the existing (older) Cypher REST Endpoint because of the decreased verbosity of the response.You can use this endpoint to post a bunch of statements in one go, or post multiple read and write statements in sequence. Rollbacks are requested by HTTP DELETE requests and commits by POSTing to a commit URL.
Here is an example session:
Create Transaction and do some work.
>> POST http://localhost:7474/db/data/transaction
{ "statements" : [ {
    "statement" : "CREATE (n {props}) RETURN n",
    "parameters" : { "props" : { "name": "My Node" } }
} ] }


<< 201: Created
Location: http://localhost:7474/db/data/transaction/3
{
  "commit" : "http://localhost:7474/db/data/transaction/3/commit",
  "results" : [ { "columns" : [ "n" ],
  "data" : [ [ { "name" : "My Node" } ] ] } ],
  "transaction" : { "expires" : "Tue, 28 May 2013 13:19:59 +0000" },
  "errors" : [ ]
}


Commit transaction
>> POST http://localhost:7474/db/data/transaction/3/commit
{ "statements" : [ {
    "statement" : "MATCH n RETURN id(n)"
  } ] }


<< 200: OK
{
  "results" : [ { "columns" : [ "id(n)" ],
  "data" : [ [ 2 ] ] } ],
  "errors" : [ ]
}
The endpoint now supports transaction timeouts and keep-alive.

PeripheryIn the Neo4j-Shell we added commands for listing automatic indexes and their state.Also the BatchInserter is now label aware, allowing you to create initial Neo4j stores containing labels.

Breaking ChangesMilestone 2 contained some breaking changes:

  • Replaced protected fields from org.neo4j.graphdb.factory.GraphDatabaseFactory
  • Removed org.neo4j.graphdb.index.BatchInserterIndex and BatchInserterIndexProvider please use the ones in the package org.neo4j.unsafe.batchinsert
  • The BatchInserter and the BatchGraphDatabase are not binary compatible with 1.9 due to some methods now taking a varargs array of labels as last argument, please recompile your code
  • We removed the alternative WITH syntax (==== a,b,c ====)


As always we welcome you to try out the new milestone release and report back any feedback, issues or suggestions.

Latest documentation is here. You can also find Neo4j 2.0.0-M03 on maven central.
Thanks a lot to Michael Bach, Rich Simon, Robert Herschke, Wes Freeman, Aseem Kishore, Morteza Milani, Javier de la Rosa and many more for great feedback on Neo4j 2.0. Keep it coming.


Published at DZone with permission of Andreas Kollegger, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)