Neo4J Java findNodes 与非单个字符串匹配

Neo4J Java findNodes with other than a single string match

我成功地使用 Neo4J Java API(当前版本 2.2.1)来执行如下简单查询:

Label label = DynamicLabel.label("User");
ResourceIterator<Node> providers = graphDb.findNodes(
        label, "username", "player1"));

但是有没有办法允许值的简单匹配(在我的例子中是字符串)以外的东西?我可以执行 REGEX 或提供键可以匹配的可能值列表以进行字符串比较吗?如果是这样,那里有任何例子吗?

我搜索了文档和我最喜欢的搜索引擎,除了直接字符串匹配(例如:http://neo4j.com/docs/2.2.1/tutorials-java-embedded-new-index.html)之外似乎找不到任何其他内容。

您很快就会发现您可能想要 execute cypher queries from inside of java 进行这种查询。

String query = "MATCH (n:Label) where n.username =~ ".*foo regex.*" RETURN n;";
try ( Transaction ignored = db.beginTx();
      Result result = db.execute(query) )
{
    while ( result.hasNext() )
    {
         // Do nifty stuff with results.
    }
}

您使用的常规嵌入式 API 方法不会支持正则表达式或大量其他内容,我建议使用 cypher 除了非常简单的情况(例如 "get node by property and value")

自己写代码。Cypher 很慢。仅查看具有查询标签 ("USER") 的 4 个节点的差异。完整的数据库只有 8 个节点!

package com.mycompany.neo4j_0;

import java.util.Iterator;
import java.util.Map;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class NewMain {

private static void registerShutdownHook(final GraphDatabaseService graphDb) {
    // Registers a shutdown hook for the Neo4j instance so that it
    // shuts down nicely when the VM exits (even if you "Ctrl-C" the
    // running application).
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            graphDb.shutdown();
        }
    });
}

public static void PrintPropertyKeysAndValues(Node t_node) {
    Map<String, Object> propertiesMap = t_node.getAllProperties();
    propertiesMap.entrySet().stream().forEach((entry) -> {
        System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
    });
}

public static void GetNodesWithLabel_and_PropertyKey(GraphDatabaseService graphDB, String label, String PropertyKey) {
    ResourceIterator<Node> nodes = graphDB.findNodes(DynamicLabel.label(label));
    int flag = 0;
    while (nodes.hasNext()) {
        Node t_node = nodes.next();
        Map<String, Object> propertiesMap = t_node.getAllProperties();
        for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
            //System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
            if (entry.getKey().equals(PropertyKey)) {
                PrintPropertyKeysAndValues(t_node);
                flag = 1;
            }
        }
    }
    if (flag == 0) {
        System.out.println("No Nodes found with this property");
    }
}

public static void GetNodesWithLabel_and_PropertyKeyCypher(GraphDatabaseService graphDB, String label, String PropertyKey) {
    String query = "MATCH (n:" + label + ") where n." + PropertyKey + " =~ '.*' RETURN n;";
    Result result = graphDB.execute(query);
    Iterator<Node> nodes = result.columnAs("n");

    int flag = 0;
    while (nodes.hasNext()) {
        Node t_node = (Node) nodes.next();
        Map<String, Object> propertiesMap = t_node.getAllProperties();
        for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
            //System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
            if (entry.getKey().equals(PropertyKey)) {
                PrintPropertyKeysAndValues(t_node);
                flag = 1;
            }
        }
    }
    if (flag == 0) {
        System.out.println("No Nodes found with this property");
    }
}

public static void main(String[] args) {

// Find nodes with Specified Label and with a Specified PropertyKey.
    long start_time = 0, end_time = 0, difference1 = 0, difference2 = 0;
    String label="USER";
    String PropertyKey="name";


    GraphDatabaseService graphDB;

    graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("Z:\neo4j-community-2.3.2\data\test\graph.db");
    registerShutdownHook(graphDB);

    try (Transaction tx = graphDB.beginTx()) {

        System.out.println("==================JavaApi==================");
        start_time = System.nanoTime();
        GetNodesWithLabel_and_PropertyKey(graphDB, label, PropertyKey);
        end_time = System.nanoTime();
        difference1 = end_time - start_time;
        System.out.println("java time: " + difference1);

        System.out.println("==================Cypher==================");
        start_time = System.nanoTime();
        GetNodesWithLabel_and_PropertyKeyCypher(graphDB, label, PropertyKey);
        end_time = System.nanoTime();
        difference2 = end_time - start_time;
        System.out.println("Cypher time: " + difference2);

        tx.success();
    }
    System.out.println("====================================");
    System.out.println("difference in nanoseconds : "+(difference2-difference1));
    System.out.println("Done successfully");

}
}

------------------------------------------------------------------------

构建 neo4j_0 1.0-SNAPSHOT

--- exec-maven-plugin:1.2.1:exec (default-cli) @ neo4j_0 ---
==================JavaApi==================
Key : name and Value: Steve 
Key : name and Value: Linda 
Key : name and Value: Michael 
Key : name and Value: Rebecca 
java time: 43800364
==================Cypher==================
Key : name and Value: Steve 
Key : name and Value: Linda 
Key : name and Value: Michael 
Key : name and Value: Rebecca 
Cypher time: 1583408758
====================================
difference in nanoseconds : 1539608394
Done successfully
------------------------------------------------------------------------
BUILD SUCCESS