【问题标题】:Neo4j cypher MATCH query not workingNeo4j 密码匹配查询不起作用
【发布时间】:2014-03-24 13:17:36
【问题描述】:

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

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

使用密码时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();

【问题讨论】:

  • 我已经试过了,这很有效。我确实必须更正您的密码查询,以便它们在节点'n'周围有括号并删除了几个';'他们不应该去的地方。
  • 您是如何使用标签“学生”和“标记”值创建节点的?你用的是什么 Neo4j 版本?
  • 我已经编辑了问题,在那里找到它。
  • 我正在使用 neo4j-community-2.0.0-M03。

标签: java neo4j cypher


【解决方案1】:

这只是工作,请参阅以下代码sn-p。你的脚本有什么不同?

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

【讨论】:

  • Timmy,您的索引定义与数据不匹配,属性名称区分大小写。 “标记”与“标记”
  • 但你是对的,更好的属性名称应该是“marks”,因为它是小写的驼峰式。
  • 您可能还想重新运行它以更正输出:)
  • 代码几乎相同,我没有用括号括住节点,但这样做也没有帮助。
  • 关于这个查询如何工作的任何想法:start n=node(127) match (n:Student) where n.Marks=30 return n;
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
相关资源
最近更新 更多