Spring 数据 Neo4j @GraphId 和 @Index

Spring Data Neo4j @GraphId and @Index

任何人都可以解释 @GraphId@Index 注释之间的区别 org.neo4j.ogm.annotation 吗? 目前,在阅读文档后,似乎 @GraphId 用于为 Neo4j 内部逻辑创建标识符,用户不应依赖它,因为它可以随着时间的推移重复使用。但是 @Index 呢?

据我了解,基于图形的数据库的主要优点是,一旦我们知道 node/relation 从哪里开始,事情就变得容易了,因为我们需要做的只是从该起始节点遍历图形.索引有助于这样做,对吗?所以,我们可以写类似 START n = node:index_name(property_name = value) 的东西,然后立即从 'property_name' 属性 的索引节点开始探索图形,对吧?

所以,考虑这个实体:

@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;
}

如您所见,String name 属性 被注释为 @Index。如何编写 Cypher 查询以实际从名称 = 'something' 的节点开始?索引名称是什么?还是 Spring Data Neo4j 4.2.0.RELEASE 只写 MATCH (event:Event {name = 'somehting'} ... 时会自行计算?

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

    Event findOne(String eventName);

}

这里是存储库 class,您可能会看到我使用 String 作为存储库管理的实体的 ID 类型,所以我假设 Spring 使用 [=事件 class 的 23=] 属性 生成对 Event findOne(String eventName);

的查询
如果您熟悉 spring-data-mongodb 或 spring-data-jpa 中的 @Index,则

@Index 类似于 @Indexed。基本上它索引字段(除其他外),这使得它搜索这个字段非常快。
关于您的存储库方法,它应该这样命名

Event findByName(String eventName);

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

我假设您已经检查了 Spring Data Neo4j 的文档。但是关于@GraphId 和@Index 我想补充的一件事是@GraphId 在整个数据库中是唯一的,而@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'} ... ?

我相信您以正确的方式编写了 Cypher 查询。索引(包括图形 ID)由数据库维护并保持最新。它们以某种数据结构的形式存储。比如B-Tree或者Map,可以减少查找的时间。您可以查看您的 neo4j 数据库以获取索引。它们要么存储在数据库 (Where does Neo4j store the data?) 维护的文件中。

至于Cypher是如何知道索引名称的,因为索引是由db维护的,Cypher的查询也是由db解码的,所以db可以根据Cypher的查询来访问索引也就说得通了.这只是我根据我对数据库系统的理解的猜测。