Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(neo4j): improve neo4j query performance by using node labels #10415

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ public RelatedEntitiesResult findRelatedEntities(
final RelationshipDirection relationshipDirection = relationshipFilter.getDirection();

String srcNodeLabel = "";
String baseStatementString = "";
// Create a URN from the String. Only proceed if srcCriteria is not null or empty
if (srcCriteria != null
&& !srcCriteria.isEmpty()
Expand All @@ -516,13 +517,6 @@ public RelatedEntitiesResult findRelatedEntities(
}
}

String matchTemplate = "MATCH (src:%s %s)-[r%s %s]-(dest %s)%s";
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate = "MATCH (src:%s %s)<-[r%s %s]-(dest %s)%s";
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate = "MATCH (src:%s %s)-[r%s %s]->(dest %s)%s";
}

final String returnNodes =
String.format(
"RETURN dest, type(r)"); // Return both related entity and the relationship type.
Expand All @@ -535,16 +529,40 @@ public RelatedEntitiesResult findRelatedEntities(

String whereClause = computeEntityTypeWhereClause(sourceTypes, destinationTypes);

// Build Statement strings
String baseStatementString =
String.format(
matchTemplate,
srcNodeLabel,
srcCriteria,
relationshipTypeFilter,
edgeCriteria,
destCriteria,
whereClause);
if (srcNodeLabel != null && !srcNodeLabel.isEmpty()) {
String matchTemplate = "MATCH (src:%s %s)-[r%s %s]-(dest %s)%s";
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate = "MATCH (src:%s %s)<-[r%s %s]-(dest %s)%s";
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate = "MATCH (src:%s %s)-[r%s %s]->(dest %s)%s";
}
// Build Statement strings
baseStatementString =
String.format(
matchTemplate,
srcNodeLabel,
srcCriteria,
relationshipTypeFilter,
edgeCriteria,
destCriteria,
whereClause);
} else {
String matchTemplate = "MATCH (src %s)-[r%s %s]-(dest %s)%s";
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate = "MATCH (src %s)<-[r%s %s]-(dest %s)%s";
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate = "MATCH (src %s)-[r%s %s]->(dest %s)%s";
}
// Build Statement strings
baseStatementString =
String.format(
matchTemplate,
srcCriteria,
relationshipTypeFilter,
edgeCriteria,
destCriteria,
whereClause);
}

log.info(baseStatementString);

Expand Down Expand Up @@ -648,18 +666,34 @@ public void removeEdgesFromNode(

// build node label from entity type
final String srcNodeLabel = urn.getEntityType();
String matchTemplate = "";
pashashaik-mms marked this conversation as resolved.
Show resolved Hide resolved

String matchTemplate =
String.format(
"MATCH (src:%s {urn: $urn})-[r%s]-(dest) RETURN type(r), dest, 2", srcNodeLabel);
if (relationshipDirection == RelationshipDirection.INCOMING) {
if (srcNodeLabel != null && !srcNodeLabel.isEmpty()) {
matchTemplate =
String.format(
"MATCH (src:%s {urn: $urn})<-[r%s]-(dest) RETURN type(r), dest, 0", srcNodeLabel);
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
"MATCH (src:%s {urn: $urn})-[r%s]-(dest) RETURN type(r), dest, 2", srcNodeLabel);
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate =
String.format(
"MATCH (src:%s {urn: $urn})<-[r%s]-(dest) RETURN type(r), dest, 0", srcNodeLabel);
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate =
String.format(
"MATCH (src:%s {urn: $urn})-[r%s]->(dest) RETURN type(r), dest, 1", srcNodeLabel);
}
} else {
matchTemplate =
String.format(
"MATCH (src:%s {urn: $urn})-[r%s]->(dest) RETURN type(r), dest, 1", srcNodeLabel);
"MATCH (src {urn: $urn})-[r%s]-(dest) RETURN type(r), dest, 2", srcNodeLabel);
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate =
String.format(
"MATCH (src {urn: $urn})<-[r%s]-(dest) RETURN type(r), dest, 0", srcNodeLabel);
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate =
String.format(
"MATCH (src {urn: $urn})-[r%s]->(dest) RETURN type(r), dest, 1", srcNodeLabel);
}
}

String relationshipTypeFilter = "";
Expand Down
Loading