Search code examples
neo4jcypher

Neo4j Traverse API vs Cypher


enter image description here

I am trying to verify if an image Node is connected with a group of tags. The picture above shows my procedure code which can be called like this: "CALL myProcedure(imageNode, ['tag1', 'tag2']) YIELD node RETURN node".

The procedure is 2x slower than cypher query: "MATCH (imageNode)-[:has_tag]->(t:Tag) WHERE t.name='tag1' WITH imageNode MATCH (imageNode)-[:has_tag]->(t:Tag) WHERE t.name='tag2' RETURN imageNode"

Why this could be happend? Did i make some mistake in my procedure? If there are too more tags the cypher query will be too long, this is why i am writing directly the verification logic into a procedure.

Any suggestions welcome, thanks :).


Solution

  • There is no need to write a custom procedure. It is easy to simplify your Cypher query to handle any number of tags.

    For example, this query assumes you pass it a $tagNames parameter containing a list of tag names:

    MATCH (image:Image)
    WHERE ALL(t IN $tagNames WHERE EXISTS((image)-[:has_tag]->({name: t})))
    RETURN image