pythonneo4jcypherneo4j-bolt

How to search for a particular property value in neo4j using python-bolt-driver


I am using Flask to make a web API based on NEO4J graph database.

I have a graph of topics and each topic has a "topic_name" and a "topic_id" properties set. I want to search for a topic with a particular topic_name (say "Tech"). If a node with this topic exists I want the transaction to return the topic_id. If not, I want it to return a boolean value. How to do this in Cypher and then using NEO4J bolt driver for python.

I can always extract all the topics and search for the particular name using python from returned response but I think this is something my database should do.


Solution

  • It's weird to want to have a result to be in a long or a boolean. Genrally it's preferable to only have type of result, or null if there is nothing.

    But to respond to your question, this the solution : the coalesce function (ie. if first argument is null, then take the value of the second argument)

    OPTIONAL MATCH (n:Topic {topic_name:'Tech'}) RETURN coalesce(n.topic_id, false)