2

I am confused about the relationship between Layers and Indexes in neo4j spatial. In particular I have the following three questions: (I can give code samples on request, but its a lot of code and not really germane to the issue).

1) Its perfectly possible to use neo4j spatial without ever explicitly creating any layers, if you add an index with a spatial index provider. However, does this mean that it is implicitly creating a layer, and that I can access that layer directly through the Java API?

2) Suppose I create a Layer. Is it possible to do Cypher Queries on this layer without explicitly creating an index as well? It doesn't seem right to have to add the same node to both a Layer and a Spatial index, yet as far as I have discovered, you can only use Cypher with spatial if you explicitly create an index.

3) I have been using SpatialIndexProvider.SIMPLE_WKT_CONFIG as my index provider, however this means that I must make a property wkt and give inputs POINT(X Y), I would like to be able to tell my encoder to use, say, two properties Longitude and Lattitude. Is this possible? It seems to be possible with the Layers, but not so much with the indexes.

phil_20686
  • 4,000
  • 21
  • 38

1 Answers1

3

1) Adding an index with a spatial index provider does, in fact create a layer. You can see this by starting with an empty database and adding a spatial index, and then looking at the nodes that were created. This set of related nodes is exactly what you will find is produced if you create a layer directly using Java or REST.

If you list the indexes created, you will find that two indexes are created. One has the name that you provided, and one starts with your name followed by a terribly long string that I assume is meant to make it unique (there may be some other purposes unknown to me).

2) You can't do Cypher queries without an index. But as it turns out, the index is actually just an entry point into Neo4j Spatial, and you don't actually have to add your nodes to the index. You should either add your nodes to the index or add your nodes to the layer. Don't do both. If you choose to add your nodes to the layer and not the index there is further step you must take before Cypher queries will work. (See my answer to this other question for details.)

3) It's entirely possible to create an index and a layer that uses the SimplePointEncoder. The REST call to do this is

POST http://localhost:7474/db/data/index/node {"name":"test", "config":{"provider":"spatial", "geometry_type":"point", "lat":"lat", "lon":"lon"}}

You then make nodes with properties lat and lon, add them to your index or layer, and everything works just fine.

Community
  • 1
  • 1
Jim Biard
  • 2,252
  • 11
  • 14
  • Can I add a node to a spatial index using a cypher command? Is that possible? – phil_20686 Jul 14 '14 at 09:20
  • 1
    No. A spatial index is an entry point "stub", and does not function like a regular index. In fact, you are likely better off if you don't add your node to a spatial index, since using the REST command to do so creates a copy node and puts that into the RTree rather than making direct use of the node that you specify. – Jim Biard Jul 14 '14 at 13:36
  • If I add a node to a point layer as you suggest, how do I update it? There doesn't appear to be an API call to update the location of the node. Just changing the lon/lat coordinate of the node doesn't appear to update the bbox of the node or the overlay layer. I've no idea what that means for the spatial index behaviour. Do you? – Dr Joe Nov 18 '14 at 20:36
  • 1
    Good question! In the REST API, there is an updateGeometryFromWKT method that you can use to update the geometry for a node. In the java API, the EditableLayer class (and it's children) has an update method that does the same thing. – Jim Biard Nov 19 '14 at 16:47