neo4jknowledge-graph

Neo4j, How to use match function in python when the targeted matching 'property' is a list?


I create a node in Neo4j, the node name is robot, the robots has a property, which is called 'capabilities'. For the capabilities, there is a list of all the capabilities for the robot. For example, the robot in the picture below has a capability lists, [Moving, ForceApplying]. enter image description here

Now I want to use matcher.match command in Py2neo to search for the robot node with specific capability. If the capability I am searching for is Moving. Then how do I write the code? Below is the code I write, but doesn't work,

from py2neo import Graph, Node, Relationship, NodeMatcher

graph = Graph('bolt://localhost:7687', auth=("neo4j", "neo4j"))
matcher = NodeMatcher(graph)     
a = matcher.match('Robot', capabilities = 'Moving').first()

enter image description here


Solution

  • The nodematcher is doing a search for an exactly the same value in the property, while the first() clause is returning the first record in the result. It is not looking at the first entry of the property 'capabilities'.

    You can read more on how it works here (nerd alert!):

    https://py2neo.org/v4/_modules/py2neo/matching.html
    

    This is how I did it.

     from py2neo import Graph, NodeMatcher
     graph = Graph("bolt://localhost:7687", user="neo4j", password="neo4j")
     matcher = NodeMatcher(graph)
     list(matcher.match("Robot").where("'Moving' in _.capabilities"))
    
    Result:
         [(_489:Robot {capabilities: ['Moving', 'ForceApplying'], name: 'Application_Kuka'})]