2
votes

I am struggeling on how to use indexes in cypher.

After creating and indexing nodes in java I am fine with executing cypher queries on those nodes. I am fine as well with querying those nodes using the created index in java.

However, when I call the index in the cypher statement I get an MissingIndexException.

So, why can't cypher find the index? Do I have to create a separate cypher index? (I have not found anything about that)

I am using version 1.8.2 Here's what I did:

public class IndexTester {

String DB_PATH = "target/java-query-db";
String resultString ="";

GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
ExecutionEngine engine = new ExecutionEngine( db );

IndexManager index = db.index();
Index<Node> personIndex;

Node n;
Node n1;

public static void main( String[] args )
{
    IndexTester indexTester = new IndexTester();
    indexTester.runIndex();
 }


public void runIndex(){

    Transaction tx = db.beginTx();
  try
  {

      personIndex = index.forNodes( "person" );

        n = createAndIndexNode("type", "adult", personIndex, db);         
        addPropertyAndIndexNode("name", "John", personIndex, n);
        addPropertyAndIndexNode("id", "1", personIndex, n);


        n1 = createAndIndexNode("type", "adult", personIndex, db);        
        addPropertyAndIndexNode("name", "Jane", personIndex, n1);
        addPropertyAndIndexNode("id", "2", personIndex, n1);

//This works fine!!

            Node foundNode = personIndex.get("name", "John").getSingle();
        System.out.println("Found Node: " + foundNode.getProperty("name"));

//This throws a MissingIndexException

            resultString = engine.execute( "start m=node:personIndex(name= 'John')  return m" ).toString();
            System.out.println(resultString);

      n.delete();
      n1.delete();

      tx.success();
  }
  finally   
  {
      tx.finish();
  }


}

private Node createAndIndexNode(final String property, final String name, Index<Node> nodeIndex, GraphDatabaseService db ) {
    Node node = db.createNode();
    node.setProperty(property , name);
    nodeIndex.add(node, property, name);
    return node;
}

    public Node addPropertyAndIndexNode(String property, String name, Index<Node> nodeIndex, Node node)
    {
          node.setProperty( property, name );
          nodeIndex.add( node, property, node.getProperty( property ) );
          return node;
    }

}

Any ideas / suggestions how to solve this? Thank you!!

1

1 Answers

3
votes

I think the actual name of your index is just person (as specified here: index.forNodes( "person" );), rather than personIndex.

Try:

start m=node:person(name= 'John')  return m