Neo4j标签索引使用通配符进行搜索

时间:2013-06-28 13:55:45

标签: java neo4j cypher

我正在尝试Neo4j 2.0(M3)中的新标签功能,特别是与索引相结合。 Lucene索引似乎被称为遗留,所以我认为Label索引是要走的路。 我应该如何使用这种新方法编写通配符查询?我在下面的代码中遗漏了什么或者这个功能还没有实现吗? 您可以使用正则表达式执行此操作(如下所示),但不要使用模式索引。

public class IndexedLabelTest {

    public static void main(final String[] args) {
        final GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase();
        final Label label = DynamicLabel.label("A_LABEL");
        final Transaction tx = db.beginTx();
        db.schema().indexFor(label).on("name").create();
        final Node node = db.createNode(label);
        node.setProperty("name", "a_certain_name");

        final ExecutionEngine execEngine = new ExecutionEngine(db);

        // Find all nodes with label A_LABEL
        ExecutionResult result = execEngine.profile("MATCH node:A_LABEL RETURN node");
        System.out.println(result.dumpToString());
        System.out.println(result.executionPlanDescription());

        // Find all nodes with label A_LABEL and with a property 'name' that contains 'certain' (regex)
        result = execEngine.profile("MATCH node:A_LABEL WHERE node.name =~ '.*certain.*' RETURN node");
        System.out.println(result.dumpToString());
        System.out.println(result.executionPlanDescription());

        // Find all nodes with label A_LABEL and with a property 'name' that is 'a_certain_name'
        result = execEngine.profile("MATCH node:A_LABEL WHERE node.name = 'a_certain_name' RETURN node");
        System.out.println(result.dumpToString());
        System.out.println(result.executionPlanDescription());

        // Try to use wildcards, no such luck
        result = execEngine.profile("MATCH node:A_LABEL WHERE node.name = '*certain*' RETURN node");
        System.out.println(result.dumpToString());
        System.out.println(result.executionPlanDescription());

        tx.success();
        tx.finish();
        db.shutdown();
    }

}

输出:

+--------------------------------+
| node                           |
+--------------------------------+
| Node[1]{name:"a_certain_name"} |
+--------------------------------+
1 row

PatternMatch(g="", _rows=1, _db_hits=0)
Filter(pred="hasLabel(node: A_LABEL)", _rows=1, _db_hits=0)
  NodeByLabel(label="A_LABEL", identifier="node", _rows=1, _db_hits=0)
+--------------------------------+
| node                           |
+--------------------------------+
| Node[1]{name:"a_certain_name"} |
+--------------------------------+
1 row

PatternMatch(g="", _rows=1, _db_hits=0)
Filter(pred="(LiteralRegularExpression AND hasLabel(node: A_LABEL))", _rows=1, _db_hits=1)
  NodeByLabel(label="A_LABEL", identifier="node", _rows=1, _db_hits=0)
+--------------------------------+
| node                           |
+--------------------------------+
| Node[1]{name:"a_certain_name"} |
+--------------------------------+
1 row

PatternMatch(g="", _rows=1, _db_hits=0)
Filter(pred="(Property == Literal(a_certain_name) AND hasLabel(node: A_LABEL))", _rows=1, _db_hits=1)
  NodeByLabel(label="A_LABEL", identifier="node", _rows=1, _db_hits=0)
+------+
| node |
+------+
+------+
0 row

PatternMatch(g="", _rows=0, _db_hits=0)
Filter(pred="(Property == Literal(*certain*) AND hasLabel(node: A_LABEL))", _rows=0, _db_hits=1)
  NodeByLabel(label="A_LABEL", identifier="node", _rows=1, _db_hits=0)

1 个答案:

答案 0 :(得分:1)

全文索引等尚未到来,可能在2.1中。暂时,做1.9索引来获得它。它的主要设计是将复杂的参数传递给标签索引,并在提交语法之前配置需要编写的全文,地理等内容。

感谢你们所有出色的SO贡献@tstorms!

/彼得