gpt4 book ai didi

java - Java应用程序中的Neo4j查询:No result is displaying[Still fail to solve it]

转载 作者:行者123 更新时间:2023-12-01 23:08:33 25 4
gpt4 key购买 nike

更新的问题

这个问题是基于我之前的post1 post2在 Neo4j 上。

我正在尝试找出居住在城市的人。我使用 2 个节点:person 和 city,这两个节点之间的关系是 (person)-[:LIVES_IN]->(city)。

根据之前的帖子各位导师的建议,我做了一些修改。

但它仅显示为该城市添加的当前人名。

我的代码如下:(这里保留3个文本框,2个按钮和1个文本区域)

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.IteratorUtil;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;

enum labels implements Label {
Person,
City
}
public class registrationFrame extends javax.swing.JFrame {


public static final String DB_PATH = "D://data";
public static GraphDatabaseService graphDb = null;
Node person;
Node password;
Node city;
String nodeResulta;
String rows = "";
Schema schema = null;
boolean e = false;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Schema index on Person and person node creation
try (Transaction tx = graphDb.beginTx()) {
schema = graphDb.schema();

for (IndexDefinition id : graphDb.schema().getIndexes()) {
e = true;
}
if (!e)
schema.indexFor(labels.Person).on("name").create();
tx.success();
}
try (Transaction tx = graphDb.beginTx();) {
person = graphDb.createNode(labels.Person);
person.setProperty("name", jTextField1.getText());
person.setProperty("password", jPasswordField1.getPassword());
graphDb.index().forNodes("name").add(person, "name", jTextField1.getText());

tx.success();
}
//Schema index on City and city node creation and relationship establishment
try (Transaction tx = graphDb.beginTx()) {
schema = graphDb.schema();

for (IndexDefinition id : graphDb.schema().getIndexes()) {
e = true;
}
if (!e)
schema.indexFor(labels.City).on("city_name").create();
tx.success();
}
try (Transaction tx = graphDb.beginTx();) {
city = graphDb.createNode(labels.City);
city.setProperty("city_name", jTextField2.getText());
graphDb.index().forNodes("city_name").add(city, "city_name", jTextField2.getText());

person.createRelationshipTo(city, RelTypes.LIVES_IN);

tx.success();
}

}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
ExecutionEngine engine = new ExecutionEngine(graphDb);
ExecutionResult result;
String temp=jTextField2.getText();
Map<String,Object> params=new HashMap<>();

try (Transaction ignored = graphDb.beginTx()) {

params.put("c_name",temp);
// **the query advised by Luanne for finding person living in same city**
result=engine.execute("MATCH (city:City {city_name:{c_name}})<-[:LIVES_IN]-(person) RETURN person",params);
System.out.println(result);
Iterator<Node> n_column = result.columnAs("person");

for (Node node : IteratorUtil.asIterable(n_column)) {

System.out.println("hiii");
nodeResulta = node + ": " + node.getProperty("name") + '\n';
}
// END SNIPPET: items
}

jTextArea1.setText(nodeResulta);

}
public static enum RelTypes implements RelationshipType {

LIVES_IN,
FRIEND,
CUISINE,
LIKES,
IN
}

}

我尝试调试代码,发现结果仅返回为该城市添加的当前人员,而不是人员列表。

这种行为背后的原因是什么?有哪位导师可以在这方面给我建议吗?我应该更改我的 neo4j 版本吗,因为我正在遵循 Luanne 和 Miheal Hunger 要求的一切。

谢谢你预先感谢您。

最佳答案

尝试坚持使用 Cypher,这会让事情变得更容易。

执行online tutorial让您跟上进度。还可以使用 Cypher Reference Card .

在 Neo4j 浏览器中对模型进行原型(prototype)设计,直到所有创建和查询语句正常工作。

然后使用带有参数占位符的密码语句,例如{name} 通过 executionEngine.execute(query,params) 从您的程序中获取。

现在您有太多的移动部件,解释其中一个部件并不能帮助您学习概念。

关于java - Java应用程序中的Neo4j查询:No result is displaying[Still fail to solve it],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22388269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com