特にインデックス作成と組み合わせて、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)