Using the py2neo tutorial (http://book.py2neo.org/en/latest/cypher/):
from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService()
query = "START a=node(1) RETURN a"
data, metadata = cypher.execute(graph_db, query)
a = data[0][0] # first row, first column
Trying to replicate this, I get:
>data[0][0]
Node('http://localhost:7474/db/data/node/1')
How do I get this to return the actual data, instead of the abstract information?
Your Cypher query returns a node (RETURN a) and so that's what's being passed back: a Node object. If it's the node's properties that you need, you can either then inspect the properties on that node with the get_properties method or return specific properties from the Cypher query instead (RETURN a.name).
Related
Retrieving all the properties of a particular node in Neo4j is a used to one among many queries. But How can I retrieve the properties of a node excluding a particular property, the labels, and the ID ?
If this below query is executed by java program after successful connection:
MATCH (n: `Group_A`: `Topper`) RETURN n
Now, Then the output it prints in screen is:
{"id":4, "labels":["Group_A", "Topper"], "name":"tom", "shift":"morning", "salary":"5000", "blood_group":"AB", "specialisation":"C#"}
I am expecting all the property of the node (n: Group_A: Topper), excluding the property type and its value, the id and the labels associated with the node.
Hence, the desired output in screen will look like:
{"name":"tom", "shift":"morning", "blood_group":"AB", "specialisation":"C#"}
========================================================================
[N.B. - I am working with Neo4j jar file where I am firing query of Neo4j and getting the result using println() method. I have my Neo4j running in the background. All are working fine with the successful connection of Neo4j but I am insearch of the Neo4j query.]
[UPDATED]
To avoid getting node metadata (in your client results), you should not return a node directly. Instead, you can use the PROPERTIES() function to get just the properties in a map:
MATCH (n: `Group_A`: `Topper`)
RETURN PROPERTIES(n) AS props
But if you also want to avoid returning some of the properties (e.g. "salary") as well, you can use the apoc.map.removeKey() function to remove them. Since that function returns a map instead of a node, it will also not contain any metadata (so you would not need to use the PROPERTIES() function in this case). For example:
MATCH (n: `Group_A`: `Topper`)
RETURN apoc.map.removeKey(n, 'salary') AS props
You could use map projection.
All node properties (no metadata)
MATCH (n: `Group_A`: `Topper`)
RETURN n {.*}
Specific properties
MATCH (n: `Group_A`: `Topper`)
RETURN n {.prop1, .prop2}
I've wanting to query for nodes that have a specific value in a string array property. For instance my nodes might have 2 properties, name (a string) and aliases (a string array). I have created an index on both properties using something like CREATE INDEX ON :F2(name).
I can query the name property using something cypher like this, and the result is immediate:
MATCH (p:F2) WHERE p.name = 'john' RETURN p;
I can query the aliases property using cypher like this, and I get the expected result but the response is very slow:
MATCH (p:F2) WHERE ANY(item IN p.aliases WHERE item = 'big john') RETURN p;
This looks like either the query is not optimal or the index is not being used.
Can someone suggest how to do this correctly. I'm pretty new to neo4j and cyper :-(
You could refactor your graph to make alias a node. So that any F2 node has zero or more aliases.
CREATE INDEX ON :Alias(name)
Then you could query it with something like this...
MATCH (p:F2)-[:HAS_ALIAS]->(:Alias {name: 'big john'})
RETURN p
Hello I make a Graph Algorithm Neo4J request in Cypher of the following kind, which first finds the nodes and then the relations between them:
CALL algo.pageRank.stream('MATCH (u:User{uid:"0ee14110-426a-11e8-9d67-e79789c69fd7"}),
(ctx:Context{name:"news180417"}), (u)<-[:BY]-(c:Concept)-[:AT]->(ctx)
RETURN DISTINCT id(c) as id',
'CALL apoc.index.relationships("TO","user:0ee14110-426a-11e8-9d67-e79789c69fd7")
YIELD rel, start, end WITH DISTINCT rel, start, end MATCH (ctx:Context)
WHERE rel.context = ctx.uid AND (ctx.name="news180417" )
RETURN DISTINCT id(start) AS source, id(end) AS target',
{graph:'cypher', iterations:5});
Which works fine. However, when I try to return c.uid instead of its Neo4J id() the Graph Algorithms don't accept it.
Does it mean I can only operate using Neo4J ids in Graph Algorithms?
When you use Cypher projection with the Graph Algorithms procedures, you pass 2 Cypher statements (and a config map).
The first Cypher statement must return an id variable whose value is the native ID of a node.
The second Cypher statement must return source and target variables whose values are also node IDs.
So, yes, your Cypher statements must always return neo4j native IDs.
I have the following cypher code:
MATCH (n)
WHERE toLower(n.name) STARTS WITH toLower('ja')
RETURN n
This case-insensitive query returns all the nodes which their names start with the substring "ja". For example if I execute this in my db it will return ["Javier", "Jacinto", "Jasper", "Jacob"]
I need this script to also remove the unwanted nodes on this list, for example let's say that an array containing ["Jasper, Javier"] is sent to the data access layer indicating that those two nodes shouldn't be returned, leaving the final query result as follows: ["Jacinto", "Jacob"]
How can I perform this?
If you know before making the query which items should be excluded you can say:
MATCH (n)
WHERE toLower(n.name) STARTS WITH toLower('ja')
AND NOT (toLower(n.name) IN ['jasper', 'javier'])
RETURN n
I am using neo4JClient to query a list of nodes in the graph and present them on the web browser as a table. In order to get data, the following code is used.
var query = client
.Cypher
.Match("DataSpace")
.Return(DataSpace => DataSpace.As<DataSpace>());
var longBooks = query.Results;
jsonString = JsonConvert.SerializeObject(longBooks, Formatting.None, settings);
context.Response.Write(jsonString);
The graph contains various other nodes.
The Resultset at the Browser Window is :
[{"DataSpaceName":"DS1","DataSpaceDescription":"First Dataspace"},{"DataSpaceName":"DS2","DataSpaceDescription":"Second Dataspace"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
There are empty nodes that get returned, which I think correlates to the other nodes in the graph.
Can I get rid of the Empty Nodes ?
The query returns all nodes because that's what you asked for. Your Neo4jClient query simply translates to this Cypher query:
MATCH DataSpace
RETURN DataSpace
In this case DataSpace is simply an identifier that represents every node, without additional constraints.
Do your DataSpace nodes have anything that separates them from other nodes, e.g. a label, a common node that they're all connected to, a unique property?
In case the nodes have a unique DataSpace label, you can for query them with:
var query = client
.Cypher
.Match("ds:DataSpace")
.Return(ds => ds.As<DataSpace>());
In case DataSpaceName is a property that is only present on DataSpace nodes, you can try:
var query = client
.Cypher
.Match("ds")
.Where("has(ds.DataSpaceName)")
.Return(ds => ds.As<DataSpace>());