我在嵌入式graphDb
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方法执行cypher查询。
[编辑]节点创建:
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();
答案 0 :(得分:1)
这只是工作,请参阅以下代码段。你的剧本有什么不同?
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