Re: [Neo4j] Cypher: Default value for non-existent relationship property

678 views
Skip to first unread message

Peter Neubauer

unread,
Jun 7, 2012, 2:10:44 AM6/7/12
to ne...@googlegroups.com
Trey,
I think you could use COALESCE,
http://docs.neo4j.org/chunked/snapshot/query-function.html#_predicates,
and then even compose your query into several parts with WITH, see
http://docs.neo4j.org/chunked/snapshot/query-with.html in order to
take your calculated return, something like

START n=node(99)
MATCH n-[r1:likes|eats]-(c)-[r2:likes|eats]-x WHERE type(r1)=type(r2)
return x.name as name, (r2.weight + COALESCE(r2.activity?, 0)) as
boost LIMIT 10;

Does that work?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


On Wed, Jun 6, 2012 at 8:57 PM, Trey Sandworm <timbr...@gmail.com> wrote:
> I've been looking at manuals about this and still can't figure out if I can
> do it, and if so, how. I would greatly appreciate help with it.
>
> In all my relationship types I have a property called 'weight'.
> In only 1 relationship type I have a property called 'activity'.
>
> I'm trying to return something like this
>
> START n=node(99) MATCH n-[r1:likes|eats]-(c)-[r2:likes|eats]-x WHERE
> type(r1)=type(r2) RETURN x.name, (r2.weight + r2.activity) as boost LIMIT
> 10;
>
> The problem is that I can't do any operation on activity because it's a
> property of only 1 relationship type.
> Ideally I need to accomplish something like:
>
> (r2.weight + if (has(r2.activity) then r2.activity else 0))
>
> putting 'r2.activity?' in RETURN works partially in at least listing the
> activity as NULL, but I would like to do operations with it as above as
> well.
>
> Is there any way to specify myself a default value for non existent
> r2.activity, similarly like r2.activity? lists it as NULL, but then also
> being able to do mathematical calculations as well?
>
> Thanks in advance

Trey Sandworm

unread,
Jun 7, 2012, 1:08:09 PM6/7/12
to Neo4j
Thanks a lot Peter, it works great.

For some reason the documentation for COALESCE did not ring a bell in
my head that I would be able to do this with it. :)

Much appreciate the help.

Neo/Cypher is awesome.

Peter Neubauer

unread,
Jun 7, 2012, 1:09:01 PM6/7/12
to ne...@googlegroups.com
Great,
btw, if you get a nice example going, a blog would rock.

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Trey Sandworm

unread,
Jun 7, 2012, 1:47:36 PM6/7/12
to Neo4j
Blog would have to wait for the time being, but here are my examples
here:

Example #1: All my relationships have a weight property, which is used
for boosting recommendations results

START n=node(3) MATCH n-[r1:likes|eats]-(c)-[r2:likes|eats]-x WHERE
type(r1)=type(r2) AND (NOT n.id = x.id) AND (NOT (n-[:likes]-x))
RETURN x.name, SUM(r2.weight) as boost
order by boost desc limit 30;

Example #2: The relationship type 'likes' has 1 more property that I
want to use for bosting in addition to weight.
So the overall boost in this case is (weight + (activity*2)), where
activity exists. If activity does not exists, it would still be just
weight.

START n=node(3) MATCH n-[r1:likes|eats]-(c)-[r2:likes|eats]-x WHERE
type(r1)=type(r2) AND (NOT n.id = x.id) AND (NOT (n-[:likes]-x))
RETURN x.name, SUM(ROUND(r2.weight + (COALESCE(r2.activity?, 0) * 2)))
as boost
order by boost desc limit 30;

Note, COALESCE / multiply operation returns float results. But SUM
does not seem to work on integer + float, so I had to add the ROUND
before I do the SUM.
Without ROUND gives:
SyntaxException: r2.weight + COALESCE(r2.activity?,0.0) * 2.0 expected
to be of type NumberType but it is of type ScalarType

Peter Neubauer

unread,
Jun 7, 2012, 1:49:25 PM6/7/12
to ne...@googlegroups.com
Trey,
do you even have a sample underlying graph? In that case, I will add
this to the cypher cookbook, like
http://docs.neo4j.org/chunked/snapshot/cypher-cookbook-friend-finding.html

WDYT?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Trey Sandworm

unread,
Jun 8, 2012, 9:19:15 AM6/8/12
to Neo4j
I can try and get you one Peter, if I knew how to generate these
(seemingly standardized) SVG graphs from my Neo4J data.

Peter Neubauer

unread,
Jun 8, 2012, 9:23:06 AM6/8/12
to ne...@googlegroups.com
Try

http://console.neo4j.org/usage.html, then press the share button,
there are a number of options ...

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Trey Sandworm

unread,
Jun 8, 2012, 11:34:55 AM6/8/12
to Neo4j
Thanks Peter.

Here's something:
http://tinyurl.com/7dkpxtg

May be easier to understand how we get to the final query by pasting
these steps:


1) START n=node(1) MATCH n-[r1:KNOWS|WORKSAT]-(c)-[r2:KNOWS|WORKSAT]-x
WHERE type(r1)=type(r2) AND (NOT n.name = x.name) AND (NOT (n-[:KNOWS]-
x)) RETURN n.name,x.name,type(r2) limit 10;

2) START n=node(1) MATCH n-[r1:KNOWS|WORKSAT]-(c)-[r2:KNOWS|WORKSAT]-x
WHERE type(r1)=type(r2) AND (NOT n.name = x.name) AND (NOT (n-[:KNOWS]-
x)) RETURN n.name, x.name, SUM(r2.weight) as boost order by boost desc
limit 10;

3) START n=node(1) MATCH n-[r1:KNOWS|WORKSAT]-(c)-[r2:KNOWS|WORKSAT]-x
WHERE type(r1)=type(r2) AND (NOT n.name = x.name) AND (NOT (n-[:KNOWS]-
x)) RETURN n.name, x.name, SUM(ROUND(r2.weight +
(COALESCE(r2.activity?, 0) * 2))) as boost order by boost desc limit
10;

Peter Neubauer

unread,
Jun 12, 2012, 11:14:54 PM6/12/12
to ne...@googlegroups.com
Thanks a lot Trey!

WDYT about http://docs.neo4j.org/chunked/snapshot/cypher-cookbook-boostingrecommendations.html
for the manual version? I simplified the query a bit also.

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Reply all
Reply to author
Forward
0 new messages