Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Cypher questions - return updates count, update 2 things in a single query
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Wujek Srujek  
View profile  
 More options Oct 15 2012, 4:56 am
From: Wujek Srujek <wujek.sru...@gmail.com>
Date: Mon, 15 Oct 2012 10:56:30 +0200
Local: Mon, Oct 15 2012 4:56 am
Subject: Cypher questions - return updates count, update 2 things in a single query

Hi. I have the following structure:
NodeA has relationships to multiple NodeB; the relationships have a
property called 'preferred', which takes boolean values. I need to be able
to atomically change the preferred NodeB. I am doing this with Cypher on
version 1.8.
What I have now is two queries which look like this:

// reset the currently preferred, if any
start me = node({me}) match me-[r:LIKES]->t set r.preferred = false where
r.preferred = true // this might result in no relationships changed, that's
Ok - no preference yet
// set the new preferred
start me = node({me}), t = node({t}) match me-[r:LIKES]->t set r.preferred
= true

(Parameters 'me' and 't' are Nodes that I already have and don't have to
look up.)

This will be done in a single transaction so that it is atomic. However, I
have a couple of questions:

1. Is it possible to do such a thing in a single query? Is this practical?
Is it better to issue 2 simpler queries than 1 complex?
2. I would like to learn if the second query actually changed any
relationship. This is for error checking - node 't' in the second query
might not be related to 'me' at all, in which case the query would change 0
relationships. I think the easiest way for me would be to check the count
of changed relationships if I could return that, and if it is 0, node 't'
was unrelated to 'me'. How can I express this in Cypher?

Regards,
wujek


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Abdul Azeez Shaik  
View profile  
 More options Oct 15 2012, 6:57 am
From: Abdul Azeez Shaik <azeeztechni...@gmail.com>
Date: Mon, 15 Oct 2012 16:26:21 +0530
Local: Mon, Oct 15 2012 6:56 am
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

Hi Wujek,

You can use WITH clause to have two sub-queries in one query.
Your query would be,
start me = node({me}), like=node{{like}} match me-[r:LIKES]->t set
r.preferred = false where r.preferred = true
WITH me, like
match me-[r:LIKES]->t set r.preferred = true

Hope this helps.

Thanks,
Abdul

On Mon, Oct 15, 2012 at 2:26 PM, Wujek Srujek <wujek.sru...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andres Taylor  
View profile  
 More options Oct 15 2012, 7:03 am
From: Andres Taylor <andres.tay...@neotechnology.com>
Date: Mon, 15 Oct 2012 13:03:30 +0200
Local: Mon, Oct 15 2012 7:03 am
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

On Mon, Oct 15, 2012 at 10:56 AM, Wujek Srujek <wujek.sru...@gmail.com>wrote:

> 2. I would like to learn if the second query actually changed any
> relationship. This is for error checking - node 't' in the second query
> might not be related to 'me' at all, in which case the query would change 0
> relationships. I think the easiest way for me would be to check the count
> of changed relationships if I could return that, and if it is 0, node 't'
> was unrelated to 'me'. How can I express this in Cypher?

We actually already keep this information, and it is exposed in the Scala
API, but not in the Java API. We should definitely move that over.

https://github.com/neo4j/community/blob/master/cypher/src/main/scala/...

Andrés


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wujek  
View profile  
 More options Oct 17 2012, 4:27 am
From: wujek <wujek.sru...@gmail.com>
Date: Wed, 17 Oct 2012 01:27:43 -0700 (PDT)
Local: Wed, Oct 17 2012 4:27 am
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

Hi.
Thanks for your answers.
So how do I do the latter (getting the count of changed nodes) now, in Java
/ Groovy? Is the only way issuing another query?

As a side question - why this duality in APIs? Why does Java need a special
package with facade-like classes that delegate to the Scala API? Is
Java-Scala integration so difficult on the Java side that there is need for
such solutions? It is an honest question, as I never used Scala classes
from Java, but I used Groovy without any problems whatsoever, and this
comes rather surprising.

wujek


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Neubauer  
View profile  
 More options Oct 17 2012, 5:32 am
From: Peter Neubauer <peter.neuba...@neotechnology.com>
Date: Wed, 17 Oct 2012 11:32:06 +0200
Local: Wed, Oct 17 2012 5:32 am
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query
Wujek,
mostly, the Scala Iterators and Collections used internally in Cypher
do not have any easy resolution in Java-land. It is unfortunate that
the class names overlap, we will pull them apart at some point. You
are right, there should only be ONE public API, and most probably that
is going to be the Java API since it is most applicable over different
langauges.

Cheers,

/peter neubauer

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

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wujek  
View profile  
 More options Oct 17 2012, 3:01 pm
From: wujek <wujek.sru...@gmail.com>
Date: Wed, 17 Oct 2012 12:01:45 -0700 (PDT)
Local: Wed, Oct 17 2012 3:01 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

Hi. This:

start me = node({me}), like=node{{like}} match me-[r:LIKES]->t set
r.preferred = false where r.preferred = true
with me, like
match me-[r:LIKES]->t set r.preferred = true

doesn't work for me in the following scenario: there is no preferred yet
(all relations have preferred = false), so the first part doesn't update
anything, and then the second doesn't update anything as well.
When I first set a preferred to some node, then the query works fine. Which
means I cannot be used as is in my use case, as there is no preferred
initially, and this seems to prevent setting one.

Am I doing something wrong?

wujek


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Abdul Azeez Shaik  
View profile  
 More options Oct 17 2012, 4:03 pm
From: Abdul Azeez Shaik <azeeztechni...@gmail.com>
Date: Thu, 18 Oct 2012 01:33:02 +0530
Local: Wed, Oct 17 2012 4:03 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

AFIK, WHERE predicate can be removed, as anyways r.preferred is false.
But, i don't know what would be the performance effect on that. May be
Peter or Michael can answer this better.

BTW, what is your data size?

Thanks,
Abdul


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Abdul Azeez Shaik  
View profile  
 More options Oct 17 2012, 4:07 pm
From: Abdul Azeez Shaik <azeeztechni...@gmail.com>
Date: Thu, 18 Oct 2012 01:36:53 +0530
Local: Wed, Oct 17 2012 4:06 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

Also, you can do other way round,

start me = node({me}), like=node{{like}} match me-[r:LIKES]->like set
r.preferred = true
WITH me
match me-[r:LIKES]->t set r.preferred = false where r.preferred = true

On Thu, Oct 18, 2012 at 1:33 AM, Abdul Azeez Shaik <azeeztechni...@gmail.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wujek  
View profile  
 More options Oct 18 2012, 12:31 pm
From: wujek <wujek.sru...@gmail.com>
Date: Thu, 18 Oct 2012 09:31:19 -0700 (PDT)
Local: Thurs, Oct 18 2012 12:31 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

Hi. I still can't get it to work.
Your comment about getting rid of the r.preferred predicate as it is always
false is wrong - it is false only the first time, when no preference is
defined; after the initial preferred is set, setting the next one must
unset the previous one, which will then mean the predicate is will find
something. And this predicate proves to be the problematic one, as the
'with' subquery doesn't work if the where is false. I am not sure why that
is, and the documentation on 'with' is very vague on this.

The second option of inverting the query parts also doesn't work. I also
don't follow it logically - whereas the first part sets the new preferred
to true for a reference to the node I want, the second part will just reset
it to false, as it does that for every relationship of type LIKE, right?

Maybe I wasn't specific enough about what I want to achieve. So: A person
has many things that she likes, and there can be either one or no preferred
thing. If there is none, setting a new preferred will just set it; if there
is one already, setting another thing as preferred must unset the
previously set one.

I am able to do it with 2 queries - first unset the previous preferred
thingy, and then set the new one. It seems it is not possible to do it in
just one query - it's fine, I was just curious if this is possible.

Thanks for your help, though.

wujek


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wes Freeman  
View profile  
 More options Oct 18 2012, 1:42 pm
From: Wes Freeman <freeman....@gmail.com>
Date: Thu, 18 Oct 2012 13:41:41 -0400
Local: Thurs, Oct 18 2012 1:41 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

It is better to do it with one query, if possible, because it creates a
sort of transaction (and is obviously more efficient over REST especially,
since it's one request instead of two).

I didn't have time to look at this before, but here goes a pretty hacky
attempt:

start me=node(3), like=node(2)
match me-[l:likes]->()
// this where is optional, but this way there's less modified
where not(me-[l]->like) and l.preferred! = true
set l.preferred = false
// hack to always return a row (we don't even use the count)
with count(*)
// we have to re-start the query, since the with wiped out our previous
named vars
start me=node(1), like=node(3)
match me-[l:likes]->like
set l.preferred=true
return l,me, like;

Wes


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wujek Srujek  
View profile  
 More options Oct 18 2012, 2:12 pm
From: Wujek Srujek <wujek.sru...@gmail.com>
Date: Thu, 18 Oct 2012 20:12:50 +0200
Local: Thurs, Oct 18 2012 2:12 pm
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things in a single query

I start the transaction myself before the first query, and finish it after
the second. As far as I know, cyhper will start own transactions for write
queries if there is no active transaction - so in this case the transaction
penalty should not exist, I think?
Will try your approach as soon as I can.
wujek


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »