0
votes

Can anyone exmplain the difference betwen @GraphId and @Index annotation from org.neo4j.ogm.annotation ? For now, after reading the docs it seems that @GraphId is used to create identifier for Neo4j internal logic and users should not rely on that, because it can be reused over time. But what about @Index?

As I understand, the main advantage of graph based databases is that once we know the node/relation from which to start things become easy, since all we need to do is just traverse the graph from that starting node. And indexing helps to do so, right? So, we can write something like START n = node:index_name(property_name = value) and immitiately start exloring the graph from the indexed node by 'property_name' property, right?

So, consider this entity :

@ToString
@NodeEntity(label = "Event")
@NoArgsConstructor
public class Event{

    public Event(String eventName, LocalDate dateTime){
        this.name = eventName;
        this.date = dateTime;
    }

    public Event(Long id, String eventName, LocalDate dateTime){
        this(eventName, dateTime);
        this.id = id;
    }

    @Getter
    @GraphId
    private Long id;

    @Getter
    @Index(unique = true, primary = true)
    @Property(name = "name")
    private String name;

    @Getter
    @Property(name = "date")
    @Convert(DateConverter.class)
    private LocalDate date;
}

As you can see the String name property is annotated with @Index. How can I write Cypher query to actually start from the node with name = 'something'? What is the index name? Or does Spring Data Neo4j 4.2.0.RELEASE figure it itself when write just MATCH (event:Event {name = 'somehting'} ... ?

@Repository
public interface EventRepository extends Neo4jRepository<Event, String>{

    Event findOne(String eventName);

}

Here the repositry class and as you might see I am using String as the type of the id of the entity the repository manages, so I assume Spring uses name property of Event class to generate a query for Event findOne(String eventName);

2

2 Answers

3
votes

@Index is similar to @Indexed if your are familiar with spring-data-mongodb or @Index in spring-data-jpa. Basically it indexes the field(among other things) which makes it searching for this field quite fast.
Regarding your repository method, it should be named like this

Event findByName(String eventName);
0
votes

Can anyone exmplain the difference betwen @GraphId and @Index annotation from org.neo4j.ogm.annotation ?

I assume that you've checked the doc of Spring Data Neo4j. But one thing I want to add about @GraphId and @Index is that @GraphId is unique among the whole db while @Index can be the same or unique, depending on your decision.

How can I write Cypher query to actually start from the node with name = 'something'? What is the index name? Or does Spring Data Neo4j 4.2.0.RELEASE figure it itself when write just MATCH (event:Event {name = 'somehting'} ... ?

I believe you write the Cypher query in a correct way. Indices (including graph id) are maintained by the database and kept up-to-date. They are stored in a form of certain data structure. For example, B-Tree or Map, which can reduce the time of searching. You can check out your neo4j db for the indices. They are either stored in files maintained by the db (Where does Neo4j store the data?).

As for how Cypher knows the name of an index, since indices are maintained by the db and Cypher queries are also decoded by the db, it would make sense that the db can access to the index according to the Cypher query. This is just my guessing based on my understanding of DB system.