更新到:将节点添加到 Neo4j 空间索引 [英] Update to: Adding node to Neo4j Spatial Index

查看:21
本文介绍了更新到:将节点添加到 Neo4j 空间索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以提供一些有关将节点添加到 Spatial 的最新说明.我能找到的最好的说明是:

I'm hoping someone can provide some updated clarification on adding nodes to Spatial. The best instructions I can find is:

Neo4j Spatial 'WithinDistance' Cypher 查询REST 调用返回数据时返回空

然而,它已经快 2 年了,并且有一些自相矛盾的信息和一个公认的错误 (https://github.com/neo4j-contrib/spatial/issues/106),它似乎仍然是开放的.

However it's almost 2 years old and has some contradictory information with an acknowledged bug (https://github.com/neo4j-contrib/spatial/issues/106), which appears to still be open.

我也找到了这个教程:

http://mattbanderson.com/setting-up-the-neo4j-spatial-extension/

这意味着我们应该将节点添加到图层并将 Neo4j ID# 作为属性插入节点,但不要将节点插入几何索引.

Which says we should add the node to the layer AND insert the Neo4j ID# into the node as a property, but NOT insert the node to the geom index.

我的主要优先事项是我能够通过 Cypher(在浏览器中)进行查询,但我们最终也希望能够通过 REST 进行查询.因此,理想情况下,我希望以两种方式都可以插入节点.

My main priority here is that I be able to query via Cypher (within the browser) but we will eventually want to be able to query via REST as well. So, ideally i'd like to insert the nodes in such a way that we can do both.

所以,我的问题是:

1) 这里允许通过 REST 和 Cypher 进行查询的正确步骤是什么?

1) What are the correct steps here to allow querying via both REST and Cypher?

2) 如果我调用/addSimplePointLayer 然后调用/index/node 添加空间索引(通过 cURL 或 REST),我可以使用 LOAD CSV 插入节点并能够通过 REST 和 Cypher 查询空间插件吗?

2) If I call /addSimplePointLayer and then /index/node to add the Spatial index (both via cURL or REST), can I use LOAD CSV to insert nodes and be able to query Spatial Plugin via both REST and Cypher?

3) 如果我使用 REST 来插入我的节点,为了确保我可以通过 REST 和 Cypher(网络浏览器)进行查询,我需要进行哪些调用(以及调用顺序)?

3) If I am using REST to insert my nodes, what calls (and in what order) would I need to make in order to ensure that I can query via both REST and Cypher (web browser)?

谢谢,我期待着把这一切都搞定!!

Thanks, I'm looking forward to getting this all worked out!!

推荐答案

question 1

您首先需要初始化图层并使用 REST 调用创建空间索引:

question 1

You first need to initialize once a layer and create the spatial index using the REST calls:

POST /db/data/ext/SpatialPlugin/graphdb/addSimplePointLayer HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{ 
    "layer" : "geom", 
    "lat" : "lat", 
    "lon" : "lon" 
}

和:

POST /db/data/index/node/ HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{ 
    "name" : "geom", 
    "config" : { 
        "provider" : "spatial", 
        "geometry_type" : "point", 
        "lat" : "lat", 
        "lon" : "lon" 
    } 
}

使用 Cypher 通过事务端点创建具有 lon/lat 属性的节点:

Create a node with lon/lat properties using Cypher via the transactional endpoint:

POST /db/data/transaction/commit HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{
  "statements" : [ 
  {
    "statement" : "create (city:City {data}) return id(city)",
    "parameters" : {
      "data" : {
        "name" : "MyTown",
        "lon": 15.2,
        "lat": 60.1
      }
    }
  } 
  ]
}

并将其添加到空间索引中 - 确保采用节点 id 和从前一个请求返回的节点的 id:

and add it to the spatial index - be sure to adopt the node id with the id of the node returned from the previous request:

POST /db/data/ext/SpatialPlugin/graphdb/addNodeToLayer HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{ 
    "layer": "geom", 
    "node": "http://localhost:7474/db/data/node/<my_nodeid_goes_here>" 
}

为了使空间与 Cypher 一起玩得很好,需要一个 hack:每个地理索引节点都应该带有一个名为 id 的属性,其中包含节点 id 的值.这可以通过一个简单的 cypher 语句来完成(以下示例对所有 City 节点执行此操作):

In order to enable Spatial play nice with Cypher a hack is required: every geo-indexed node should carry a property called id with the value of the node id. This can be accomplished with a simple cypher statement (the example below does this for all City nodes):

POST /db/data/transaction/commit HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{ 
    "statements" : [ 
        { "statement" : "match (n:City) set n.id=id(n)" } 
    ] 
}

有了它,您就可以使用 Cypher 进行地理查询,例如:

With that in place you can use Cypher for geo queries, e.g.:

POST /db/data/transaction/commit HTTP/1.1
Host: localhost:7474
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache

{
  "statements" : [ 
  {
    "statement" : "start city = node:geom('withinDistance:[60.1,15.2, 100.0]') return city",
    "parameters" : {}
  } 
  ]
}

注意:对于withinDistance,您必须指定latlondistanceInKm.

NB: for withinDistance you have to specify lat,lon, distanceInKm.

如上所述,您需要有一个单独的 REST 调用来向空间索引添加节点.目前无法直接使用 Cypher.作为一种解决方法,使用 LOAD CSV 创建具有 lon/lat 属性的节点.在预处理步骤中,运行类似 MATCH (n:City) where not has(n.id) set n.id = id(n) return id(n) as id 的语句.这将设置 id 属性(上面描述的 hack)并返回新节点的 id 列表.对于它们中的每一个,发出 REST 调用以将节点添加到地理索引.

As described above you need to have a separate REST call for adding a node to the spatial index. This is currently not possible with Cypher directly. As a workaround use LOAD CSV to create the nodes with lon/lat properties. In a preprocessing step run a statement like MATCH (n:City) where not has(n.id) set n.id = id(n) return id(n) as id. This will set the id properties (the hack described above) and returns a list of ids of the new nodes. For each of them emit the REST call to add a node to the geo index.

上面已经回答了:-)

这篇关于更新到:将节点添加到 Neo4j 空间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆