# Variable edge properties

**URL:** <https://community.neo4j.com/t/variable-edge-properties/38065>\
**Category:** Neo4j Graph Platform\
**Tags:** apoc, cypher, relationship\
**Created:** [May 10, 2021, 3:07am UTC](https://community.neo4j.com/t/variable-edge-properties/38065 "2021-05-10T03:07:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [May 10, 2021, 3:07am UTC](https://community.neo4j.com/t/variable-edge-properties/38065/1 "2021-05-10T03:07:50Z")

</div>

I am constructing a graph in Neo4j like this. First, I create the nodes with an array property. Then I check if any two nodes have common elements between the arrays (using `apoc` intersection call) and if so I create a relationship between the nodes.

For example, lets say, nodes `N` and `M` has arrays `S = [A, B, C]` and `T = [A, B, D]` as properties, respectively, then `N` and `M` will have a relationship `r` between them, since they share the common elements `A, B` .

Now, each of the array elements also has properties associated with them and I want to populate the edge with common elements together with their properties. That is, we have `A.name, A.age, B.name, B.age` associated with them. Note that `A` and `B` are variables themselves. That is, `A` could be `Doctor X` and `B` could be `Doctor Y` .

How do I annotate the edges with the variable name and their properties so that I can query the database using the variable name? I somehow need to preserve the association between elements and their properties within the edges as edge properties.

How do I do this? Thank you very much.

---

<div class="post-metadata">

**Author:** ![Joel](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joel/32/17191_2.png) [@Joel](https://community.neo4j.com/u/Joel)\
**Post date:** [May 12, 2021, 3:08pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/2 "2021-05-12T15:08:48Z")

</div>

You can add properties to edges and query by them, but be aware that you can't use the regular indexes on relationship properties, but it is possible to add a full text search.

[https://neo4j.com/docs/cypher-manual/current/administration/indexes-for-full-text-search/](https://neo4j.com/docs/cypher-manual/current/administration/indexes-for-full-text-search/)

That said, at times I do use unindexed edge properties in queries, but always in combination with label and node property filters. Here is a very simple example (note: you can create both nodes and relationship with properties in one cypher command, but this is may be easier to follow)

```auto
// create node A
CREATE (:NODE {name:'A'});

// create node B
CREATE (:NODE {name:'B'});'

// check that they were created
MATCH (x :NODE {name:'A'})
MATCH (y :NODE {name:'B'})
RETURN x, y

// create relationship, and set value (using set instead of inline)
// (there are two ways to relationship properties, see reference below for inline style)
MATCH (x :NODE {name:'A'})
MATCH (y :NODE {name:'B'})
CREATE (x)-[r:KNOWS]->(y)
SET r.relationship='friends'

// query relationship property for value and return a path
MATCH t=(:NODE)-[r]-(:NODE)
WHERE r.relationship='friends'
RETURN t

// same query, but return nodes and relationship
MATCH (x:NODE)-[r]-(y:NODE)
WHERE r.relationship='friends'
RETURN x ,r, y

```

Reference:

> **[CREATE - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/create/#create-create-a-relationship-and-set-properties)**
>
> The \`CREATE\` clause is used to create nodes and relationships.

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [May 12, 2021, 8:24pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/3 "2021-05-12T20:24:37Z")

</div>

It's not clear how you stored array elements and their associated properties. Please share the Cypher query you are using in your test.

---

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [May 13, 2021, 2:12pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/4 "2021-05-13T14:12:06Z")

</div>

@Joel Thanks. Yes, I realize we can do that. This is not what I meant. Each element in the array has properties that need to be set and there is a variable number of them.

---

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [May 13, 2021, 2:15pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/5 "2021-05-13T14:15:48Z")

</div>

@ameyasoft What do you mean? You mean how I create the nodes? There are no different from how we usually do, using MERGE.

---

<div class="post-metadata">

**Author:** ![Joel](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joel/32/17191_2.png) [@Joel](https://community.neo4j.com/u/Joel)\
**Post date:** [May 13, 2021, 6:03pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/6 "2021-05-13T18:03:04Z")

</div>

A note of clarification, Neo4j allows properties to be added adhoc to any node or relationship, they do not need to be the same, this is schema-less, so any given node/relationships can have any property, or not have it. Just be aware that you may want/need to check if a property exists, before checking if the property has a specific value, otherwise the query results may not be what you expect.

If you want to dynamically create a series of properties on the fly it seems likely you'll need to construct the CREATE/MERGE query on the fly as a string, then execute it. There is an APOC function that enables this, (if you aren't already writing code in a language like java, python)

> **[apoc.cypher.run - APOC Extended Documentation](https://neo4j.com/labs/apoc/4.1/overview/apoc.cypher/apoc.cypher.run/)**
>
> This section contains reference documentation for the apoc.cypher.run procedure.

There are many ways to unwind an array, in neo4j/cypher or outside, then add properties, since the property name is dynamic I think you will need to create cypher strings, then execute them, but there might be another clever way around it? (interested to know if there is)

---

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [May 14, 2021, 12:53pm UTC](https://community.neo4j.com/t/variable-edge-properties/38065/7 "2021-05-14T12:53:44Z")

</div>

@Joel again, thanks. Yes, I am creating relationships and their properties dynamically through apoc. What I am looking for is preserving an association between two properties without introducing any pseudo node or relationship.
