Message from discussion
Cypher questions - return updates count, update 2 things in a single query
Received: by 10.236.42.39 with SMTP id i27mr10720129yhb.50.1350583973448;
Thu, 18 Oct 2012 11:12:53 -0700 (PDT)
X-BeenThere: neo4j@googlegroups.com
Received: by 10.236.139.40 with SMTP id b28ls1229313yhj.3.gmail; Thu, 18 Oct
2012 11:12:51 -0700 (PDT)
Received: by 10.236.82.9 with SMTP id n9mr17681805yhe.24.1350583971423;
Thu, 18 Oct 2012 11:12:51 -0700 (PDT)
Received: by 10.236.82.9 with SMTP id n9mr17681804yhe.24.1350583971403;
Thu, 18 Oct 2012 11:12:51 -0700 (PDT)
Return-Path: <wujek.sru...@gmail.com>
Received: from mail-gg0-f173.google.com (mail-gg0-f173.google.com [209.85.161.173])
by gmr-mx.google.com with ESMTPS id u17si1133685ank.0.2012.10.18.11.12.50
(version=TLSv1/SSLv3 cipher=OTHER);
Thu, 18 Oct 2012 11:12:50 -0700 (PDT)
Received-SPF: pass (google.com: domain of wujek.sru...@gmail.com designates 209.85.161.173 as permitted sender) client-ip=209.85.161.173;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of wujek.sru...@gmail.com designates 209.85.161.173 as permitted sender) smtp.mail=wujek.sru...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-gg0-f173.google.com with SMTP id s5so2743771ggc.32
for <neo4j@googlegroups.com>; Thu, 18 Oct 2012 11:12:50 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=yt9t+kjS2DkOKMXkkqRPkeaLnWpkoAaV6RS03xVxFDQ=;
b=BkNW2NCOIBc0eMIeUfpttaOzwnRKq7bYyr9I9WbfQATp85pm/sIYa9G1sQUJL+Jw7s
i9NrZ2aA+jzZ5u903TbSuTVsF0qzJLtf3dWG+KDqWpUXEvEjPY6QplwZXKX9BZshy0sV
TFFIA6obnmryZTCzMXvmJ8h87mVSVnfvgru+feqig4nvASGtt0TckOvbdqTHCp/IdFoo
9Fm5EW0ocJeA6n+c0e9RFW1X3PqyEqBXNIelZWJG/usVIS29cuuUhBmUA6CMZMvPwBIa
Rkmfi20zrYTl73AGYRWJO3zr/aQKe3lI7Ioeuj/WpvVsqR9FFIZGOkph+nzh4QW1GrdV
+DcA==
MIME-Version: 1.0
Received: by 10.236.108.194 with SMTP id q42mr22128720yhg.3.1350583970207;
Thu, 18 Oct 2012 11:12:50 -0700 (PDT)
Received: by 10.101.3.6 with HTTP; Thu, 18 Oct 2012 11:12:50 -0700 (PDT)
In-Reply-To: <CAAOkgHOxzPQ3yW-QmqiOoF2_wsnO5YTxasanJDgp4CPB6MW...@mail.gmail.com>
References: <CAAuGTBi3pZ74PxhAeCsShw4WsehSwog84cPF4wWm4v3Zzta...@mail.gmail.com>
<CAP4mnvY-364N9Z8T8c5Na4_frSxgYTWaUQB-r48A3tmC8O=...@mail.gmail.com>
<86a8f9a6-5ac7-46d5-a705-53eb12debb6c@googlegroups.com>
<CAP4mnvYzzuvQJN4JBDqv5XVTF-qVTsqi=26TwgAv2FHrhvL...@mail.gmail.com>
<CAP4mnvbn+o9qQNLuoY6shKLJwK=UqKyQaRbsvYtAL8wrQDQ...@mail.gmail.com>
<6d72e08b-32ba-47f2-b538-3a6ce0856578@googlegroups.com>
<CAAOkgHOxzPQ3yW-QmqiOoF2_wsnO5YTxasanJDgp4CPB6MW...@mail.gmail.com>
Date: Thu, 18 Oct 2012 20:12:50 +0200
Message-ID: <CAAuGTBgetnug+Uwb3DtDNQM1cdH8a68UAJ6vp8zir-3na7-...@mail.gmail.com>
Subject: Re: [Neo4j] Cypher questions - return updates count, update 2 things
in a single query
From: Wujek Srujek <wujek.sru...@gmail.com>
To: neo4j@googlegroups.com
Content-Type: multipart/alternative; boundary=20cf30363d19c8970204cc59559c
--20cf30363d19c8970204cc59559c
Content-Type: text/plain; charset=ISO-8859-1
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
On Thu, Oct 18, 2012 at 7:41 PM, Wes Freeman <freeman....@gmail.com> wrote:
> 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
>
> On Thu, Oct 18, 2012 at 12:31 PM, wujek <wujek.sru...@gmail.com> wrote:
>
>> 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
>>
>>
>> On Wednesday, October 17, 2012 10:07:37 PM UTC+2, Abdul Azeez Shaik wrote:
>>
>>> 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 <azeezte...@gmail.com
>>> > wrote:
>>>
>>>> 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
>>>>
>>>>
>>>> On Thu, Oct 18, 2012 at 12:31 AM, wujek <wujek....@gmail.com> wrote:
>>>>
>>>>> 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
>>>>>
>>>>>
>>>>> On Monday, October 15, 2012 12:57:03 PM UTC+2, Abdul Azeez Shaik wrote:
>>>>>>
>>>>>> 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
>>>>>>
>>>>>>
>>>>>> --
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>> --
>>
>>
>>
>
> --
>
>
>
--20cf30363d19c8970204cc59559c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
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?<div>
Will try your approach as soon as I can.</div><div>wujek<br><br><div class=
=3D"gmail_quote">On Thu, Oct 18, 2012 at 7:41 PM, Wes Freeman <span dir=3D"=
ltr"><<a href=3D"mailto:freeman....@gmail.com" target=3D"_blank">freeman=
....@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">It is better to do it with one query, if pos=
sible, because it creates a sort of transaction (and is obviously more effi=
cient over REST especially, since it's one request instead of two).<div=
>
<br></div><div>I didn't have time to look at this before, but here goes=
a pretty hacky attempt:<br>
<div><br></div><div><div>start me=3Dnode(3), like=3Dnode(2)=A0</div><div>ma=
tch me-[l:likes]->()=A0</div><div>// this where is optional, but this wa=
y there's less modified</div><div>where not(me-[l]->like) and l.pref=
erred! =3D true=A0</div>
<div>set l.preferred =3D false=A0</div><div>// hack to always return a row =
(we don't even use the count)</div><div>with count(*)=A0</div><div>// w=
e have to re-start the query, since the with wiped out our previous named v=
ars</div>
<div>start me=3Dnode(1), like=3Dnode(3)=A0</div><div>match me-[l:likes]->=
;like=A0</div><div>set l.preferred=3Dtrue=A0</div><div>return l,me, like;=
=A0</div><span class=3D"HOEnZb"><font color=3D"#888888"><div><br></div><div=
>Wes</div></font></span><div>
<div class=3D"h5"><br><div class=3D"gmail_quote">On Thu, Oct 18, 2012 at 12=
:31 PM, wujek <span dir=3D"ltr"><<a href=3D"mailto:wujek.sru...@gmail.co=
m" target=3D"_blank">wujek.sru...@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi. I still can't get it to work.<div>Yo=
ur comment about getting rid of the r.preferred predicate as it is always f=
alse is wrong - it is false only the first time, when no preference is defi=
ned; after the initial preferred is set, setting the next one must unset th=
e 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.</div>
<div><br></div><div>The second option of inverting the query parts also doe=
sn't work. I also don't follow it logically - whereas the first par=
t sets the new preferred to true for a reference to the node I want, the se=
cond part will just reset it to false, as it does that for every relationsh=
ip of type LIKE, right?</div>
<div><br></div><div>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 eit=
her one or no preferred thing. If there is none, setting a new preferred wi=
ll just set it; if there is one already, setting another thing as preferred=
must unset the previously set one.</div>
<div><br></div><div>I am able to do it with 2 queries - first unset the pre=
vious preferred thingy, and then set the new one. It seems it is not possib=
le to do it in just one query - it's fine, I was just curious if this i=
s possible.</div>
<div><br></div><div>Thanks for your help, though.</div><div><br></div><div>=
wujek<div><br><br>On Wednesday, October 17, 2012 10:07:37 PM UTC+2, Abdul A=
zeez Shaik wrote:</div><blockquote class=3D"gmail_quote" style=3D"margin:0;=
margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>Also, you can do other way round,<br><br><div>start me =3D node({me}),=
like=3Dnode{{like}}=A0match me-[r:LIKES]->like set r.preferred =3D true=
</div><div>WITH me</div><div>match me-[r:LIKES]->t set r.preferred =3D f=
alse where r.preferred =3D true</div>
<div><br></div></div><div class=3D"gmail_quote"><div>On Thu, Oct 18, 2012 a=
t 1:33 AM, Abdul Azeez Shaik <span dir=3D"ltr"><<a>azeezte...@gmail.com<=
/a>></span> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
AFIK, WHERE predicate can be removed, as anyways r.preferred is false.<div>=
But, i don't know what would be the performance effect on that. May be =
Peter or Michael can answer this better.</div><div><br></div><div>BTW, what=
is your data size?</div>
<div><br></div><div>Thanks,</div></div><div>Abdul<div><div><div><br><div><b=
r><div class=3D"gmail_quote">On Thu, Oct 18, 2012 at 12:31 AM, wujek <span =
dir=3D"ltr"><<a>wujek....@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi. This:<div><div><div><br></div><div>start=
me =3D node({me}), like=3Dnode{{like}} match me-[r:LIKES]->t set r.pref=
erred =3D false where r.preferred =3D true</div>
</div><div>with me, like</div><div><div>match me-[r:LIKES]->t set r.pref=
erred =3D true</div></div></div><div><br></div><div>doesn't work for me=
in the following scenario: there is no preferred yet (all relations have p=
referred =3D false), so the first part doesn't update anything, and the=
n the second doesn't update anything as well.<div>
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 init=
ially, and this seems to prevent setting one.</div><div><br></div><div>
Am I doing something wrong?</div><span><font color=3D"#888888"><div><br></d=
iv></font></span><div><span><font color=3D"#888888">wujek</font></span><div=
><br><br>On Monday, October 15, 2012 12:57:03 PM UTC+2, Abdul Azeez Shaik w=
rote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;=
border-left:1px #ccc solid;padding-left:1ex">
Hi Wujek,<div><br></div><div>You can use WITH clause to have two sub-querie=
s in one query.</div><div>Your query would be,</div><div>start me =3D node(=
{me}), like=3Dnode{{like}} match me-[r:LIKES]->t set r.preferred =3D fal=
se where r.preferred =3D true</div>
<div>WITH me, like</div><div>match me-[r:LIKES]->t set r.preferred =3D t=
rue</div><div><br></div></blockquote><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex=
">
<div><br></div>
</blockquote></div></div></div>
<p></p>
-- <br>
=A0<br>
=A0<br>
</blockquote></div><br></div></div></div></div></div>
</blockquote></div><br>
</blockquote></div>
<p></p>
-- <br>
=A0<br>
=A0<br>
</blockquote></div><br></div></div></div></div>
<p></p>
-- <br>
=A0<br>
=A0<br>
</blockquote></div><br></div>
--20cf30363d19c8970204cc59559c--