Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Properties on relationship

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.

like image 369
pawan9977 Avatar asked Oct 14 '13 11:10

pawan9977


People also ask

Can relationships have properties in Neo4j?

The Neo4j Graph Data Science Library provides multiple operations to work with relationships and their properties stored in a projected graphs. Relationship properties are either added during the graph projection or when using the mutate mode of our graph algorithms.

How are relationships stored in Neo4j?

Properties are stored as a linked list of property records, each holding a key and value and pointing to the next property. Each node and relationship references its first property record. The Nodes also reference the first relationship in its relationship chain. Each Relationship references its start and end node.

What are relationships in Neo4j?

Relationships describes a connection between a source node and a target node. Relationships always has a direction (one direction). Relationships must have a type (one type) to define (classify) what type of relationship they are.

How many nodes can a single relationship connect Neo4j?

You sometimes find cases where you need to connect more data to a relationship than can be fully captured in the properties. In other words, you want a relationship that connects more than two nodes. Mathematics allows this, with the concept of a hyperedge. This is impossible in Neo4j.


2 Answers

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
like image 97
jjaderberg Avatar answered Sep 19 '22 20:09

jjaderberg


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
like image 45
Lisa Li Avatar answered Sep 20 '22 20:09

Lisa Li