0
votes

I have been trying to create nodes and relations ships for our new module with neo4jphp [https://github.com/jadell/neo4jphp/wiki].

I am using cypher queries for the same.

Creating nodes with below query:

$queryNodes = "CREATE (n:User { props } ) ";
$query      = new Everyman\Neo4j\Cypher\Query($client, $queryNodes, array('props' => $arrNodeProperties));
$result     = $query->getResultSet();

Creating relationships with below query:

$queryRelations = "
   MATCH (authUser: User { userid: 0001 }),(friend)
     WHERE friend.userid IN ['" . implode("','",$relations) . "']
        CREATE UNIQUE (authUser)-[r:KNOWS { connection: 'user_friend' }]->(friend)";

So far node creation works gr8.

But when i try to create Unique relationships for the nodes, it takes too long....

Note: There is unique constraint userid for label User, hence node with label user is indexed by Neo4j on property userid.

CREATE CONSTRAINT ON (user:User) ASSERT user.userid IS UNIQUE

Questions:

  1. Is there any other way we can achieve creating unique relationships.
  2. Can i use index on relationships?? If Yes how can I achieve the same.
2
Use a label for your friend match (friend:User) otherwise it won't use the index.Michael Hunger
Thanks @MichaelHunger. It worked!!!dirtycode

2 Answers

1
votes

You might try use use MERGE instead of CREATE UNIQUE. Additionally use a Cypher parameter for the fried's list instead of concatenation on client side, see http://docs.neo4j.org/chunked/stable/cypher-parameters.html

0
votes

Finally I worked it out with few changes...

Thanks @MichaelHunger for the help.

So here is how i did it...

Creating Unique Nodes using MERGE, FOREACH, ON CREATE SET and params:

$queryNodes = "
    FOREACH (nodeData IN {nodeProperties}|
        MERGE (n:User { userid: nodeData.userid })
            ON CREATE SET
                n.login = nodeData.login,
                n.userid = nodeData.userid,
                n.username = nodeData.username,
                n.name = nodeData.name,
                n.gender = nodeData.gender,
                n.profile_pic = nodeData.profile_pic,
                n.create_date = timestamp()
            ON MATCH SET
                n.update_date = timestamp()
    )
";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryNodes, array('nodeProperties' => $arrNodeProperties));
$result = $query->getResultSet();

Creating Unique Relationships with below query:

$queryRelations = "
    MATCH (authUser: User { userid: {authUserid} }), (friend:User)
        WHERE friend.userid IN {friendUserIds}
    CREATE UNIQUE (authUser)-[r:KNOWS { connection: 'user_friend' }]->(friend)
";
$query  = new Everyman\Neo4j\Cypher\Query($client, $queryRelations, array('friendUserIds' => $arrFriendUserId, 'authUserid' => $authUserid));
$result = $query->getResultSet();

Please comment if we can improve the same even further.