I'm using Neo4j to find Users who are in a 50 km radius and who are available on specific days.
This question is similar to this other question but Indexes have changed since Neo4J 2.0 so the solution does not work.
I use Neo4j 2.2.1, Neo4j-spatial 0.14 and py2neo / py2neo-spatial to interact with the graph.
To add user geometries to the graph I use:
def create_geo_node(graph, node, layer_name, name, latitude, longitude):
spatial = Spatial(graph)
layer = spatial.create_layer(layer_name)
node_id = node._id
shape = parse_lat_long(latitude, longitude)
spatial.create_geometry(geometry_name=name, wkt_string=shape.wkt, layer_name="Users", node_id=node_id)
..which creates the spatial nodes as wanted.
I then would like to query the graph by doing:
START user=node:Users('withinDistance:[4.8,45.8,100]')
MATCH period=(start_date:Date)-[:NEXT_DAY*]->(end_date:Date)
WHERE start_date.date="2014-03-03" AND end_date.date="2014-03-04"
UNWIND nodes(period) as nodes_in_period
OPTIONAL MATCH (nodes_in_period)<-[a:AVAILABLE]-(user:User)
RETURN user.uuid, count(a)/count(nodes(period))
but the query returns:
Index `Users` does not exist
Is seems that the py2neo spatial.create_layer(..) creates the layer but not the Index (but should it? ..as Indexes are now a "Legacy" of Neo4j 1.*)
Using py2neo spatial find_within_distance works but since it uses the REST api I cannot make mixed requests which take into account other parameters
From what I understand, START is deprecated since Neo4j 2.0 but I am unable to find the correct Cypher query for withinDistance in Neo4j 2.2
Thank you in advance,
Benjamin
create_layer
creates a "spatial" index which is different to Neo's Indexes. It actually creates a graph which models some bounding boxes for you so that you can then carry out spatial queries over your data. You don't need to reference this index directly. Think of it more like a GIS layer.You can inspect your graph and dig out the Node attributes you need to write your own cypher query.
But you could also use the py2neo spatial API
find_within_distance
http://py2neo.org/2.0/ext/spatial.html#py2neo.ext.spatial.plugin.Spatial.find_within_distanceHope this helps.