gpt4 book ai didi

java - Neo4j 密码 MATCH 查询不起作用

转载 作者:行者123 更新时间:2023-12-01 13:13:56 25 4
gpt4 key购买 nike

我在我的嵌入 graphDb 中的标签 Student 上创建了以下索引

Schema schema = graphDb.schema();    
indexDefinition = schema.indexFor(DynamicLabel.label("Student")).on("NodeType").create();
indexDefinition = schema.indexFor(DynamicLabel.label("Student")).on("Marks").create();

使用 cypher MATCH 查询时

这有效:match (n:Student) return n;

这也有效:match (n:Student) where n.Marks<30 return n;

但是,这失败了:match (n:Student) where n.Marks=30 return n;

还有这个:match (n:Student) where n.Marks='30' return n;

奇怪的是这个有效:

start n=node(127) match (n:Student) where n.Marks=30 return n;

有效:我得到了预期的结果,失败:没有结果

任何人都可以解释这种行为,因为所有属性都已索引(标签)并且密码应该返回所需的结果。

我还使用以下命令检查了标签的属性是否已索引:

Label label1 = DynamicLabel.label("Student");
System.out.println(schema.getIndexes(label1));

我正在使用 this 执行密码查询方法。

[编辑]节点创建:

Integer marks = 30;
Label label = DynamicLabel.label("Student");
tx = graphDb.beginTx();
Node studentNode = graphDb.createNode(label);
studentNode.setProperty("NodeType", "Student");
studentNode.setProperty("Marks", marks);
tx.success();

最佳答案

这确实有效,请参阅以下代码片段。您的脚本有什么不同?

final GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase();
final ExecutionEngine cypher = new ExecutionEngine(graphDb);
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
//schema.indexFor(DynamicLabel.label("Student")).on("NodeType").create();
schema.indexFor(DynamicLabel.label("Student")).on("marks").create();
Label label1 = DynamicLabel.label("Student");
System.out.println(schema.getIndexes(label1));
tx.success();
}
try (Transaction tx = graphDb.beginTx()) {
Node node = graphDb.createNode(DynamicLabel.label("Student"));
node.setProperty("marks", 20);
node = graphDb.createNode(DynamicLabel.label("Student"));
node.setProperty("marks", 30);
node = graphDb.createNode(DynamicLabel.label("Student"));
node.setProperty("marks", 40);
System.out.println(cypher.execute("match (n:Student) return n").dumpToString());
System.out.println(cypher.execute("match (n:Student) where n.marks<30 return n;").dumpToString());
System.out.println(cypher.execute("match (n:Student) where n.marks=30 return n;").dumpToString());
System.out.println(cypher.execute("match (n:Student) where n.marks='30' return n;").dumpToString());
tx.success();
}

脚本输出:

[IndexDefinition[label:Student, on:marks]]
+-------------------+
| n |
+-------------------+
| Node[0]{marks:20} |
| Node[1]{marks:30} |
| Node[2]{marks:40} |
+-------------------+
3 rows

+-------------------+
| n |
+-------------------+
| Node[0]{marks:20} |
+-------------------+
1 row

+-------------------+
| n |
+-------------------+
| Node[1]{marks:30} |
+-------------------+
1 row

+---+
| n |
+---+
+---+
0 row

关于java - Neo4j 密码 MATCH 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com