2

我成功地使用 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)。

4

2 回答 2

2

编写自己的代码.. Cypher 很慢。仅查看带有查询标签(“USER”)的 4 个节点的差异。full db 只有 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
于 2016-02-22T13:17:15.200 回答
1

您会很快发现您可能希望从 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,除了非常简单的情况(例如“通过属性和值获取节点”)

于 2015-04-22T21:17:49.170 回答