Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-16-2021 05:29 AM
Hello All,
I wanted to write a query that can give an attribute name search where it has an attribute name matching a provided name.
For example, return the nodes where any node has an attribute e.g. named "value". Not a value of "value" but an attribute named value.
The result list should output
"The following nodes have attributes named "value":
Bow this heading, show results in rows where the columns of the rows shown are:
node id, node label(s)
The result list should output
"The following nodes have attributes named "value":
Obviously replace “value” with whatever the attribute name was given in query Preformatted text
Query:
with "value" as m
match (n) where m in keys(n)
return n as The following nodes have attributes named
+[m]
I have checked the google and Neo4j docs but couldn't find a way to do that. Any help would be appreciated!
Thank you in advance!
Thanks,
Rishabh
03-16-2021 07:45 AM
Have you tried treating the node as a property map? This should work:
WITH "value" as propertyName
MATCH (u)
WHERE u[propertyName] IS NOT NULL
RETURN u;
The key trick here is that properties can be used like maps; with each property name (in this case, a variable called propertyName
acting as a key
03-16-2021 08:23 AM
@david.allen Thank you for response! but looks like I couldn't explain it properly in the post.
My question is how we can return "value" / propertyName in return statement:
Something like : RETURN u as The following nodes have attributes named value/propertyName
Obviously replace “value” with whatever the attribute name was given in query
Thanks,
Rishbah
03-16-2021 08:41 AM
Perhaps I still don't understand, but you can do this with simple string concatenation:
WITH "value" as propertyName
RETURN "The following nodes have attributes named " + propertyName;
03-16-2021 08:52 AM
I tried the string concatenation but that didn't work. I get below error when I try to concatenate.
I want something like this. Where I have written manually "value"
03-16-2021 09:20 AM
Try this:
match (c) where exists (c.value)
with collect(distinct labels(c)) as lbls
return 'the following labels have value as property:' + lbls
03-16-2021 09:36 AM
@ameyasoft No luck, We are not able to concatenate anything after the return statement. Not sure if this is possible or not in Neo4j.
03-16-2021 09:55 AM
You are trying return node 'n' as a string and this is where it's failing.
Use RETURN `The following nodes have attributes named` +[m]
03-16-2021 10:09 AM
I think, I am not able to explain my problem. I able to run below query successfully with out any error and it is giving the required nodes but my client want specific text "The following nodes have attributes named: value". "Value" is the string which user is searching. So we can't hard code it. So I want searched string at the end but not getting the way to do.
with "value" as m
match (n) where m in keys(n)
with n, collect(distinct labels(n)) as lbls
return n as `The following nodes have attributes named:`
03-16-2021 11:49 AM
Try this:
match (c) where exists (c.value)
with collect(distinct labels(c)) as the_following_labels_have_value_as_property
return the_following_labels_have_value_as_property
03-16-2021 12:49 PM
@ameyasoft Thanks for answering this but I will request you to read my post and comments so that you can understand my problem.
03-17-2021 09:36 PM
I think I see the problem.
This doesn't really have to do with what's happening before or after a RETURN, this has to do with limitations for how we are allowed to alias variables in Cypher, since what you're actually trying to do is alias a variable such that the variable is assembled dynamically.
And that currently isn't allowed. Aliases for variables must be hard coded in the query, they cannot be dynamic.
What you could do, in your client code that is going to make the query to Neo4j, is to dynamically append the value into the query string before you send that off to Neo4j for execution, so once it reaches Neo4j, it is essentially hardcoded.
Or, if you want to handle this entirely within the Cypher query itself, you can create a map with a dynamic key, and return that map, but you'll have to use some functions from APOC Procedures to do this:
WITH "value" as m
MATCH (n)
WHERE m in keys(n)
WITH m, collect(n) as nodes
WITH nodes, "The following nodes have attributes named '" + m + "'" as key
RETURN apoc.map.fromValues([key, nodes]) as result
That's as close as you can get.
All the sessions of the conference are now available online