tstorms
tstorms

Reputation: 5001

Neo4j label index searches with wildcards

I'm trying out the new label feature in Neo4j 2.0 (M3), especially in combination with indexing. Lucene indexes seem to be referred to as legacy, so I guess Label indexes are the way to go. 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)

Upvotes: 0

Views: 824

Answers (1)

Peter Neubauer
Peter Neubauer

Reputation: 6331

Fulltext indexing etc is yet to come, likely in 2.1. For the time being, do the 1.9 indexing to get that. 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!

/peter

Upvotes: 1

Related Questions