Skip to content

Commit

Permalink
Procedure for searching legacy index for relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Jun 15, 2017
1 parent ecaea25 commit 7cf92a7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Expand Up @@ -204,8 +204,8 @@ public Stream<NodeResult> nodeLegacyIndexSeek( @Name( "indexName" ) String legac
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Node index %s not found",
legacyIndexName );
}
IndexHits<Node> query = index.forNodes( legacyIndexName ).query( key, value );
return query.stream().map( NodeResult::new );
IndexHits<Node> nodes = index.forNodes( legacyIndexName ).query( key, value );
return nodes.stream().map( NodeResult::new );
}

@Description( "Search nodes from legacy index. Replaces `START n=node:nodes('key:foo*')`" )
Expand Down Expand Up @@ -237,8 +237,24 @@ public Stream<RelationshipResult> relationshipLegacyIndexSeek( @Name( "indexName
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Node index %s not found",
legacyIndexName );
}
IndexHits<Relationship> query = index.forRelationships( legacyIndexName ).query( key, value );
return query.stream().map( RelationshipResult::new );
IndexHits<Relationship> relationships = index.forRelationships( legacyIndexName ).query( key, value );
return relationships.stream().map( RelationshipResult::new );
}

@Description( "Search relationship from legacy index. Replaces `START r=relationship:relIndex('key:foo*')`" )
@Procedure( name = "db.relationshipLegacyIndexSearch", mode = SCHEMA )
public Stream<RelationshipResult> relationshipLegacyIndexSearch( @Name( "indexName" ) String legacyIndexName,
@Name("query") Object query )
throws ProcedureException
{
IndexManager index = graphDatabaseAPI.index();
if ( !index.existsForRelationships( legacyIndexName ) )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Node index %s not found",
legacyIndexName );
}
IndexHits<Relationship> relationships = index.forRelationships( legacyIndexName ).query( query );
return relationships.stream().map( RelationshipResult::new );
}

private IndexProcedures indexProcedures()
Expand Down
Expand Up @@ -90,4 +90,59 @@ class LegacyIndexProcsIT extends ExecutionEngineFunSuite {
a [CypherExecutionException] should be thrownBy
execute("""CALL db.relationshipLegacyIndexSeek('index', 'key', 'value') YIELD relationship AS r RETURN r""")
}

test("Relationship legacy index search plus MATCH") {
val node = createNode(Map("prop" -> 42))
val otherNode = createNode(Map("prop" -> 21))
val relationship = relate(node, otherNode)

graph.inTx {
val relationshipIndex = graph.index().forRelationships("relIndex")
relationshipIndex.add(relationship, "key", "value")
}

val query = "CALL db.relationshipLegacyIndexSearch('relIndex','key:*') YIELD relationship AS r MATCH (a)-[r]-(b) RETURN r"
val result = execute(query)

result.toList should equal(List(
Map("r"-> relationship),
Map("r"-> relationship)
))
}

test("Relationship legacy index search plus MATCH directed") {
val node = createNode(Map("prop" -> 42))
val otherNode = createNode(Map("prop" -> 21))
val relationship = relate(node, otherNode)

graph.inTx {
val relationshipIndex = graph.index().forRelationships("relIndex")
relationshipIndex.add(relationship, "key", "value")
}

val query = "CALL db.relationshipLegacyIndexSearch('relIndex','key:*') YIELD relationship AS r MATCH (a)-[r]->(b) RETURN r"
val result = execute(query)

result.toList should equal(List(
Map("r"-> relationship)
))
}

test("should return correct results on combined node and relationship index starts") {
val node = createNode()
val resultNode = createNode()
val rel = relate(node, resultNode)
relate(node, createNode())

graph.inTx {
graph.index().forNodes("nodes").add(node, "key", "A")
graph.index().forRelationships("rels").add(rel, "key", "B")
}

val result = execute("CALL db.nodeLegacyIndexSeek('nodes', 'key', 'A') YIELD node AS n " +
"CALL db.relationshipLegacyIndexSeek('rels', 'key', 'B') YIELD relationship AS r " +
"MATCH (n)-[r]->(b) RETURN b")
result.toList should equal(List(Map("b" -> resultNode)))
}

}

0 comments on commit 7cf92a7

Please sign in to comment.