Cypher finds (multiple) 'lowest' nodes

WBOY
Release: 2024-02-08 20:54:11
forward
644 people have browsed it

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)
Copy after login

Then my products have categorie_ids array like this:

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

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
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!