WildBill
WildBill

Reputation: 9291

How do you use parameters with neo4j?

I am looking at the documentation for neo4j and I see that I can use parameters when I create objects. Specifically when I look at this page I see the code:

{
  "props" : {
    "position" : "Developer",
    "name" : "Andres"
  }
}

Query.

CREATE ({ props })

Yet when I use the web interface to access my neo4j database on my local machine I do not know how to specify the parameter. Simply copy/pasting that JSON object yields an error. I see on the page that

Exactly how to submit them depends on the driver in use.

but how does one use them on that command line/web interface?

Upvotes: 6

Views: 6838

Answers (2)

makiko_fly
makiko_fly

Reputation: 606

The syntax to define params in the browser's command line is :params followed by the variable you would like to define, just type in :params and you will get a sense of how this command works from the resulting prompts.

For the case of HTTP API, in the latest version (v3.5 as of this writing), use the following syntax, copied from https://neo4j.com/docs/http-api/current/http-api-actions/execute-multiple-statements/:

{
  "statements" : [ {
    "statement" : "CREATE (n) RETURN id(n)"
  }, {
    "statement" : "CREATE (n {props}) RETURN n",
    "parameters" : {
      "props" : {
        "name" : "My Node"
      }
    }
  } ]
}

Upvotes: 0

Kenny Bastani
Kenny Bastani

Reputation: 3308

Cypher supports queries with parameters which are submitted as JSON. For example, the following is the REST API usage. For the Java embedded API please refer to the following documentation: http://docs.neo4j.org/chunked/milestone/tutorials-cypher-parameters-java.html

MATCH (x { name: { startName }})-[r]-(friend)
WHERE friend.name = { name }
RETURN TYPE(r)

Example request

POST http://localhost:7474/db/data/cypher
Accept: application/json; charset=UTF-8
Content-Type: application/json 

{
  "query" : "MATCH (x {name: {startName}})-[r]-(friend) WHERE friend.name = {name} RETURN TYPE(r)",
  "params" : {
    "startName" : "I",
    "name" : "you"
  }
}

Example response

200: OK
Content-Type: application/json; charset=UTF-8

{
  "columns" : [ "TYPE(r)" ],
  "data" : [ [ "know" ] ]
}

Parameters are not currently supported in regular Cypher statements in the Neo4j 2.0 browser. However, you can use the :POST syntax to achieve this.

Refer to the documentation for more information on Cypher queries via REST API.

http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html

Update:

The following query allows you to accomplish this in the browser, although it is not an ideal experience:

:POST /db/data/transaction/commit {
    "statements": [
        {
            "statement": "MATCH (u:User {name:{username}}) RETURN u.name as username",
            "parameters": {
                "username": "my name"
            }
        }
    ]
}

Upvotes: 7

Related Questions