0

I have 2 nodes: name and city. and a relationship between these two is (name) [:LIVES_IN]->(city). I am trying to generate a query to find out who are those people living in city X(where X will be coming from a text box).

I am trying this construct this query following Luanne and Micheal Hunger's suggestion:

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
    }

But this query does not give any result as well as any exception.

Is my query formation right? Can any one tell me how can I resolve this? Shall I change my neo4j version because I am following everything as Luanne and Miheal HUnger has asked.

Thank You

5
  • Please check carefully- you are returning name but doing a result.columnsAs("n"). Also make sure you execute this query in the browser/shell and verify if it returns anything please. Commented Mar 13, 2014 at 10:33
  • @Luanne: You are trying most for me. But all turns in vain. I am trying to scratch from the basic. So I have moved to run the query in browser, but it fails to work. I posted a new question in stack overflow on this Commented Mar 13, 2014 at 14:59
  • In your code above, only change this line Iterator<Node> n_column = result.columnAs( "n" ); to Iterator<Node> n_column = result.columnAs( "name" ); Commented Mar 13, 2014 at 15:59
  • @Luanne: I posted the same question again by giving all the details of my code. The result appears same: No result is displayed Commented Mar 13, 2014 at 18:56
  • @Luanne: I posted the code what have I written in netbeans as it is. What will you suggest? shall i change my neo4j version. Commented Mar 14, 2014 at 7:42

1 Answer 1

2

You have not quoted the city value:

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city=dhaka return n.name

should be

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city='dhaka' return n.name

Also, please use parameters:

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city={city} return n.name

http://docs.neo4j.org/chunked/stable/tutorials-cypher-parameters-java.html

EDIT Since you've modified your query, from Michael's comment, try

   Map<String,Object> params=new HashMap<String,Object>();
   params.put("city_name","dhaka");
   result=engine.execute("MATCH (city:City {city:{city_name})<-[:LIVES_IN]-(person) RETURN person",params);

   Iterator<Node> n_column = result.columnAs( "person" );

Create an index on City before that: CREATE INDEX ON :City(city) (http://docs.neo4j.org/chunked/stable/query-schema-index.html)

Please also go through the following learning material:

The online training course: http://www.neo4j.org/learn/online_course

The manual: http://docs.neo4j.org/chunked/stable/

Learn Cypher: http://www.neo4j.org/tracks/cypher_track_start

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for you kind reply. I change my code as such: result=engine.execute("start n=node(*) MATCH n-[:LIVES_IN]->city where city.city='"+jTextField2.getText()+"' return n.name"); But the following exception arises then: Exception in thread "AWT-EventQueue-0" org.neo4j.cypher.EntityNotFoundException: No column named 'n' was found. Found: ("n.name") ............I just updated my question above accroding to your answer.........
Because there is no column n. You're returning n.name
Actually as you are using Neo4j 2.0 stay away from using start n=node(*) just create an index for :City(city) and do MATCH (city:City {city:{city_name})<-[:LIVES_IN]-(person) return person.name
@Luanne: I am a new in neo4j, So I am trying in various ways. I updated my query above by returning only n But it still shows error :NODE[1] has no property with propertyKey="name".
@Luanne: I am trying to follow your suggestion. But still error arise. I updated my code above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.