Does Constraints works on relationship property in neo4j 3.5 +

Hello I am trying to put constraint on relationship's attribute. My relationship and nodes files are separate. the relationship file has timestamp filed which is unique . previously I put constraints on nodes and my query gets more faster performance than before. Now I need to put constraint on relationships attribute. From doc I have seen that it is possible to put constraint in relationship attribute. I did that but not getting better speed for query performance
CREATE CONSTRAINT ON (r:owns) ASSERT r.TIMESTAMP IS UNIQUE

my timestamp field is string format but I am casting it in date time. do I need to put constraint at casted datetime value. My data size is 7million +

While you can add a constraint on relationship properties, it does not create an index on the relationship, which is why you're not seeing a corresponding increase in query speed. A way to get around this, is to place a node in the middle of your relationship that has the properties that you're trying to index on, like:

(n1)-[:RELATIONSHIP]->(:Node with properties you care about)-[:RELATIONSHIP]->(:n2)

This will allow you to index the property you're currently trying to set on the relationship and should increase your query speed.

unfortunately I don't have nodes with TIMESTAMP property. Only relationship contains timestamp property

I'm aware that you don't currently have that node. I'm just telling you that if you have that property on your relationship then you're not going to be able to enjoy the increased query speed you are after. You might decided to change your data model in which case you could probably do a simple query like:

MATCH (n1)-[r:relationship {timeStamp: data})->(n2)
MERGE (n1)-[:RELATIONSHIP]->(:Timestamp {timeStamp: r.timeStamp})-[:RELATIONSHIP]->(n2)
DELETE r

Something of that nature and get the desired result. But that's just suggestion.

Just pointing out, that constraint you have there:
CREATE CONSTRAINT ON (r:owns) ASSERT r.TIMESTAMP IS UNIQUE

Is a node constraint on :owns nodes, not on relationships.

At present the only relationship constraints we support are existence constraints on relationship properties

2 Likes

@MuddyBootsCode @andrew.bowman I have tried full text schema index on relationship like
call db.index.fulltext.createRelationshipIndex("timestampindex",["owns"," has"],["TIMESTAMP"],{eventually_consistent:"true"})

and tried to query

call db.index.fulltext.queryRelationships("timestampindex","match ()-[r:owns]->(d)
with datetime(r.TIMESTAMP) as dateval,d,type(r) as transactiontype
where dateval.year=2019 and dateval.month=03 and dateval.day=01 and dateval.hour>=10 and dateval.hour<12
return dateval.hour,d.ID,transactiontype,dateval ")

and got bellow error
Neo.ClientError.Procedure.ProcedureCallFailed
Failed to invoke procedure db.index.fulltext.queryRelationships: Caused by: org.apache.lucene.queryparser.classic.ParseException: Encountered " ")" ") "" at line 1, column 7.
Was expecting one of:
** ...**
** "+" ...**
** "-" ...**
** ...**
** "(" ...**
** "*" ...**
** ...**
** ...**
** ...**
** ...**
** ...**
** "[" ...**
** "{" ...**
** ...**
** ...**
I never used this full text schema before but my instinct is saying I have missed something here. Could you help me out please
Thanks

I am getting this after performing this query

CREATE CONSTRAINT ON ()-[owns: OWN]-() ASSERT exists(owns.TIMESTAMP)

Unable to create CONSTRAINT ON ()-[ owns:OWN]-() ASSERT exists(owns.TIMESTAMP):
Relationship(744882451) with type OWN must have the property `TIMESTAMP

Yes, the error is telling you that there is a relationship in your graph (with id() = 744882451) that is an :OWN relationship and doesn't have a TIMESTAMP property. Since this would violate the constraint, the constraint cannot be created.

You may want to make sure all of your :OWN relationships adhere to the constraint you want to add before creating the constraint.