Search code examples
neo4jcypherneo4jclient

Struggling to perform an Index Cypher Query in Neo4jClient.NET


I am try to perform a query on my NameIndex in Neo4j using the Neo4jClient for .NET but i get this error:

{"Received an unexpected HTTP status when executing the request.\r\n\r\nThe response status was: 500 Internal Server Error\r\n\r\nThe raw response body was: {\"exception\":\"NullPointerException\",\"stacktrace\":[\"org.apache.lucene.util.SimpleStringInterner.intern(SimpleStringInterner.java:54)\",\"org.apache.lucene.util.StringHelper.intern(StringHelper.java:39)\",\"org.apache.lucene.index.Term.<init>(Term.java:38)\",\"org.apache.lucene.queryParser.QueryParser.getFieldQuery(QueryParser.java:643)\",\"org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1421)\",\"org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1309)\",\"org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1237)\",\"org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1226)\",\"org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:206)\",\"org.neo4j.index.impl.lucene.IndexType.query(IndexType.java:300)\",\"org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:227)\",\"org.neo4j.server.rest.web.DatabaseActions.getIndexedNodesByQuery(DatabaseActions.java:977)\",\"org.neo4j.server.rest.web.DatabaseActions.getIndexedNodesByQuery(DatabaseActions.java:960)\",\"org.neo4j.server.rest.web.RestfulGraphDatabase.getIndexedNodesByQuery(RestfulGraphDatabase.java:692)\",\"java.lang.reflect.Method.invoke(Unknown Source)\"]}"}

My method looks as follows:

public IEnumerable GraphGetNodeByName(string NodeName)
        {
            GraphOperationsLogger.Trace("Now entering GraphGetNodeByName() method");

            IEnumerable QueryResult = null;


                GraphOperationsLogger.Trace("Now performing the query");
                var query = client_connection.QueryIndex<GraphNode>("NameIndex", IndexFor.Node,
                //Here I want to pass in the NodeName into the query
                //@"Start n = node:NameIndex(Name = '"+ NodeName +"') return n;");
                //Here I am hard-coding the NodeName
                    @"Start n = node:NameIndex(Name = ""Mike"") return n;");
                QueryResult = query.ToList();


            return QueryResult;
        }

I ideally would like to pass in the NodeName into the query but that is not working therefore I have tried hard-coding it in and that also doesn't work. Both scenarios produce the same error message?


Solution

  • The method you are calling, IGraphClient.QueryIndex is not a Cypher method. It's a wrapper on http://docs.neo4j.org/chunked/milestone/rest-api-indexes.html#rest-api-find-node-by-query. It's an older API, from before Cypher existed.

    You're already half way there though, because your code comments include the Cypher query:

    Start n = node:NameIndex(Name = "Mike")
    return n;
    

    So, let's just translate that into C#:

    client
        .Cypher
        .Start(new CypherStartBitWithNodeIndexLookup("n", "NameIndex", "Name", "Mike"))
        .Return<Node<Person>>("n");
    

    Always start your Cypher queries from IGraphClient.Cypher or NodeReference.StartCypher (which is just a shortcut to the former).

    There are some other issues with your method:

    1. You're returning a raw IEnumerable. What is in it? You should return IEnumerable<T>.
    2. You're calling query.ToList(). I'd be surprised if that even compiles. You want to call ToList on the results so that the enumerable is hit.
    3. In C#, your local variables should be in camelCase not PascalCase. That is, queryResult instead of QueryResults.

    Combining all of those points, your method should be:

    public IEnumerable<Person> GetPeopleByName(string name)
    {
        return graphClient
            .Cypher
            .Start(new CypherStartBitWithNodeIndexLookup("n", "NameIndex", "Name", "Mike"))
            .Return<Node<Person>>("n")
            .Results
            .ToList();
    }