diff --git a/core/src/main/java/apoc/schema/Schemas.java b/core/src/main/java/apoc/schema/Schemas.java index 51de0fb447..a6bfd5b1de 100644 --- a/core/src/main/java/apoc/schema/Schemas.java +++ b/core/src/main/java/apoc/schema/Schemas.java @@ -82,6 +82,12 @@ public Boolean indexExistsOnNode(@Name("labelName") String labelName, @Name("pro return indexExists(labelName, propertyNames); } + @UserFunction(value = "apoc.schema.relationship.indexExists") + @Description("RETURN apoc.schema.relationship.indexExists(relName, propertyNames)") + public Boolean indexExistsOnRelationship(@Name("labelName") String relName, @Name("propertyName") List propertyNames) { + return indexExistsForRelationship(relName, propertyNames); + } + @UserFunction(value = "apoc.schema.node.constraintExists") @Description("RETURN apoc.schema.node.constraintExists(labelName, propertyNames)") public Boolean constraintExistsOnNode(@Name("labelName") String labelName, @Name("propertyName") List propertyNames) { @@ -245,16 +251,23 @@ private Map> copyMapOfObjects(Map> inp * @return true if the index exists otherwise it returns false */ private Boolean indexExists(String labelName, List propertyNames) { - Schema schema = tx.schema(); + Iterable nodeIndexes = tx.schema().getIndexes(label(labelName)); + return isIndexExistent(propertyNames, nodeIndexes); + } + + private Boolean indexExistsForRelationship(String relName, List propertyNames) { + Iterable relIndexes = tx.schema().getIndexes(RelationshipType.withName(relName)); + return isIndexExistent(propertyNames, relIndexes); + } - for (IndexDefinition indexDefinition : Iterables.asList(schema.getIndexes(Label.label(labelName)))) { + private Boolean isIndexExistent(List propertyNames, Iterable indexes) { + for (IndexDefinition indexDefinition : indexes) { List properties = Iterables.asList(indexDefinition.getPropertyKeys()); if (properties.equals(propertyNames)) { return true; } } - return false; } diff --git a/core/src/test/java/apoc/schema/SchemasTest.java b/core/src/test/java/apoc/schema/SchemasTest.java index c6494cfc1a..a0b49548e4 100644 --- a/core/src/test/java/apoc/schema/SchemasTest.java +++ b/core/src/test/java/apoc/schema/SchemasTest.java @@ -355,8 +355,19 @@ public void testIndexNotExists() { assertEquals(false, r.entrySet().iterator().next().getValue()); }); } - - + + @Test + public void testRelationshipExists() { + db.executeTransactionally("CREATE INDEX rel_index_simple FOR ()-[r:KNOWS]-() ON (r.since)"); + db.executeTransactionally("CREATE INDEX rel_index_composite FOR ()-[r:PURCHASED]-() ON (r.date, r.amount)"); + awaitIndexesOnline(); + + assertTrue(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('KNOWS', ['since'])")); + assertFalse(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('KNOWS', ['dunno'])")); + // - composite index + assertTrue(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('PURCHASED', ['date', 'amount'])")); + assertFalse(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('PURCHASED', ['date', 'another'])")); + } @Test public void testUniquenessConstraintOnNode() { diff --git a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship-lite.csv b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship-lite.csv index 519f73a065..22538a4d7a 100644 --- a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship-lite.csv +++ b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship-lite.csv @@ -1,2 +1,3 @@ ¦signature ¦apoc.schema.relationship.constraintExists(type :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?) +¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?) diff --git a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.csv b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.csv index 91c30b7cbd..ddac652f56 100644 --- a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.csv +++ b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.csv @@ -4,3 +4,8 @@ RETURN apoc.schema.relationship.constraintExists(type, propertyNames) |label:function[] |label:apoc-core[] +|xref::overview/apoc.schema.relationship/apoc.schema.adoc[apoc.schema.relationship.indexExists icon:book[]] + +RETURN apoc.schema.relationship.indexExists(relName, propertyNames) +|label:function[] +|label:apoc-core[] diff --git a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists-lite.csv b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists-lite.csv new file mode 100644 index 0000000000..9bd214a967 --- /dev/null +++ b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists-lite.csv @@ -0,0 +1,2 @@ +¦signature +¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?) diff --git a/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists.adoc b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists.adoc new file mode 100644 index 0000000000..7019e86a06 --- /dev/null +++ b/docs/asciidoc/modules/ROOT/examples/generated-documentation/apoc.schema.relationship.indexExists.adoc @@ -0,0 +1,5 @@ +¦xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]] + + + +¦label:function[] +¦label:apoc-core[] diff --git a/docs/asciidoc/modules/ROOT/examples/generated-documentation/documentation.csv b/docs/asciidoc/modules/ROOT/examples/generated-documentation/documentation.csv index a693264e4e..ee0d037130 100644 --- a/docs/asciidoc/modules/ROOT/examples/generated-documentation/documentation.csv +++ b/docs/asciidoc/modules/ROOT/examples/generated-documentation/documentation.csv @@ -526,6 +526,7 @@ for the provided `label` and `uuidProperty`, in case the UUID handler is already ¦function¦apoc.schema.node.constraintExists¦apoc.schema.node.constraintExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.node.constraintExists(labelName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc ¦function¦apoc.schema.node.indexExists¦apoc.schema.node.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.node.indexExists(labelName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc ¦function¦apoc.schema.relationship.constraintExists¦apoc.schema.relationship.constraintExists(type :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.relationship.constraintExists(type, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc +¦function¦apoc.schema.relationship.indexExists¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.relationship.indexExists(relName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc ¦function¦apoc.scoring.existence¦apoc.scoring.existence(score :: INTEGER?, exists :: BOOLEAN?) :: (FLOAT?)¦apoc.scoring.existence(5, true) returns the provided score if true, 0 if false¦true¦ ¦function¦apoc.scoring.pareto¦apoc.scoring.pareto(minimumThreshold :: INTEGER?, eightyPercentValue :: INTEGER?, maximumValue :: INTEGER?, score :: INTEGER?) :: (FLOAT?)¦apoc.scoring.pareto(10, 20, 100, 11) applies a Pareto scoring function over the inputs¦true¦ ¦function¦apoc.static.get¦apoc.static.get(key :: STRING?) :: (ANY?)¦apoc.static.get(name) - returns statically stored value from config (apoc.static.) or server lifetime storage¦false¦xref::misc/static-values.adoc diff --git a/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/apoc.schema.relationship.indexExists.adoc b/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/apoc.schema.relationship.indexExists.adoc new file mode 100644 index 0000000000..3da7dffddb --- /dev/null +++ b/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/apoc.schema.relationship.indexExists.adoc @@ -0,0 +1,29 @@ +//// +This file is generated by DocsTest, so don't change it! +//// + += apoc.schema.relationship.indexExists +:description: This section contains reference documentation for the apoc.schema.relationship.indexExists function. + +label:function[] label:apoc-core[] + +[.emphasis] +RETURN apoc.schema.relationship.indexExists(relName, propertyNames) + +== Signature + +[source] +---- +apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?) +---- + +== Input parameters +[.procedures, opts=header] +|=== +| Name | Type | Default +|labelName|STRING?|null +|propertyName|LIST? OF STRING?|null +|=== + +xref::indexes/schema-index-operations.adoc[More documentation of apoc.schema.relationship.indexExists,role=more information] + diff --git a/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/index.adoc b/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/index.adoc index 276ece6d22..72d6346702 100644 --- a/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/index.adoc +++ b/docs/asciidoc/modules/ROOT/pages/overview/apoc.schema/index.adoc @@ -48,5 +48,10 @@ RETURN apoc.schema.node.indexExists(labelName, propertyNames) RETURN apoc.schema.relationship.constraintExists(type, propertyNames) |label:function[] |label:apoc-core[] +|xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]] + +RETURN apoc.schema.relationship.indexExists(relName, propertyNames) +|label:function[] +|label:apoc-core[] |=== diff --git a/docs/asciidoc/modules/ROOT/partials/generated-documentation/documentation.adoc b/docs/asciidoc/modules/ROOT/partials/generated-documentation/documentation.adoc index d5ddde17d8..c43d5c2839 100644 --- a/docs/asciidoc/modules/ROOT/partials/generated-documentation/documentation.adoc +++ b/docs/asciidoc/modules/ROOT/partials/generated-documentation/documentation.adoc @@ -2910,6 +2910,11 @@ RETURN apoc.schema.node.indexExists(labelName, propertyNames) RETURN apoc.schema.relationship.constraintExists(type, propertyNames) |label:function[] |label:apoc-core[] +|xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]] + +RETURN apoc.schema.relationship.indexExists(relName, propertyNames) +|label:function[] +|label:apoc-core[] |=== [discrete] diff --git a/docs/asciidoc/modules/ROOT/partials/generated-documentation/nav.adoc b/docs/asciidoc/modules/ROOT/partials/generated-documentation/nav.adoc index 5eac171113..ecf001240c 100644 --- a/docs/asciidoc/modules/ROOT/partials/generated-documentation/nav.adoc +++ b/docs/asciidoc/modules/ROOT/partials/generated-documentation/nav.adoc @@ -559,6 +559,7 @@ This file is generated by DocsTest, so don't change it! *** xref::overview/apoc.schema/apoc.schema.node.constraintExists.adoc[] *** xref::overview/apoc.schema/apoc.schema.node.indexExists.adoc[] *** xref::overview/apoc.schema/apoc.schema.relationship.constraintExists.adoc[] +*** xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[] ** xref::overview/apoc.scoring/index.adoc[] *** xref::overview/apoc.scoring/apoc.scoring.existence.adoc[] *** xref::overview/apoc.scoring/apoc.scoring.pareto.adoc[]