简体   繁体   English

Neo4j标签索引搜索,带通配符

[英]Neo4j label index searches with wildcards

I'm trying out the new label feature in Neo4j 2.0 (M3), especially in combination with indexing. 我正在尝试Neo4j 2.0(M3)中的新标签功能,尤其是与索引结合使用时。 Lucene indexes seem to be referred to as legacy, so I guess Label indexes are the way to go. Lucene索引似乎被称为旧式索引,因此我猜想Label索引是必经之路。 How should I write wildcard queries with this new approach? 如何使用这种新方法编写通配符查询? Am I missing something in the code below or is this feature yet to be implemented? 我是否在下面的代码中缺少某些内容,或者此功能尚待实现? You could do this with regex (as shown below), but than you don't use the schema index. 您可以使用正则表达式执行此操作(如下所示),但不要使用架构索引。

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();
    }

}

Output: 输出:

+--------------------------------+
| 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)

Fulltext indexing etc is yet to come, likely in 2.1. 全文索引等尚未出现,可能在2.1中出现。 For the time being, do the 1.9 indexing to get that. 目前,请执行1.9索引来获取该索引。 It's mainly design around passing complex parameters to label indexes, and configuring things like fulltext, geo etc that need to be speced out before committing to a syntax for that. 它主要是围绕将复杂参数传递给标签索引以及配置诸如全文本,地理等之类的东西来设计的,这些东西在提交给它的语法之前需要进行选择。

Thanks for all your great SO-contributions @tstorms! 感谢您对@tstorms所做的巨大贡献!

/peter /彼得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM