cypher query connundrum

69 views
Skip to first unread message

Eugen Paraschiv

unread,
Apr 14, 2012, 7:20:32 AM4/14/12
to ne...@googlegroups.com
Hi,
I'm working with Spring Data Neo4J, Neo4j 1.6.1 and the following model:
@Indexed(indexName = "locationsindex")
public class Location {
    @GraphId
    private Long id;

    private float latitude;
    private float longitude;
    ...
}
and I'm trying to write a Cypher query like:
@Query("...")
Location findByLatAndLong(final float latitude, final float longitude);
I have tried several options and nothing seems to work; I'm now thinking that perhaps I should select the node by an index query (because I need to check both latitude and longitude).
Any help is appreciated.
Thanks.
Eugen.

Peter Neubauer

unread,
Apr 14, 2012, 10:41:24 AM4/14/12
to ne...@googlegroups.com

Yes,
That sounds like a plan. Have you checked out the spatial support, with a Well Known Text property to hold the points?

Michael Hunger

unread,
Apr 14, 2012, 10:46:55 AM4/14/12
to ne...@googlegroups.com

Eugen Paraschiv

unread,
Apr 15, 2012, 6:21:51 AM4/15/12
to ne...@googlegroups.com
Sorry, I shouldn't have used Location(lat,lng) necessarily - it can be any other entity with two floats (I'm not using the spring data geo support at the moment). So essentially it's not related to geospatial stuff - just a general entity and an general query retrieving it by 2 fields.
I'm not exactly sure about the documentation helping all that much on this - what exactly is the example query I should be looking at?
Thanks.
Eugen.

Jacob Hansson

unread,
Apr 16, 2012, 3:09:10 AM4/16/12
to ne...@googlegroups.com

Could you include an example of a query you have tried, and the result you expected?

Like you mention, you need an index lookup (or a node id) for Cypher to start from. It currently does not let you look globally for unindexed properties.

I'm not super familiar with the spring integration.. does the @indexed in the beginning of the entity mean all attributes are indexed?

If so, you should be able to do something like:

START nodes=node:locationsIndex(latitude=100, longitude=200)
RETURN nodes

Sent from my phone, please excuse typos and brievety.

Michael Hunger

unread,
Apr 16, 2012, 3:36:54 AM4/16/12
to ne...@googlegroups.com
Eugen,

you should always be able to do something like:

Location findByLatitudeAndLongitude(final float latitude, final float longitude);

Which auto-generates the query you need. (It uses the where-clause to filter for the entities).

You might index the properties (at least one, there is still a bug with indexing two) then the query would use that to look up.

Michael

Eugen Paraschiv

unread,
Apr 17, 2012, 4:36:17 PM4/17/12
to ne...@googlegroups.com
Right, I had some trouble with the autogenerated queries (posted about it a while back) so I didn't even try - it works well, thanks. Is there a way to see the exact query that gets generated?
On the query you suggested, that's fine if you have the values (100 and 200) but if you have arguments:
Location findByLatAndLong(final float latitude, final float longitude);
then using {0} and {1} INSIDE the query:
@Query("START location=node:locationsindex(\"latitude:{0} AND longitude:{1}\") RETURN location")
doesn't work.
Thanks.
Eugen.

Michael Hunger

unread,
Apr 17, 2012, 4:54:35 PM4/17/12
to ne...@googlegroups.com
Eugen, you will have to construct the lucene index query outside and pass it in as an parameter:

String locationQuery = String.format("latitude:%d AND longitude:%d",lat,lon);

@Query("START location=node:locationsindex({0}) RETURN location")
findByLocationQuery(String locationQuery)

HTH

Michael

Eugen Paraschiv

unread,
Apr 18, 2012, 4:26:05 AM4/18/12
to ne...@googlegroups.com
Yeah, I thought about doing that - I just wanted to make sure there's no other way.
Thanks for all the feedback and help.
Eugen.
Reply all
Reply to author
Forward
0 new messages