Neo4j Properties on relationship

12,162

Solution 1

You can filter on relationship properties like you would node properties, in the WHERE clause. Just make sure you bind the relationship to an identifier in the MATCH clause. I don't understand your model (is the temperature a property of the causation?) but you could try something like

MATCH (body_temperature) <-[r:CAUSES]- (fever)
WHERE r.degreeCelsius > 102

Is that what you are looking for?

Edit

To make a CREATE clause dependent on some condition you can state your condition as a pattern which only matches the cases where you want something created, and then just proceed to create. Just make sure that in your START, MATCH and WHERE clauses you bind the different nodes or relationships that you want to use in your CREATE clause. Sometimes you might have to use WITH to carry results to a new part of your query, but for your case first try something like

START bodyTemp = node:MyIndex(name="Body Temperature"), fever = node:MyIndex(name="Fever")
WHERE HAS(bodyTemp.degreeCelsius) AND bodyTemp.degreeCelsius > 102.0
CREATE bodyTemp -[:CAUSES]-> FEVER

Solution 2

Suppose the "degree Celsius" is the property of the relationship "CAUSES", you can use the "where" clause to specify the property value constraint as follows,

Match t:BodyTemperature-[r:CAUSES]->f:Fever
Where r.degreeCelsius > 102.0
Return f
Share:
12,162
pawan9977
Author by

pawan9977

Updated on June 16, 2022

Comments

  • pawan9977
    pawan9977 about 2 years

    Say I have two nodes "Body Temperature" and "Fever" , The relationship between them has name "causes" and property as "degree Celsius" has value "102.0" . Now What I want to do is write a cypher query in which if there is property value > 102.0 in MATCH clause then it must retrieve fever node otherwise not.

    I have no idea of how to write such a query other then creating such structure.

    Any help would be appreciated.

  • jjaderberg
    jjaderberg over 10 years
    It seems I keep posting just one minute after someone else already answered :) I would bow out and delete my answer, except i think you may be assuming that Neo4j 2.0 is being used, is that right?
  • Lisa Li
    Lisa Li over 10 years
    No need to delete your answer. I don't see any harm to have multiple answers. It happens quite often, and yes the use of labels requires 2.0.
  • pawan9977
    pawan9977 over 10 years
    Will this query create relationship between body temperature and fever if and only if degreeCelsius is greater than 102.0 ?
  • pawan9977
    pawan9977 over 10 years
    Will this query create relationship between body temperature and fever if and only if degreeCelsius is greater than 102.0 ?
  • jjaderberg
    jjaderberg over 10 years
    No, this matches a pattern already in the graph, filtering on a relationship property. Do you want to create the relationship if the "Body temperature" node has a value over 102.0 for its "degree Celsius" property?
  • pawan9977
    pawan9977 over 10 years
    Ya , I wanted to do that only . Sorry if i didnt write my question clearly.