Search code examples
node.jsneo4jcyphernode-neo4j

How to abort query on demand using Neo4j drivers


I am making a search engine application using Node.js and Neo4j that allows users to submit a graph traversal query via a web-based user interface. I want to give users the option to cancel a query after it has been submitted (i.e. if a user decides to change the query parameters). Thus, I need a way to abort a query using either a command from a Node.js-to-Neo4j driver or via Cypher query.

After a few hours of searching, I haven't been able to find a way to do this using any of the Node.js-to-Neo4j drivers. I also can't seem to find a cypher query that allows killing of a query. Am I overlooking something, or is this not possible with Neo4j? I am currently using Neo4j 2.0.4, but I am willing to upgrade to a newer version of Neo4j if it has query killing capabilities.


Solution

  • Starting from Neo4j version 2.2.0, it's possible to kill queries from the UI and via the REST interface. I don't know if existing nodejs drivers support this feature, but if not you can still achieve the same functionality by making HTTP request to the REST interface.

    If you run a query in the browser in version 2.2.x or later, you will notice that there is an (X) close link at the top-right corner of the area where the queries are executed and displayed.

    You can achieve the same results by wrapping your queries in transactions and rolling them back. In fact, I just opened the web inspector to see how the browser UI was canceling the running queries.

    I think this would be the recommended approach:

    1. Begin a transaction with the Cypher query you want to run. The transaction will be assigned an identifier, i.e. request POST http://localhost:7474/db/data/transaction and response with Location: http://localhost:7474/db/data/transaction/7
    2. If you want to cancel the query, delete the transaction using the identifier you got in step 1, i.e. DELETE http://localhost:7474/db/data/transaction/7

    You will find more info about the Transactional Cypher HTTP Endpoint and examples in the official docs.

    Update: node-neo4j seems to support transaction handling, including rollback. See docs.