Consulta usando neo4j incrustado en java
Tengo 2 nodos: nombre y ciudad. y una relación entre estos dos es (nombre) [: LIVES_IN] -> (ciudad). Estoy tratando de generar una consulta para averiguar quiénes son esas personas que viven en la ciudad X (de donde X vendrá de un cuadro de texto).
Estoy intentando construir esta consulta siguiendo la sugerencia de Luanne y Micheal Hunger:
import java.io.IOException;
import java.util.ArrayList;
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 org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
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;
public registrationFrame() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//node and relationship creation code
try (Transaction tx = graphDb.beginTx();) {
person = graphDb.createNode();
person.setProperty("name", jTextField1.getText());
person.setProperty("password", jPasswordField1.getPassword());
graphDb.index().forNodes("name").add(person, "name", jTextField1.getText());
city = graphDb.createNode();
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) {//query code
ExecutionEngine engine = new ExecutionEngine(graphDb);
ExecutionResult result;
String temp=jTextField2.getText();
Map<String,Object> params=new HashMap<>();
//result = engine.execute("START n=node(*) MATCH (x:city)<-[:LIVES_IN]-(y:person) where x.name='"+jTextField2.getText()+"' RETURN y.name;");
//List<String> columns = result.columns();
//Iterator<Node> n_column = result.columnAs( "person" );
try (Transaction ignored = graphDb.beginTx()) {
//result = engine.execute("START n=node(*) MATCH (x:city)<-[:LIVES_IN]-(y:person) where x.name='"+temp+"' RETURN y");
// END SNIPPET: execute
// START SNIPPET: items
//result = engine.execute("START n=node(*) MATCH (x:city) RETURN x");//this query also returns nothing
params.put("c_name",temp);
result=engine.execute("MATCH (city_name: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)) {
// note: we're grabbing the name property from the node,
// not from the n.name in this case.
nodeResulta = node + ": " + node.getProperty("name") + '\n';
//nodeResult1.add(node.getProperty( "name" ).toString());
}
// END SNIPPET: items
}
jTextArea1.setText(nodeResulta);// output will show here
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new registrationFrame().setVisible(true);
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
registerShutdownHook(graphDb);
//System.out.println("Created Social Graph!!");
}
});
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
public static enum RelTypes implements RelationshipType {
LIVES_IN,
FRIEND,
CUISINE,
LIKES,
IN
}
Pero esta consulta no ofrece ningún resultado ni ninguna excepción.
¿Es correcta la formación de mi consulta? ¿Alguien puede decirme cómo puedo resolver esto? ¿Debo cambiar mi versión neo4j porque estoy siguiendo todo como Luanne y Miheal HUnger me han pedido?
Gracias