Neo4j:有没有一种快速的方法来获得两个节点之间的距离?

时间:2016-10-17 14:44:04

标签: neo4j

我在链中有节点,如下所示:

func saveNoteButtonTapped(sender: UIButton) {
   guard let selectedCellRow = rowOfLongPressedItem else {
       return // rowOfLongPressedItem was nil
   }

   let realm = try! Realm()
   let itemsList = realm2.objects(ItemsList)

   var selectedItem = ItemsList[selectedCellRow]

   try! realm.write {
      selectedItem.counts += 1

      let noteTimePair = noteAndTimeEntryObj()

      noteTimePair.timeStampOfNote = NSDate()
      noteTimePair.note = noteTextField.text
      noteTimePair.itemsList = selectedItem
      realm.add(noteTimePair)
   }

   notePopup.hidden = true
   notePopup.endEditing(true)
   noteTextField.text = ""
}
  • 每个节点都有一个唯一索引。
  • 这个链中有几十万个节点。

是否有快速计算(a)<-[:rel]-(b)<-[:rel]-(c) ... (x) (a)之间的节点(或关系)数量,即使它们之间有数千个节点?到目前为止(x)已经慢了#34;

注意:我不需要知道其间的节点,我只是想找到距离。

1 个答案:

答案 0 :(得分:2)

使用shortestPath方法:

MATCH (martin:Person { name:"Martin Sheen" }),(oliver:Person { name:"Oliver Stone" }), 
p = shortestPath((martin)-[*..15]-(oliver))
RETURN length(p)

https://neo4j.com/docs/developer-manual/current/cypher/#query-shortest-path