How to only return subset of larger query in GraphQL without returning the whole thing? - neo4j

I am new to GraphQL and trying to understand how to only return a subsection of the resulting query. For example, here is my cypher query in Neo4j:
MATCH (u:User)-[HAS_FRIEND]-(f:User)
WHERE u.name = "Joe"
RETURN f.name
Notice how the above query only returns the friend names.
GraphQL
Here's a sample of GraphQL version of the above cypher query.
Input
{
"where": {
"name": "Joe"
}
}
Query
query Query($where: NameWhere) {
names(where: $where) {
name
friends {
name
}
}
}
Expected Output
Obviously, I expect output to include friends name array and the name of the user having the friend. But, what if I only want the friends array like the cypher query gives me?
While this is a simple example, I am using a graph database that has a series of connections and my nesting is looking pretty deep, but I only really need a subquery from the result. A great example is with localization - I don't need the country and language (locale), etc. in every query; I only want the subquery result (e.g. the spelling in UK/ENG is colour and in US/ENG is color).

Neo4j offers a Javascript GraphQL library that is able to handle your use case (and many more)!
Please have a look at the documentation for information on how to get started. See even the example queries, where this one is fairly close to what you are looking for.
Anyhow, the GraphQL query provided above, using the #neo4j/graphql library, could look like so:
query Query {
users (where: { name: "Joe" }) {
friends {
name
}
}
}
(Note that the where parameter ("Joe") is provided in-line in this case)

Related

Handling multiple search fields in cypher or py2neo

Just getting started with cypher. Trying to understand best way to handle this case. I need to query database based on several search fields(not always present). Wondering what's the best way to handle it. For e.g I collect data from html post request
storeName = request.form['storeName']
storeCountry = request.form['storeCountry']
storeState = request.form['storeState']
storeCity = request.form['storeState']
I'm using flask framework, so defined this function with py2neo to handle search fields. Not sure if I got it right but if all fields are set correctly, hopefully, something like this should work fine
def get_search_results(skipNumber,limitNumber, storeName, storeCountry,storeState, storeCity):
query = """
MATCH (store:store) WHERE store.name = {storeName}
MATCH (store:store)-[:IS_IN_COUNTRY]->(c:Country) WHERE c.name = {storeCountry}
MATCH (store:store)-[:IS_IN_STATE]->(s:State) WHERE s.name = {storeState}
MATCH (store:store)-[:IS_IN_CITY]->(ct:City) WHERE ct.name = {storeCity}
RETURN user SKIP {skip} LIMIT {limit}
"""
return graph.cypher.execute(query, skip=skipNumber, limit=limitNumber, storeName=storeName, storeCountry=storeCountry, storeState=storeState, storeCity=storeCity)
I would like to handle cases where users only submit one or two fields.
For e.g
1. if users only submit store name, then I should be able to return all the nodes with that store name.
2. If users only submit store name and country. Return all the stores nodes in that country
3. If users only submit store name, country and state. Return all the stores nodes in that country and state
Is it possible to write a generic cypher query to handle such scenarios i.e ignore cases where field is not set or is None or I need to write different queries to handle each case ?

Cypher query with literal map syntax & dynamic keys

I'd like to make a cypher query that generates a specific json output. Part of this output includes an object with a dynamic amount of keys relative to the children of a parent node:
{
...
"parent_keystring" : {
child_node_one.name : child_node_one.foo
child_node_two.name : child_node_two.foo
child_node_three.name : child_node_three.foo
child_node_four.name : child_node_four.foo
child_node_five.name : child_node_five.foo
}
}
I've tried to create a cypher query but I do not believe I am close to achieving the desired output mentioned above:
MATCH (n)-[relone:SPECIFIC_RELATIONSHIP]->(child_node)
WHERE n.id='839930493049039430'
RETURN n.id AS id,
n.name AS name,
labels(n)[0] AS type,
{
COLLECT({
child.name : children.foo
}) AS rel_two_representation
} AS parent_keystring
I had planned for children.foo to be a count of how many occurrences of each particular relationship/child of the parent. Is there a way to make use of the reduce function? Where a report would generate based on analyzing the array proposed below? ie report would be a json object where each key is a distinct RELATIONSHIP and the property value would be the amount of times that relationship stems from the parent node?
Thank you greatly in advance for guidance you can offer.
I'm not sure that Cypher will let you use a variable to determine an object's key. Would using an Array work for you?
COLLECT([child.name, children.foo]) AS rel_two_representation
I think, Neo4j Server API output by itself should be considered as any database output (like MySQL). Even if it is possible to achieve, with default functionality, desired output - it is not natural way for database.
Probably you should look into creating your own server plugin. This allows you to implement any custom logic, with desired output.

How to get total number of db-hits from Cypher query within a Java code?

I am trying to get total number of db-hits from my Cypher query. For some reason I always get 0 when calling this:
String query = "PROFILE MATCH (a)-[r]-(b)-[p]-(c)-[q]-(a) RETURN a,b,c";
Result result = database.execute(query);
while (result.hasNext()) {
result.next();
}
System.out.println(result.getExecutionPlanDescription().getProfilerStatistics().getDbHits());
The database seems to be ok. Is there something wrong about the way of reaching such value?
ExecutionPlanDescription is a tree like structure. Most likely the top element does not directly hit the database by itself, e.g. a projection.
So you need to write a recursive function using ExecutionPlanDescription.getChildren() to drill to the individual parts of the query plan. E.g. if one of the children (or sub*-children) is a plan of type Expand you can use plan.getProfilerStatistics().getDbHits().

py2neo return number of nodes and relationships created

I need to create a python function such that it adds nodes and relationship to a graph and returns the number of created nodes and relationships.
I have added the nodes and relationship using graph.cypher.execute().
arr_len = len(dic_st[story_id]['PER'])
for j in dic_st[story_id]['PER']:
graph.cypher.execute("MERGE (n:PER {name:{name}})",name = j[0].upper()) #creating the nodes of PER in the story
print j[0]
for j in range(0,arr_len):
for k in range(j+1,arr_len):
graph.cypher.execute("MATCH (p1:PER {name:{name1}}), (p2:PER {name:{name2}}) WHERE upper(p1.name)<>upper(p2.name) CREATE UNIQUE (p1)-[r:in_same_doc {st_id:{st_id}}]-(p2)", name1=dic_st[story_id]['PER'][j][0].upper(),name2=dic_st[story_id]['PER'][k][0].upper(),st_id=story_id) #linking the edges for PER nodes
What I need is to return the number of new nodes and relationships created.
What I get to know from the neo4j documentation is that there is something called "ON CREATE" and "ON MATCH" for MERGE in cypher, but thats not being very useful.
The browser interface for neo4j do actually shows the number of nodes and relationship updated. This is what I need to return, but I am not getting quite the way for it to access it.
Any help please.
In case you need the exact counts of properties either created or updated then you have use "Match" with "Create" or "Match" with "Set" and then count the size of results. Merge may not return which ones are updated and which ones are created.
When you post your query against the Cypher endpoint of the neo4j REST API without using py2neo, you can include the argument "includeStats": true in your post request to get the node/relationship statistics. See this question for an example.
As far as I can tell, py2neo currently does not support additional parameters for the Cypher query (even though it is using the same API endpoints under the hood).
In Python, you could do something like this (using the requests and json packages):
import requests
import json
payload = {
"statements": [{
"statement": "CREATE (t:Test) RETURN t",
"includeStats": True
}]
}
r = requests.post('http://your_server_host:7474/db/data/transaction/commit',
data=json.dumps(payload))
print(r.text)
The response will include statistics about the number of nodes created etc.
{
"stats":{
"contains_updates":true,
"nodes_created":1,
"nodes_deleted":0,
"properties_set":1,
"relationships_created":0,
"relationship_deleted":0,
"labels_added":1,
"labels_removed":0,
"indexes_added":0,
"indexes_removed":0,
"constraints_added":0,
"constraints_removed":0
}
}
After executing your query using x = session.run(...) you can use x.summary.counters to get the statistics noted in Martin Perusse's answer. See the documentation here.
In older versions the counters are available as a "private" field under x._summary.counters.

Neo4j Cypher 2.0: Pass in params for batch match - relationships

This question is similar to this: create relationships between nodes in parallel and this Neo4j: Best way to batch relate nodes using Cypher?
I would like to parameterize a batch for creating relationships using a Cypher query and Neo4jClient (a c# client for Neo4j).
How would I write this out (specifically focusing on performance) - i.e. using only match and create statements and not Merge as merge ends up timing out for some reason?
I was thinking I could do something like this ( as stated in that second SO link)
MATCH (s:ContactPlayer {ContactPrefixTypeId:{cptid}})
MATCH (c:ContactPrefixType {ContactPrefixTypeId:{cptid}})
CREATE c-[:CONTACT_PLAYER]->s
with params:
{
"query":...,
"params": {
"cptid":id1
}
}
But this doesn't work, because it's trying to match the property as an Array.
I modified it to use WHERE x.Y IN {params} but this was extremely slow. The second recommendation was to try to use the transactional endpoint for Neo4j but I'm unsure how to do that with Neo4jClient.
This was the recommendation from the 2nd SO link above:
{
"statements":[
"statement":...,
"parameters": {
"cptid":id1
},
"statement":...,
"parameters": {
"cptid":id2
}
]
}
I did see this pull request but did not see that it had been implemented yet: https://github.com/Readify/Neo4jClient/pull/26
Without transaction support, is there another way to do this?
What's the performance when you use the query below?
USING PERIODIC COMMIT 1000
MATCH (s:ContactPlayer), (c:ContactPrefixType)
WHERE s.ContactPrefixTypeId = c.ContactPrefixTypeId
CREATE c-[:CONTACT_PLAYER]->s
If you want to try out the periodic commit statement, you'll have to use version 2.1.0-M1 for now. Otherwise, you can leave it out.

Resources