Neo4j study with ChatGPT

docx

School

Concordia University Saint Paul *

*We aren’t endorsed by this school

Course

517

Subject

Computer Science

Date

Nov 24, 2024

Type

docx

Pages

24

Uploaded by Charitaa

Report
https://www.businessinsider.com/the-kennedy-political-dynasty-family-tree-infographic-2012-1 User Joseph P Kennedy was the son of Patrick Joseph Kennedy CREATE (joseph:Person {name: 'Joseph P Kennedy'}) CREATE (patrick:Person {name: 'Patrick Joseph Kennedy'})
CREATE (joseph)-[:FATHER_OF]->(patrick) User Rose Fitzgerald Kennedy was the daughter of John F Fitzgerald. Rose was the wife of Joseph P Kennedy. CREATE (rose:Person {name: 'Rose Fitzgerald Kennedy'}) CREATE (john:Person {name: 'John F Fitzgerald'}) CREATE (rose)-[:FATHER_OF]->(john) CREATE (rose)-[:WIFE_OF]->(joseph) Joseph P Kennedy and Rose Fitzgerald Kennedy had three sons: Ted Kennedy, John F Kennedy and Robert F Kennedy Let's incorporate the three sons of Joseph P Kennedy and Rose Fitzgerald Kennedy into the family tree: cypher Copy code CREATE (ted:Person {name: 'Ted Kennedy'}) CREATE (john_f:Person {name: 'John F Kennedy'}) CREATE (robert_f:Person {name: 'Robert F Kennedy'}) CREATE (joseph)-[:FATHER_OF]->(ted) CREATE (joseph)-[:FATHER_OF]->(john_f) CREATE (joseph)-[:FATHER_OF]->(robert_f) CREATE (rose)-[:MOTHER_OF]->(ted) CREATE (rose)-[:MOTHER_OF]->(john_f)
CREATE (rose)-[:MOTHER_OF]->(robert_f) Joseph P Kennedy and Rose Fitzgerald Kennedy had two daughters: Jean Kennedy and Eunice Shriver. Eunice Shriver was married to Sargent Shriver. CREATE (jean:Person {name: 'Jean Kennedy'}) CREATE (eunice:Person {name: 'Eunice Shriver'}) CREATE (sargent:Person {name: 'Sargent Shriver'}) CREATE (joseph)-[:FATHER_OF]->(jean) CREATE (joseph)-[:FATHER_OF]->(eunice) CREATE (rose)-[:MOTHER_OF]->(jean) CREATE (rose)-[:MOTHER_OF]->(eunice) CREATE (eunice)-[:WIFE_OF]->(sargent) Ted Kennedy had a son Patrick Joseph Kennedy II CREATE (patrick_ii:Person {name: 'Patrick Joseph Kennedy II'}) CREATE (ted)-[:FATHER_OF]->(patrick_ii) The daughter of John F Kennedy was Caroline Kennedy CREATE (caroline:Person {name: 'Caroline Kennedy'}) CREATE (john_f)-[:FATHER_OF]->(caroline) Eunice Shriver and Sargent Shriver had three children: Mark Kennedy Shriver, Robert Shriver, and Maria Shriver. Maria Shriver was married to Arnold Schwarzenegger. CREATE (mark:Person {name: 'Mark Kennedy Shriver'}) CREATE (robert:Person {name: 'Robert Shriver'}) CREATE (maria:Person {name: 'Maria Shriver'}) CREATE (arnold:Person {name: 'Arnold Schwarzenegger'})
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CREATE (eunice)-[:MOTHER_OF]->(mark) CREATE (eunice)-[:MOTHER_OF]->(robert) CREATE (eunice)-[:MOTHER_OF]->(maria) CREATE (sargent)-[:FATHER_OF]->(mark) CREATE (sargent)-[:FATHER_OF]->(robert) CREATE (sargent)-[:FATHER_OF]->(maria) CREATE (maria)-[:WIFE_OF]->(arnold) Robert F Kennedy had three children: Kathleen Kennedy, Joseph Patrick Kennedy II and Mary Kennedy. Mary Kennedy was married to Andrew Cuomo but then the got divorced. CREATE (kathleen:Person {name: 'Kathleen Kennedy'}) CREATE (joseph_ii:Person {name: 'Joseph Patrick Kennedy II'}) CREATE (mary:Person {name: 'Mary Kennedy'}) CREATE (andrew:Person {name: 'Andrew Cuomo'}) CREATE (robert_f)-[:FATHER_OF]->(kathleen) CREATE (robert_f)-[:FATHER_OF]->(joseph_ii) CREATE (robert_f)-[:FATHER_OF]->(mary) CREATE (mary)-[:WIFE_OF]->(andrew) CREATE (mary)-[:DIVORCED_FROM]->(andrew) User Joseph Patrick Kennedy III is the son of Joseph Patrick Kennedy II CREATE (joseph_iii:Person {name: 'Joseph Patrick Kennedy III'}) CREATE (joseph_ii)-[:FATHER_OF]->(joseph_iii)
Below is the code only. Without explanations. CREATE (joseph:Person {name: 'Joseph P Kennedy'}) CREATE (patrick:Person {name: 'Patrick Joseph Kennedy'}) CREATE (joseph)-[:FATHER_OF]->(patrick) CREATE (rose:Person {name: 'Rose Fitzgerald Kennedy'}) CREATE (john:Person {name: 'John F Fitzgerald'}) CREATE (rose)-[:FATHER_OF]->(john) CREATE (rose)-[:WIFE_OF]->(joseph) CREATE (ted:Person {name: 'Ted Kennedy'}) CREATE (john_f:Person {name: 'John F Kennedy'}) CREATE (robert_f:Person {name: 'Robert F Kennedy'}) CREATE (joseph)-[:FATHER_OF]->(ted) CREATE (joseph)-[:FATHER_OF]->(john_f) CREATE (joseph)-[:FATHER_OF]->(robert_f) CREATE (rose)-[:MOTHER_OF]->(ted) CREATE (rose)-[:MOTHER_OF]->(john_f) CREATE (rose)-[:MOTHER_OF]->(robert_f) CREATE (jean:Person {name: 'Jean Kennedy'}) CREATE (eunice:Person {name: 'Eunice Shriver'}) CREATE (sargent:Person {name: 'Sargent Shriver'}) CREATE (joseph)-[:FATHER_OF]->(jean) CREATE (joseph)-[:FATHER_OF]->(eunice) CREATE (rose)-[:MOTHER_OF]->(jean) CREATE (rose)-[:MOTHER_OF]->(eunice) CREATE (eunice)-[:WIFE_OF]->(sargent) CREATE (patrick_ii:Person {name: 'Patrick Joseph Kennedy II'}) CREATE (ted)-[:FATHER_OF]->(patrick_ii) CREATE (caroline:Person {name: 'Caroline Kennedy'})
CREATE (john_f)-[:FATHER_OF]->(caroline) CREATE (mark:Person {name: 'Mark Kennedy Shriver'}) CREATE (robert:Person {name: 'Robert Shriver'}) CREATE (maria:Person {name: 'Maria Shriver'}) CREATE (arnold:Person {name: 'Arnold Schwarzenegger'}) CREATE (eunice)-[:MOTHER_OF]->(mark) CREATE (eunice)-[:MOTHER_OF]->(robert) CREATE (eunice)-[:MOTHER_OF]->(maria) CREATE (sargent)-[:FATHER_OF]->(mark) CREATE (sargent)-[:FATHER_OF]->(robert) CREATE (sargent)-[:FATHER_OF]->(maria) CREATE (maria)-[:WIFE_OF]->(arnold) CREATE (kathleen:Person {name: 'Kathleen Kennedy'}) CREATE (joseph_ii:Person {name: 'Joseph Patrick Kennedy II'}) CREATE (mary:Person {name: 'Mary Kennedy'}) CREATE (andrew:Person {name: 'Andrew Cuomo'}) CREATE (robert_f)-[:FATHER_OF]->(kathleen) CREATE (robert_f)-[:FATHER_OF]->(joseph_ii) CREATE (robert_f)-[:FATHER_OF]->(mary) CREATE (mary)-[:WIFE_OF]->(andrew) CREATE (mary)-[:DIVORCED_FROM]->(andrew) CREATE (joseph_iii:Person {name: 'Joseph Patrick Kennedy III'}) CREATE (joseph_ii)-[:FATHER_OF]->(joseph_iii) Important Note: All these operations are copied and pasted together and executed with one single click. If the operations are executed separately then all sorts of ghost nodes will be created. Then the match/merge operations will be needed, or the “with nyc” style operations. Possibly with WHERE clauses and id(..)=
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Now look around in the user interface a bit!!!!!
Note that Mary Kennedy is both WIFE_OF and DIVORCED_FROM Andrew Cuomo. ========================= who is the grandfather of caroline kennedy? Caroline Kennedy's grandfather is Joseph P. Kennedy. Joseph P. Kennedy is the father of John F. Kennedy, making him Caroline Kennedy's paternal grandfather.
CORRECT Who are the sons of Robert F Kennedy Robert F. Kennedy had eleven children. Among his sons are: Joseph Patrick Kennedy II Robert F. Kennedy Jr. David A. Kennedy Note: I only entered Joseph Patrick Kennedy II. WIKIPEDIA confirms that he had 11 children! Note again that I did not enter Timothy. Note also that I never used the brother-of relationship.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
THIS IS NOT TRUE. I NEVER ENTERED David. A. Kennedy. I also did not enter Robert. F. Kennedy Jr. Notably, it correctly did NOT return the daughters that I entered. I did not provide gender information! We did enough CREATE.
Now let’s work on READ. Write CYPHER code that will return the grandfather of Kathleen Kennedy MATCH (kathleen:Person {name: 'Kathleen Kennedy'})-[:FATHER_OF]->()-[:FATHER_OF]- >(grandfather:Person) RETURN DISTINCT grandfather.name THIS DID NOT WORK. But ChatGPT warned me: This Cypher query will find the grandfather of Kathleen Kennedy by traversing two generations up through the "FATHER_OF" relationship. Adjust the relationship type and pattern if your data model has a different structure or relationship naming convention. It is objectively wrong. FATHER_OF points in the wrong direction. See here: I will now hand-fix this by reversing the arrow directions. MATCH (kathleen:Person {name: 'Kathleen Kennedy'})<-[:FATHER_OF]-()<-[:FATHER_OF]- (grandfather:Person) RETURN DISTINCT grandfather.name
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
This WORKS! Also slight variation: MATCH (kathleen:Person {name: 'Kathleen Kennedy'})<-[:FATHER_OF]-()<-[:FATHER_OF]- (grandfather:Person) RETURN DISTINCT grandfather (No Name at the end!) write cypher code to find the brothers of x MATCH (x)-[:FATHER_OF|:MOTHER_OF]->()<-[:FATHER_OF|:MOTHER_OF]- (brother:Person) WHERE x <> brother RETURN DISTINCT brother.name ChatGPT explanation: This Cypher code uses a pattern to find nodes connected to the same parent node as x through the relationships "FATHER_OF" or "MOTHER_OF", excluding the node x itself, and then returns the names of those nodes, representing the brothers of x. Adjust the relationship types and conditions based on the specific structure of your data model.
Note that it included sisters. But that is because we did not specify gender. Alternatively we could have specified DAUGHTER_OF instead of FATHER_OF in the opposite direction. That would have eliminated the problem. DELETE We need two kinds of deletes: Node deletes and link deletes We will not do Property Deletes here. Let’s start by deleting the WIFE_OF link. Remember they got divorced. write cypher code to delete the WIFE_OF link MATCH (:Person {name: 'Person_Name'})-[r:WIFE_OF]->() DELETE r There was only one WIFE_OF in my database. It does not remember that. It gives me generic delete code. So I will try again.
write cypher code to delete the WIFE_OF link of Mary Kennedy MATCH (:Person {name: 'Mary Kennedy'})-[r:WIFE_OF]->() DELETE r Let’s look at the database to make sure:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Actually we don’t need Andrew Cuomo at all. Let’s delete him. write code to delete Andrew Cuomo from the database MATCH (a:Person {name: 'Andrew Cuomo'}) DETACH DELETE a You cannot delete a node that is connected to anything.
Andrew Cuomo is still connected to Mary Kennedy. So we first have to DETACH him from every other node. Then we can delete him. Andrew Cuomo is gone. UPDATE We need at least two kinds of updates: Node Update and Link Update Also we might want to update properties of nodes and properties of links. But we did not create any link properties yet. Let’s correct the name of Andrew Cuomo. For this we have to put him back in the database first. Write code to insert Andrew Cuomo back in the database CREATE (:Person {name: 'Andrew Cuomo'}) I wrote this code myself:
Write code to change Andrew Cuomo's name to Andrew Mark Cuomo MATCH (a:Person {name: 'Andrew Cuomo'}) SET a.name = 'Andrew Mark Cuomo' Again, I wrote the display code myself.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Let’s changed MOTHER_OF to FEMALE_PARENT_OF This is clearly more difficult. Write code to change the relationship MOTHER_OF to FEMALE_PARENT_OF at all places // Create the new relationship FEMALE_PARENT_OF and delete the old MOTHER_OF relationship MATCH (:Person)-[oldRel:MOTHER_OF]->(:Person) CREATE (:Person)- [:FEMALE_PARENT_OF]->(:Person) DELETE oldRel And here ChatGPT screwed up A LOT.
write cypher code to delete the whole database MATCH (n) DETACH DELETE n Now I am recreating the whole database as it was at the beginning. Write code to change the MOTHER_OF link from Eunice Shriver to MARK KENNEDY SHIRIVER to a FEMALE_PARENT_OF link MATCH (eunice:Person {name: 'Eunice Shriver'})-[oldRel:MOTHER_OF]->(mark:Person {name: 'Mark Kennedy Shriver'}) CREATE (eunice)-[:FEMALE_PARENT_OF]->(mark) DELETE oldRel
The system claims it did it: BUT: There is no FEMALE_PARENT_OF here on the right:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Now the debugging starts. Metaphor: (And joke): Off-Road Vehicles. They get stuck where the tow truck cannot get them.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Can ChatGPT do better? I tell it: This did not work. MATCH (eunice:Person {name: 'Eunice Shriver'})-[oldRel:MOTHER_OF]->(mark:Person {name: 'Mark Kennedy Shriver'}) DELETE oldRel CREATE (eunice)-[:FEMALE_PARENT_OF]->(mark)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
This worked. Not sure why the first one did not work.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help