Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs on dynamic properties #972

Merged
merged 7 commits into from
Aug 22, 2024

Conversation

gem-neo4j
Copy link
Contributor

@gem-neo4j gem-neo4j commented May 31, 2024

No description provided.

@gem-neo4j
Copy link
Contributor Author

Copy link
Contributor

@JoelBergstrand JoelBergstrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I think there is a spelling misstake otherwise clear and well explained.

@gem-neo4j gem-neo4j force-pushed the dev_set_dynamic_properties branch from 7b7ebfd to 1bb0556 Compare June 25, 2024 07:29
@gem-neo4j
Copy link
Contributor Author

@JoelBergstrand I have added a new part for dynamic labels for SET/REMOVE now as well :D

@gem-neo4j gem-neo4j force-pushed the dev_set_dynamic_properties branch from 1bb0556 to 6eb3b4e Compare August 9, 2024 09:24
@gem-neo4j gem-neo4j added the 5.24 label Aug 9, 2024
@gem-neo4j gem-neo4j force-pushed the dev_set_dynamic_properties branch from 6eb3b4e to fe4cd12 Compare August 12, 2024 12:13
@gem-neo4j gem-neo4j marked this pull request as ready for review August 12, 2024 12:13
Copy link
Collaborator

@JPryce-Aklundh JPryce-Aklundh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gem-neo4j! There were a few queries here which I think may need more explanation (I have added suggestions to this end - let me know what you think 😸 )

Comment on lines 90 to 91
WITH [k IN keys(n) WHERE k CONTAINS "Test" | k] as propertyKeys
FOREACH (i IN propertyKeys | REMOVE k[i])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See subsequent comment for context

Suggested change
WITH [k IN keys(n) WHERE k CONTAINS "Test" | k] as propertyKeys
FOREACH (i IN propertyKeys | REMOVE k[i])
WITH [k IN keys(n) WHERE k CONTAINS "Test" | k] as propertyKeys // <1>
FOREACH (i IN propertyKeys | REMOVE k[i]) // <2>

FOREACH (i IN propertyKeys | REMOVE k[i])
RETURN n.name, keys(n);
----

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this query needs a bit more explanation (though Im not sure my attempt is accurate 😄 )

Suggested change
<1> The xref:function/list.adoc#functions-key[keys()] function retrieves all property keys of the matched nodes, and a xref:values-and-types/lists.adoc#cypher-list-comprehension[list comprehension]filters these keys to include only those that contain the substring "Test", assigning the resulting list to the variable `propertyKeys`.
<2> The xref:clauses/foreach.adoc[`FOREACH`] clause iterates over each key in the `propertyKeys` list and removes the corresponding property using the `REMOVE` clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should be clear that it gets all the keys of the node (singular) it is operating on, it isn't getting the keys of all the matched nodes (plural), but otherwise I think this good.

Could even mention it removes the corresponding property dynamically 😏 hehe but that is probably obvious 😅

== Dynamically removing a label

`REMOVE` can be used to remove a label on a node even when the label is not statically known.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the relevant syntax here as well?

[source, cypher, indent=0]
----
MATCH (n {name: 'Peter'})
UNWIND labels(n) AS label
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UNWIND labels(n) AS label
UNWIND labels(n) AS label // <1>

REMOVE n:$(label)
RETURN n.name, labels(n)
----

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<1> xref:clauses/unwind.adoc[`UNWIND`] is used here to transform the list of labels from the xref:functions/list.adoc#functions-list[`lists()]` function into separate rows, allowing subsequent operations to be performed on each label individually.

[source, cypher]
----
MATCH (n)
FOREACH (k IN keys(n) | SET n[k + "Copy"] = n[k])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FOREACH (k IN keys(n) | SET n[k + "Copy"] = n[k])
FOREACH (k IN keys(n) | SET n[k + "Copy"] = n[k]) // <1>

FOREACH (k IN keys(n) | SET n[k + "Copy"] = n[k])
RETURN n.name, keys(n);
----

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<1> The xref:clauses/foreach.adoc#[`FOREACH`] clause iterates over each property key `k` obtained from the xref:function/list.adoc#functions-key[`keys()`] function.
For each key, it sets a new property on the nodes with a key name of `k` + "Copy" and copies the value from the original property.

@@ -522,3 +585,28 @@ The newly-labeled node is returned by the query.
2+d|Rows: 1 +
Labels added: 2
|===

[[set-set-multiple-dynamic-labels-on-a-node]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[set-set-multiple-dynamic-labels-on-a-node]]
[role=label--new-5.24]
[[set-set-multiple-dynamic-labels-on-a-node]]

.Query
[source, cypher]
----
WITH COLLECT { UNWIND range(0,3) AS id RETURN "Label" + id } as labels
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WITH COLLECT { UNWIND range(0,3) AS id RETURN "Label" + id } as labels
WITH COLLECT { UNWIND range(0,3) AS id RETURN "Label" + id } as labels //<1>

SET n:$(labels)
RETURN n.name, labels(n) AS labels
----

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<1> A xref:subqueries/collect.adoc[`COLLECT`] subquery aggregates the results of `UNWIND range(0,3) AS id RETURN "Label" + id`, which generates a `LIST<STRING>` strings ("Label0", "Label1", "Label2", "Label3"), and assigns it to the variable `labels`.

[source, cypher]
----
MATCH (n:Swedish)
SET n:$(n.name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have an example where the label to add comes from a Cypher parameter, what would the syntax look like ?

SET n:$($label) ?

Same for multiple labels.

@gem-neo4j gem-neo4j force-pushed the dev_set_dynamic_properties branch 4 times, most recently from 2da8a1d to d31e717 Compare August 16, 2024 13:38
@gem-neo4j gem-neo4j force-pushed the dev_set_dynamic_properties branch from d31e717 to 1524325 Compare August 16, 2024 14:09
Copy link
Contributor

@JoelBergstrand JoelBergstrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No additional comments from me.

@neo-technology-commit-status-publisher
Copy link
Collaborator

Thanks for the documentation updates.

The preview documentation has now been torn down - reopening this PR will republish it.

@JPryce-Aklundh JPryce-Aklundh merged commit 0ab44c1 into neo4j:dev Aug 22, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants