我成功使用Neo4J Java API(目前版本为2.2.1)来执行这样的简单查询:
Label label = DynamicLabel.label("User");
ResourceIterator<Node> providers = graphDb.findNodes(
label, "username", "player1"));
但是有没有办法允许除了简单匹配(在我的情况下为String)之外的其他内容?我可以执行REGEX,还是提供密钥可以匹配的可能值列表以进行字符串比较?如果是的话,那里有什么例子吗?
我已经挖掘了文档和我最喜欢的搜索引擎,除了直接字符串匹配之外似乎找不到任何东西(例如:http://neo4j.com/docs/2.2.1/tutorials-java-embedded-new-index.html)。
答案 0 :(得分:2)
编写自己的代码.. Cypher很慢。仅查看带有查询标签的4个节点的差异(&#34; USER&#34;)。完整数据库只有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");
}
}
------------------------------------------------------------------------
--- 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
答案 1 :(得分:1)
您很快就会发现您可能希望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,除非是非常简单的情况(例如&#34;按属性和值获取节点&#34;)