Abdullah Al Noman
Abdullah Al Noman

Reputation: 2878

How to check if an index exist in neo4j cypher

I am trying to find a way to check if a certain index exists in cypher schema indexes. I can find all the indexes by using call db.indexes() . but how can I check for a specific index?

Upvotes: 5

Views: 3746

Answers (3)

Sam Wynne
Sam Wynne

Reputation: 15

In the current Neo4J 5 in 2024, you can use SHOW INDEXES (or SHOW CONSTRAINTS) and it will present a list

Docs: Managing Indexes Manual

OR, you can just create the desired index or constraint with the "IF NOT EXISTS" clause, and it will add it only if it isn't in place yet.

Example:

    CREATE INDEX <index_name> IF NOT EXISTS
    FOR (x:<node_label>)
    ON x.<property_key>

Docs: CREATE INDEX IF NOT EXISTS

Upvotes: 0

cybersam
cybersam

Reputation: 67019

The APOC plugin has an apoc.schema.node.indexExists function for determining whether a specific index exists.

Upvotes: 4

Tezra
Tezra

Reputation: 8833

If you want the index to exist, I would recommend just running the Cypher to create the index. The result being that whether the index existed or not, after the call it is guaranteed to exist.

On the other hand, if you just want the information for display purposes or something, you can use YIELD to continue a cypher from a CALL. For example...

CALL db.indexes() YIELD label, properties WHERE label="Person" RETURN *

For db.indexes, the variables you can yield are description, label, properties, provider, state, type (you have to yield them by name, YIELD a,b,c,d,e,f won't work)

Upvotes: 5

Related Questions