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

标签 java neo4j cypher

我在我的嵌入 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/

相关文章:

java - EJB3、JPA 错误,从 Query.getSingleResult() 返回了多个结果

java - 如果同步块(synchronized block)也能完成同样的事情,为什么还要使用同步方法

python - 从neo4j查询结果构建NetworkX图?

java - 在 JavaSparkStreamingContext 中执行查询

neo4j cypher - 将两个分组查询的结果分开(第 1 列/第 2 列)

java - 从 Java 中的 Cypher 查询检索结果缓慢 - Neo4j 2.0

java - sun.reflect.CallerSensitive 注释是什么意思?

java - 什么是 NullPointerException,我该如何解决?

neo4j 找到节点之间的所有路径。徒步旅行和登山路线

python - 使用 Neo4j-Python 连接器创建/合并图形期间出现 CypherError