Home  >  Article  >  Backend Development  >  Cypher finds (multiple) "lowest" nodes

Cypher finds (multiple) "lowest" nodes

WBOY
WBOYforward
2024-02-08 20:54:11634browse

Cypher 查找(多个)“最低”节点

Question content

Case:

My Nodes Categories As shown below, I created the relationship from category to parent.

CREATE(p1:Categorie {
  id: 2,
  parent_id: 1,
  name: "Kids",
  is_active: true ,
  position: 1,
  level: 1,
})
CREATE(c1:Categorie {
  id: 5,
  parent_id: 2,
  name: "Toys",
  is_active: true ,
  position: 1,
  level: 2,
})

//New root category
CREATE(p2:Categorie {
  id: 9,
  parent_id: 1,
  name: "Holiday",
  is_active: true ,
  position: 1,
  level: 1,
})
CREATE(c2:Categorie {
  id: 12,
  parent_id: 9,
  name: "Water",
  is_active: true ,
  position: 1,
  level: 2,
})


CREATE(c1)-[:CHILD_OF]->(p1)
CREATE(c2)-[:CHILD_OF]->(p2)

Then my products have categorie_ids array like this:

<code>{
    "sku": "abc",
    "name": "Electric boot",
    "categorie_ids": [ 
        1,
        5,
        9,
        12
    ]
}
</code>

The API will return all ids where the product is located "in". All relationships are so confusing to me. This example is minimal, but in reality there may be up to 20 relationships per product.

Questions/Problems:

I want to create a relationship with the lowest node in the category. In this case, the product has two main categories and therefore two bottom categories. So I want to create relationship with id 12 and 5.

I just can't understand the password query. Can someone explain this?


Correct Answer


You are basically looking at the following query:

According to your model, you are considering adding a predicate, the category node does not have a relationship of type INCOMING CHILD_OF

WITH [1,5,9,12] AS categoryIds
MATCH (n:Categorie)
WHERE n.id IN categoryIds
AND NOT ()-[:CHILD_OF]->(n)
RETURN n.id

The above is the detailed content of Cypher finds (multiple) "lowest" nodes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete