How to find specific patterns in a graph

Hello,
I am starting to learn Graphs and neo4j.

I extract some informations and I want to query a specific pattern in my DB and find the "best" one.

Nodes :

  • Pages
  • Keywords

Edge :

  • LINK between pages and keywords.

LINK as a rank_id ,e.g. for specific keywords my page are ranked.

How can I extract the most powerfull page for a page with it's related pages ?
(k:Keyword {"kw": "mykeyword"})-[l:LINK]->(p:Page)-... ?

thanks for your help :slight_smile:

Not knowing your exact data model and requirements, I will give you an example that should spark some ideas. We can help further with more information about your model.

The following query will find the keyword node identified with its 'word' property equal to the string 'mykeyword'. It then finds all the related pages for the given keyword node. The returns the properties of each related page, order by the 'rank_id' property of the 'LINK' relationship.

Match (:Keyword {word:"mykeyword"})-[l:LINK]->(p:Page)
RETURN properties(p) as page
ORDER by l.rank_id DESC`

Thanks for your time.
I will try to explain my model (Is there any way to draw the "meta model" ?)

Here Serp = Keywords

This a part of my model :
Page -[IS_RANKED]->(Serp)

IS_RANKED has rank_id

Links between pages (internal linking)

So let's say I want to select Serp = "tv solaire" and I want to have all pages (Let's say p1) connected with LINKS and the "first layer" of pages linked to these pages.

Something like
(p2:Page)-[:LINKS]-(p1:Page) -[IS_RANKED]->(Serp)

It also depends on what output you want. This query gives you the first and second layer pages for a specific Serp node. I didn't know if you wanted the second layer directed or not, so I provide both. It depends on what you define as a 2nd layer page.

With a Direction for the second match:

match(p1:Page)-[IS_RANKED]->(s:Serp)
where s.keyword = "keyword_to_find"
with s, p1
match (p2:Page)-[:LINKS]->(p1)
return s, p1 as firstLayerPages, collect(p2) as secondLayerPages

Without a direction:

match(p1:Page)-[IS_RANKED]->(s:Serp)
where s.keyword = "keyword_to_find"
with s, p1
match (p2:Page)-[:LINKS]-(p1)
return s, p1 as firstLayerPages, collect(p2) as secondLayerPages

Great ! It is really nice :)

1 Like