I'm going to try in my application Neo4j Manual Index on Relationship Properties in order to fix the performance issue I faced Neo4j Cypher query performance optimization
I have a few question which is not clear to me from the official Neo4j documentation:
MATCH (:Flight)-[r:DESTINATION]->(:Airport) CALL apoc.index.addRelationship(r,['taxi_time']) RETURN count(*)
The statement will create the relationship index with the same name as relationship-type, in this case DESTINATION and add the relationship by its properties to the index.
When do I need to create this relationship index? It should be done once(let's say at the application startup) or do I need to invoke this APOC function each time new -[r:DESTINATION]->
relationship is added between Flight
and Airport
nodes?
In case of existing -[r:DESTINATION]->
relationship update, how to update this information in the appropriate manual index?
In case of deleting some of Flight
or Airport
node - do I need to manually find and remove appropriate -[r:DESTINATION]->
relationships from the manual index or it will be done automatically by APOC and Neo4j?
In case of Spring Data Neo4j project - how to properly execute queries that contain APOC functions? For example, I want to call apoc.index.addRelationship
in order to create the manual index for the relationship properties. Can I use org.neo4j.ogm.session.Session.query
for this purpose?
What the consistency model is used for the manual indexes - Do they use eventual consistency or strong consistency model between the index and the original data?
I agree Neo4J documentation on this issue is really insufficient.
To answer your questions:
1.If you upgraded your Neo4J from an older version that used automatic relationship index you would need to run APOC (only once) to index your existing relationships using something like
MATCH ()-[r]->() CALL apoc.index.addRelationship(r,['property_1','property_2']) RETURN count(*);
You would then need to set up a trigger to any new relationship you add to that index, running something like this once:
CALL apoc.trigger.add('RELATIONSHIP_INDEX',"UNWIND {createdRelationships} AS r MATCH ()-[r]->() CALL apoc.index.addRelationship(r,['property_1','property_2']) RETURN count(*)", {phase:'after'})
(you will need to activate apoc.trigger.enabled=true
in neo4j.conf
file before)
2.See above
3.You would need to remove them also from the index, it is not done automatically. Set up an APOC trigger with removeRelationshipByName()
for that.
4.Should be possible.
5.Somebody from Neo4J should answer this.
Hope this helps and saves you some time!