Where filter on array property

1,313 views
Skip to first unread message

Alessandro Mazzotti

unread,
Jul 31, 2013, 6:42:34 AM7/31/13
to ne...@googlegroups.com
Hello,

I have nodes with a String array property and I want to query the nodes starting from an index on another property (it's a spatial index) and filter them by the elements inside the array property. An example could be

START n=node:location(..) WHERE n.array CONTAINS ["string1", "string2"] return n

Considering there's no CONTAINS keyword, is there a way to do this? 

Thanks

Alessandro

Michael Hunger

unread,
Jul 31, 2013, 7:16:54 AM7/31/13
to ne...@googlegroups.com
There is an IN operator.

START n=node:location(..) WHERE n.array IN ["string1", "string2"] return n


Cheers

Michael

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alessandro Mazzotti

unread,
Jul 31, 2013, 7:32:23 AM7/31/13
to ne...@googlegroups.com
Hi Michael,

thanks for your answer. I tried the IN operator before but seems not to work in this case or I'm missing something. Let's say I have a node with array = ["string1, "string2"]

START n=node:location(..) WHERE n.array IN ["string1", "string2"] return n 

returns nothing while

START n=node:location(..) WHERE n.array = ["string1", "string2"] return n

returns the node

What am I doing wrong?

Thanks

Alessandro

Michael Hunger

unread,
Jul 31, 2013, 8:41:45 AM7/31/13
to ne...@googlegroups.com
Oh, sorry my bad.
You want to do subarray matches ?

something like
WHERE ALL ( x IN ["string1", "string2"] WHERE x in n.array ) 

Michael

Alessandro Mazzotti

unread,
Jul 31, 2013, 10:08:56 AM7/31/13
to ne...@googlegroups.com
Thanks Michael,

perfect, these queries cover my use case

WHERE  ANY ( x IN ["string1", "string2"] WHERE x in n.array ) return n

WHERE ALL ( x IN ["string1", "string2"] WHERE x in n.array ) return n

Thanks

Alessandro

Michael Hunger

unread,
Jul 31, 2013, 10:50:10 AM7/31/13
to ne...@googlegroups.com
if your array only has one or two elements you might be faster of with an "string1" IN n.array OR "string2" IN n.array

and make sure to use parameters for the array / data you pass in

Michael

Alessandro Mazzotti

unread,
Jul 31, 2013, 11:12:51 AM7/31/13
to ne...@googlegroups.com
Thanks Michael,

I'm using the queries on repository methods with SDN, so it's something like this:

@Query(value = "start poi=node:locations({0}) WHERE ANY(x IN {1} WHERE x in poi.seasons) WITH poi, length (poi-[:UPVOTED]-()) as upvotes," + 
"length (poi-[:DOWNVOTED]-()) as downvotes  return poi ORDER BY (upvotes - downvotes) DESC", 
countQuery = "start poi=node:locations({0}) WHERE ANY(x IN {1} WHERE x in poi.seasons) return count(poi)")
Page<Poi> findInBoxPageableWithSeasons(String query, Season[] seasons, Pageable pageable);

where query is a box query [bbox[...]]

I'm making another post with some more questions that gives some context on this query thogh

Alessandro

Michael Hunger

unread,
Jul 31, 2013, 1:02:22 PM7/31/13
to ne...@googlegroups.com
Oh, cool, please blog about your use-case :)

Michael

Alessandro Mazzotti

unread,
Aug 1, 2013, 7:57:03 AM8/1/13
to ne...@googlegroups.com
Will do as soon as we have a blog :D

I made another post here, the title is "Using Spring Data Neo4j for a mobile app backend, some questions". Could you check it if you have time? I'm not super sure about some choices I made.

Thanks 

Alessandro
Reply all
Reply to author
Forward
0 new messages