Query Node from Neo4j by using java - java

Now I am using RESTFul API to interface with Neo4j. My issue is: for example I have already created a Node1 in Neo4j. Then I just want to create a Node2 and a relationship to connect to Node1. I know I need to query node from Neo4j and return a node. But how to do it? I am new in Neo4j please help.
And I have already build a delete function by using java to delete all nodes and relationships in Neo4j. Here is my code:
public String deleteAllNodeOrRelation() throws ClientHandlerException,
UniformInterfaceException, JDOMException {
String cypherPayload = "{\"query\": \"START a=node(*) MATCH a-[r?]-() DELETE a,r RETURN a\", \"params\":{}}";
String user_name = getUserName(cypherPayload);
return user_name;
}
is the query node function similar with this delete function? and to be noticed, I have properties stored in each node. The property name is "title". Someone told me I can query "title" to search and return the node1. But I still dont know how to do it....

Yes, you would have to enable auto-indexes for your domain-keys just do:
START u1=node:node_auto_index(name={user1}),
u2=node:node_auto_index(name={user2})
CREATE (u1)-[:KNOWS]->(u2)
you pass user1 and user2 as parameters to your function and to the cypher query call.
For some ways of how to call cypher from Java see: https://github.com/jexp/cypher-http-examples

Related

get all graph data including nodes and relationships from neo4j using java into a hashmap

I am trying to get all graph data including nodes and relationships from neo4j using java into a Hashmap to be able to read all nodes and their data for further manipulation with the data. I have tried using this query but it returns an error.
try (Session session = driver.session()) {
return session.run("MATCH (m) RETURN m");
}
I am trying to figure out how can I get the data in the right format.
You can use below example to parse the result from neo4j query. If you get into any issue(s), pls let me know. Thanks.
Iterator<Node> javaNodes = execResult.columnAs("m");
for (Node node : IteratorUtil.asIterable(javaNodes))
{
//parse the node in here
}
Reference: https://neo4j.com/docs/java-reference/current/java-embedded/cypher-java/

Retrieving exact node id in neo4j cypher

I'm using neo4j-jdbc 2.3.2 as my neo4j client for java. When I executed following cypher query
match(p:Person) where p.id_number='761201948V' return p.id; it will return P2547228 as node id. I feel like id is same as other properties of the node as I can use it inside where clauses. But here I'm expecting an integer which can use inside this query START p=node('node.id') return p; Is this id is an internal thing to neo4j db? and is there a way to retrieve this id?
From the following two cyphers what is most efficient one?(if both referring same node)
START p=node('2547223') return p;
match(p:Person) where p.id='P2547228' return p;
You have to use the ID(x) function for this. Note, that ID(x) and x.id are a complete different thing. The former returns the internal node/relationship id managed by Neo4j itself. The latter gives the id property which is managed by the user and not by the database itself.
Also note, that a node/relationship ID is always numeric.
Using START is pretty much old school and shouldn't be used any more (except for accessing manual indexes):
start p=node(2547228) return p
This one is a equivalent statement. It is highly efficient since it just needs to do a simple seek operation on the node store:
match(p:Person) where id(p)=2547228 return p;
Looking for a property requires either a node label scan or a schema index lookup:
match(p:Person) where p.id=2547228 return p;
Just check out the query plans on your own by prefixing the statement with PROFILE.

deleteing a node cypher java

i'm working with neo4j version 2.2.1. I would like to know how to delete a specific node using a cypher query in a java program containing a parameter.
i tried this but it wouldn't work :
Map<String, Object> params = new HashMap<String, Object>();
params.put("numero", "1");
String query1 ="MATCH (pe:Person) WHERE PeNumero={numero} DELETE pe";
Result result1 = graphDb.execute( query1, params);
Person is my node label, and PeNumero is one of its properties.
Thanks a lot, ghrs
In order to delete a node, you'll probably have to delete its relationships too. Otherwise deleting a node would leave "dangling" relationships, which wouldn't mean anything and that's not permitted. Try this instead:
MATCH (pe:Person)-[r]-(m) WHERE PeNumero={numero} DELETE r, pe;
so the problem is finally solved. I used a Cypher query to find the node, retrieve its id and then delete the node using a transaction that gets the node by id.It worked !
Thanks FrobberOfBits and cybersam.
Try like this
MATCH (pe:Person {PeNumero:{numero}})
OPTIONAL MATCH (pe)-[r]-(m)
DELETE r, pe;
It will delete that particular node and if that node having some relationships, then that relationships will get deleted.
You can check references here http://neo4j.com/docs/stable/query-delete.html

ElasticSearch get _id

How can I retrieve the _id elasticsearch field information using Java API? I know I can see this information with head plugin, looking to documents, but it's not necessary for me.
I'm developing a Java project, and all commands, like update, delete, require this _id value. But how can I get it?
Thanks in advance
When executing a search, you get back an object SearchResponse. By calling the method getHits() you get back a list of objects of type SearchHit. This object has a method id().
SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery()).get();
for (SearchHit hit : searchResponse.getHits()) {
String yourId = hit.id();
}

Can I use a manually created index and query nodes using Cypher?

Here I have created a manual/legacy index and added some nodes with certain properties into it.
IndexManager indexy = graphdb.index();
Index<Node>indexery = indexy.forNodes("Main_Twitter_Index");
indexery.add(one,"Name",one.getProperty("Name"));
indexery.add(one,"Email",one.getProperty("Email"));
indexery.add(four,"Name",four.getProperty("Name"));
indexery.add(four,"Email",four.getProperty("Email"));
Now, to query the nodes of that index neo4j suggests query, which uses a key-value pair binding. My question is can I query the same nodes added into the manual index using a simple cypher query like,
START n=node:Main_Twitter_Index(Name = 'Akina')
RETURN n
Which version of Neo4j are you using? The method you describe is the typical index search for anything before 2.0, before they added schema indexing. Your query should work, even in 2.0. Are you having problems running it?

Categories

Resources