victordobry
victordobry

Reputation: 3408

Unique index in Spring Data Neo4j 4

How can I ensure uniqueness:

Currently I use SDN 4.0.0.RC2. I peek the docs and it seems SDN 4 not supports this feature now (but will support in future). Did I understand correctly?

Upvotes: 2

Views: 1333

Answers (1)

František Hartman
František Hartman

Reputation: 15086

for property of an graph entity?

Use cypher to create a (possibly unique) index on a property:

CREATE INDEX ON :Person(name)

Unique index (aka constraint):

CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE

From http://neo4j.com/docs/stable/query-schema-index.html#schema-index-create-index-on-a-label

You can execute this automatically when you bootstrap your spring context:

@Component
public class IndexCreator {

    @Autowired
    Neo4jTemplate neo4jTemplate;

    @PostConstruct
    public void createIndexes() {
        try {
            neo4jTemplate.execute("CREATE INDEX ON :Person(name)", null);
        } catch (Exception ex) {
            // index already exists?
        }
    }
}

for combination of several properties simultaneously?

This is not directly supported. You can concatenate multiple properties into one and create an index on that (while keeping the original properties to be able to access them). Or (as pointed out by Michael) you can use array property to store multiple values.

Upvotes: 5

Related Questions