Skip to content

Instantly share code, notes, and snippets.

@BryantAvey
Last active May 14, 2021 17:23
Show Gist options
  • Save BryantAvey/af1bb0cb2542c7829dab947e2dc97daa to your computer and use it in GitHub Desktop.
Save BryantAvey/af1bb0cb2542c7829dab947e2dc97daa to your computer and use it in GitHub Desktop.
Neo4j Cypher - Generate Blog Article and Category with Relationships From any Blog Feed
CALL apoc.load.xml("https://www.starwars.com/news/feed", "rss/channel/item")
YIELD value
WITH [child in value._children WHERE child._type = "title" | child._text][0] AS title,
[child in value._children WHERE child._type = "link" | child._text][0] AS guid,
apoc.text.regreplace(
[child in value._children WHERE child._type = "description" | child._text][0],
'<[^>]*>',
' '
) AS body,
[child in value._children WHERE child._type = "category" | child._text] AS categories
MERGE (a:Article {id: guid})
SET a.body = body, a.title = title, a.blog = "StarWars.com"
WITH a, categories
CALL {
WITH a, categories
UNWIND categories AS category
MERGE (c:Category {name: category})
MERGE (a)-[:IN_CATEGORY]->(c)
RETURN count(*) AS categoryCount
}
RETURN a.id AS articleId, a.title AS title, categoryCount;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment