How to pass Collection Parameters to Repository Queries for Neo4J - neo4j

Using Spring Data for Neo4J I want to pass a collection as a parameter to a repository query:
#Query("MATCH (product:Product) WHERE ANY(c IN product.categories WHERE c IN {categories}) RETURN product")
Iterable<Product> findAllWithCategories(#Param("categories") List<String> categories);
On the command line the corresponding query runs successfully and delivers the right results:
MATCH (product:Product) WHERE ANY(c IN product.categories WHERE c IN ["Märklin","Fleischmann"]) RETURN product
But from within Java no results are returned, when the findAllWithCategories method is invoked with a list of categories. The strange thing is that it looks like the correct http-request is sent to the DB:
request: {"statements":[{"statement":"MATCH (product:Product) WHERE ANY(c IN product.categories WHERE c IN {categories}) RETURN product","parameters":{"categories":["Märklin","Fleischmann"]},"resultDataContents":["graph"],"includeStats":false}]}
Any idea what goes wrong here? In general how can I pass collections as parameters to a repository query to Neo4J?
Edit
The same query run without the Spring Data repository but with the more lower-level Neo4JTemplate gets the same result, which is really strange as the Query on the command line does what it should.
private final String FIND_PRODUCTS_WITH_CATEGORIES = "MATCH (product:Product) WHERE ANY(c IN product.categories WHERE c IN {categories}) RETURN product";
String[] categories = ...
Map<String, Object> map = new HashedMap<>();
map.put("categories", categories);
products = neo4j.queryForObjects(Product.class, FIND_PRODUCTS_WITH_CATEGORIES, map);
I don't think there is anything wrong with the query statement, but rather with the parameter of list type.
Edit
After half a day I tried the bolt driver, instead of the http driver, and everything was okay (using the version 2.0.6 of the driver, version 2.1.0 throw a strange exception)

The queries are all right. The handover of arrays or lists as parameters to the queries works. The problem is the driver: no success using the http-driver, the bolt-driver seems to be buggy in the newest version 2.1.0. But with bolt 2.0.6 I got it running.

Related

ResultConsumedException with Micronaut and Neo4j: Cannot access records on this result any more as the result has already been consumed

I'm attempting to get a simple app working with Micronaut 1.3.5 and Neo4J 4.0.0. I'm using the io.micronaut.configuration:micronaut-neo4j-bolt:2.0.0 driver. I have code like this:
public Stream<Map<String, Object>> findAll() {
try (Session s = driver.session()) {
String stmt = "MATCH (n:Product) RETURN n";
return s.readTransaction(tx -> tx.run(stmt)).list(r -> r.asMap()).stream();
}
}
And I'm getting the following exception:
result has already been consumed or the query runner where the result is created has already been closed.
org.neo4j.driver.exceptions.ResultConsumedException: Cannot access records on this result any more as the result has already been consumed or the query runner where the result is created has already been closed.
I've done some googling and found a few things that would suggest this is normal behavior, but nothing that discusses the actual solution. I'm assuming it has something to do with the async nature of the driver/session.
The way that would work is,
Stream<Map<String, Object>> stream = s.readTransaction(tx -> {
Result result = tx.run(stmt);
return result.list(r -> r.asMap()).stream();
});
The results need to be processed within the readTransaction
There are bunch of examples at https://github.com/neo4j/neo4j-java-driver/tree/4.1/examples/src/main/java/org/neo4j/docs/driver which you may find useful

How do we generate a cypher query by using Java and APOC for Neo4j?

I am trying to create my own procedure in Java in order to use it for Neo4j.I wanted to know how we can execute Cypher code in Java ?
I tried to use graphDB.execute() function but it doesn't work.
I just want to execute a basic code in Java by using Neo4j libraries.
Example of a basic code I want to execute:
[EDIT]
public class Test
{
#Context public GraphDatabaseService graphDb;
#UserFunction
public Result test() {
Result result = graphDb.execute("MATCH (n:Actor)\n" +
"RETURN n.name AS name\n" +
"UNION ALL MATCH (n:Movie)\n" +
"RETURN n.title AS name", new HashMap<String, Object>());
return result;
}
}
If you want to display nodes (as in the graphical result view in the browser), then you have to return the nodes themselves (and/or relationships and/or paths), not the properties alone (names and titles). You'll also need this to be a procedure, not a function. Procedures can yield streams of nodes, functions can only return single values.
Change this to a procedure, and change your return type to be something like Stream<NodeResult> where NodeResult is a POJO that has a public Node field.
You'll need to change your return accordingly.

Can I write a Neo4j Plugin to intercept and modify CYPHER queries

In my system, I would like to intercept and change Cypher queries as they come in, one alternative is to modify them before sending them from my middle layer to the graph - but is there a way to have a plugin do the conversion for me in the graph itself?
I'd like to do some of the following:
If someone identifying themselves as members of group A, imagine I'd like to change their request from:
MATCH(f:Film)-[r:REVIEWED_BY]-(u:User {id:"1337"})
to:
MATCH(p:Product)-[p:PURCHASED_BY]-(u:User {id:"1337"})
Is something like this possible? Or do I have to write the traversals in Java directly to achieve this?
Of course you can. You can do ANYTHING in Neo4j. Just grab the cypher string in an unmanaged extension that receives a post request, alter it any way you want, execute it with the graphdb.execute method and return the result as normal.
#POST
#Path("/batch")
public Response alterCypher(String body, #Context GraphDatabaseService db) throws IOException, InterruptedException {
ArrayList<Result> results = new ArrayList<>();
// Validate our input or exit right away
HashMap input = Validators.getValidCypherStatements(body);
ArrayList<HashMap> statements = (ArrayList<HashMap>)input.get("statements");
for (HashMap statement : statements) {
// write the alterQuery method to change the queries.
String alteredQuery = alterQuery((String)statement.get("statement"));
Result result = db.execute(alteredQuery, (Map)statement.getOrDefault("parameters", new HashMap<>()));
results.add(result);
}
// or go the results and return them however you want
// see https://github.com/dmontag/neo4j-unmanaged-extension-template/blob/master/src/main/java/org/neo4j/example/unmanagedextension/MyService.java#L36
return Response.ok().build();
}
At this time it's not possible to extend or modify Cypher queries.
If you need that I recommend you to use Transaction Event API - http://graphaware.com/neo4j/transactions/2014/07/11/neo4j-transaction-event-api.html
With that you should be able to change what query returns.

Not able to access iterator() from RestTraverser, it gives exception java.lang.IllegalAccessError

I am implementing traversal framework using neo4j java-rest-binding project.
Code is as follows:
RestAPI db = new RestAPIFacade("http://localhost:7474/db/data");
RestNode n21 = db.getNodeById(21);
Map<String,Object> traversalDesc = new HashMap<String, Object>();
traversalDesc.put("order", "breadth_first");
traversalDesc.put("uniqueness", "node_global");
traversalDesc.put("uniqueness", "relationship_global");
traversalDesc.put("returnType", "fullpath");
traversalDesc.put("max_depth", 2);
RestTraverser traverser = db.traverse(n21, traversalDesc);
Iterable<Node> nodes = traverser.nodes();
System.out.println("All Nodes:"); // First Task
for(Node n:nodes){
System.out.println(n.getId());
}
Iterable<Relationship> rels = traverser.relationships();
System.out.println("All Relations:"); // Second Task
for(Relationship r:rels){
System.out.println(r.getId());
}
Iterator<Path> paths = traverser.iterator(); // Third Task
while(paths.hasNext()){
System.out.println(paths.next());
}
I need to do 3 tasks as commented in the code:
Print all the node IDs related to node no. 21
Print all the relation IDs related to node no. 21
Traverse all the paths related to node no. 21
Tasks 1 & 2 are working fine.
But when I try to do traverser.iterator() in 3rd task it throws an Exception saying:
java.lang.IllegalAccessError: tried to access class org.neo4j.helpers.collection.WrappingResourceIterator from class org.neo4j.rest.graphdb.traversal.RestTraverser
Can anyone please check why this is happening or if I am doing wrong then what is the right method to do it.
Thanks in Advance.
I don't believe using the Neo4j Traversal Framework via the REST DB binding is properly supported, nor is it advisable. If you traverse via REST, each node and each relationship will be retrieved over the network as the traversal proceeds, incurring a tremendous overhead for the traversal.
Edit: The above is not true, the REST traverser is smarter than I thought.
In general, it will be faster to use Cypher, and access the Neo4j Server using JDBC. Read more about JDBC here: https://github.com/neo4j-contrib/neo4j-jdbc
If you really want to use the Traversal Framework, you should use Server Extensions, which allow you to design a traversal to run on the server itself, and then only move the result of the traversal over the network. Read more about server extensions here: http://docs.neo4j.org/chunked/stable/server-unmanaged-extensions.html

Map cypher query result to domain object

I just started to use Neo4j with Spring Data and I'm not able to recover graph objects and convert them back to domain objects. I must say I have no previous experience in that kind of databases.
In that way, I'm using Spring Data repositories. For standard queries the repository code is auto-generated but I would like to define some custom methods, so I created my custom repository as explained here.
For example, I would like to be able to update a certain property value (currentValue property in this case) from a given edge between two certain nodes (searchByUserName is a previously defined index in my node entity which represents a user). I'm using the query method from the Neo4j template in my custom repository implementation as follows:
public class TwitterUserRepositoryImpl implements TwitterUserRepositoryCustom{
#Autowired
private Neo4jOperations neo4jTemplate;
public void updateRelationshipValueByUserName(
String userAUserName, String userBUserName, double value){
HashedMap params = new HashedMap();
params.put("userAUserName", userAUserName);
params.put("userBUserName", userBUserName);
params.put("value", value);
String query = "START x=node:searchByUserName(userName = {userAUserName}), " +
"y=node:searchByUserName(userName = {userBUserName})" +
" MATCH (x)-[r:FOLLOWS]->(y)" +
" SET r.currentValue = {value}" +
" RETURN r";
Result<Map<String, Object>> relationships = neo4jTemplate.query(query, params);
/* let's try to recover the relationship entity and do some more stuff */
}
The cypher query returns an "edge" between two users, where its relationship type is "FOLLOWS", simulating a Twitter users network. I have no idea how to convert this QueryResult object back to my RelationshipEntity object. Is that possible?
Just use the result-dsl: http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1118
relationships.to(MyRelationshipEntity.class)
will return you a Result<MyRelationshipEntity> which is an Iterable

Resources