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

Allow direction-only (typeless) relationships in the path expander relationshipFilter and sequences #821

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions docs/expand.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Syntax: `[<]RELATIONSHIP_TYPE1[>]|[<]RELATIONSHIP_TYPE2[>]|...`
| LIKES> | LIKES | OUTGOING
| <FOLLOWS | FOLLOWS | INCOMING
| KNOWS | KNOWS | BOTH
| > | any type | OUTGOING
| < | any type | INCOMING
|===

==== Label Filter
Expand Down
2 changes: 2 additions & 0 deletions docs/overview.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ Syntax: `[<]RELATIONSHIP_TYPE1[>]|[<]RELATIONSHIP_TYPE2[>]|...`
| LIKES> | LIKES | OUTGOING
| <FOLLOWS | FOLLOWS | INCOMING
| KNOWS | KNOWS | BOTH
| > | any type | OUTGOING
| < | any type | INCOMING
|===

=== Label Filter
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/apoc/path/RelationshipSequenceExpander.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ protected Iterator<Relationship> createNestedIterator(
{
RelationshipType type = entry.first();
Direction dir = entry.other();
return ( ( dir == Direction.BOTH ) ? node.getRelationships( type ) :
node.getRelationships( type, dir ) ).iterator();
if (type != null) {
return ((dir == Direction.BOTH) ? node.getRelationships(type) :
node.getRelationships(type, dir)).iterator();
} else {
return ((dir == Direction.BOTH) ? node.getRelationships() :
node.getRelationships(dir)).iterator();
}
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/apoc/path/ExpandPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import apoc.util.TestUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -325,4 +326,34 @@ public void testCompoundLabelWorksInBlacklist() {
assertEquals("Gene Hackman", node.getProperty("name"));
});
}

@Test
public void testRelationshipFilterWorksWithoutTypeOutgoing() {
TestUtil.testResult(db,
"MATCH (k:Person {name:'Keanu Reeves'}) " +
"CALL apoc.path.subgraphNodes(k, {relationshipFilter:'>', labelFilter:'>Movie', uniqueness: 'NODE_GLOBAL'}) yield node " +
"return collect(node.title) as titles",
result -> {

List<String> expectedTitles = new ArrayList<>(Arrays.asList("Something's Gotta Give", "Johnny Mnemonic", "The Replacements", "The Devil's Advocate", "The Matrix Revolutions", "The Matrix Reloaded", "The Matrix"));
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
List<String> titles = (List<String>) maps.get(0).get("titles");
assertEquals(7, titles.size());
assertTrue(titles.containsAll(expectedTitles));
});
}

@Test
public void testRelationshipFilterWorksWithoutTypeIncoming() {
TestUtil.testResult(db,
"MATCH (k:Person {name:'Keanu Reeves'}) " +
"CALL apoc.path.subgraphNodes(k, {relationshipFilter:'<', labelFilter:'>BigBrother', uniqueness: 'NODE_GLOBAL'}) yield node " +
"return node",
result -> {

List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
});
}
}
18 changes: 18 additions & 0 deletions src/test/java/apoc/path/RelSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -84,4 +86,20 @@ public void testRelSequenceWhenNotBeginningAtStart() throws Throwable {
assertTrue(names.containsAll(expectedNames));
});
}

@Test
public void testRelationshipFilterWorksWithoutTypeWithRelSequence() {
TestUtil.testResult(db,
"MATCH (k:Person {name:'Keanu Reeves'}) " +
"CALL apoc.path.subgraphNodes(k, {relationshipFilter:'>,<', labelFilter:'/Person'}) yield node " +
"return collect(node.name) as names",
result -> {
List<String> expectedNames = new ArrayList<>(Arrays.asList("Nancy Meyers", "Jack Nicholson", "Diane Keaton", "Dina Meyer", "Ice-T", "Takeshi Kitano", "Robert Longo", "Jessica Thompson", "Angela Scope", "James Thompson", "Brooke Langton", "Gene Hackman", "Orlando Jones", "Howard Deutch", "Al Pacino", "Taylor Hackford", "Charlize Theron", "Lana Wachowski", "Joel Silver", "Hugo Weaving", "Andy Wachowski", "Carrie-Anne Moss", "Laurence Fishburne", "Emil Eifrem"));
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
List<String> names = (List<String>) maps.get(0).get("names");
assertEquals(24, names.size());
assertTrue(names.containsAll(expectedNames));
});
}
}
13 changes: 13 additions & 0 deletions src/test/java/apoc/path/SequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -106,4 +108,15 @@ public void testExpandWithSequenceIgnoresLabelFilter() throws Throwable {
assertTrue(names.containsAll(expectedNames));
});
}

@Test
public void testRelationshipFilterWorksWithoutTypeWithFullSequence() {
String query = "MATCH (t:Person {name: 'Tom Hanks'}) CALL apoc.path.expandConfig(t,{sequence:'>Person, >, Movie, <DIRECTED'}) yield path with distinct last(nodes(path)) as node return collect(node.name) as names";
TestUtil.testCall(db, query, (row) -> {
List<String> expectedNames = new ArrayList<>(Arrays.asList("Mike Nichols", "Robert Zemeckis", "Penny Marshall", "Ron Howard", "Frank Darabont", "Andy Wachowski", "Lana Wachowski", "Tom Tykwer", "Tom Hanks", "John Patrick Stanley", "Nora Ephron", "James Marshall", "Rob Reiner"));
List<String> names = (List<String>) row.get("names");
assertEquals(13l, names.size());
assertTrue(names.containsAll(expectedNames));
});
}
}