Working with Neo4j with Django neomodel library with Dara Silvera
Transcript
By Dara Silvera. From DjangoCon US 2023. Published December 14, 2023.
Watch the video Markdown version 2,060 words · auto-generated · en
Summary
Graph databases are useful when the important part of the data is how entities connect, especially for complex relationships, hierarchies, evolving schemas, recommendations, fraud detection, and real-time analysis. Dara Silvera explains Neo4j’s property-graph model, Cypher pattern-matching queries, variable-length path searches, and algorithms for centrality, community detection, and path finding. She then shows how neomodel brings Django-style model definitions and query methods to Neo4j, using a Panama Papers-style data model and examples that combine the neomodel API with raw Cypher. The central argument is that Neo4j makes relationship-heavy data easier to explore and can reveal connections that would be difficult to find with relational queries alone.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction
Well, hello everybody. And so today is my first time in the Shangokon. So this talk is about NeoProche with Django and Neo Model Library. So let's start. So it's easy to come across with use cases where it could be useful to use graph database. And we don't realize since we are used to other types of database. The idea of this talk is to present some examples of when to make sense to use to use it and incorporate it with our Django backend using Neo Model Library.
So first of all, I'm going to talk about me. My name is Dara Silvera. I'm a full stack developer at Octobot. which is a software company with nine years of experience. I'm from Montevideo, Uruguay. It's a small country in South America between Brazil and Argentina. I'm a huge fan of Formula One. I love rabbits traveling. Oh yes, we have fun of Formula One here. And I'm kind of a makeup artist. And this is my QR of LinkedIn if you want to connect. So we have SQL database and no SQL database listed here.
1:56 Graph Database Fundamentals
When you have a problem, your mind is just trying to go to a relational database. Even though they are called relational, they don't handle relationships very well. For no SQL we have three we have four no SQL Database listed here, three of them are don't have the concept of relationship, but each one is created to resolve a particular problem. Then we have the graph database and there we then we have Neil4. This captures the relationship as part of the data
Sorry. Sorry for that. Okay, so Neo4chet as an FD Graph database. The connections are already there, stored right in the database. Native technologies tends to perform faster, scale bigger and run more efficiently, calling for a much lower hardware requirement. This capability to transverse data without the index lookup makes it more efficient. So what that looks like. The data model for Net4G is something called property graph. Apart from defining the nodes that indicates are person and car. and we have to define some attributes that
identify them. These attributes can be a string, a Boolean or a numeric. And for relationships we have to define exactly one type and one property that in this case we have loves and owns, for example, and we can have zero or more properties like for example the model of the car the sorry signs and the date of when it drives okay so When to use Graph database. First of all, some complex data relationships Some databases have relationships that are complex by the very nature.
4:20 Graph Database Use Cases
An example of this is the is the Paras paper. It was an investigative journalism effort that exposed people who were using offshore financial instruments and accounts to hire different companies. So the people who were using this mechanism were trying to hide their wealth. Another example could be highly connected data where the grade of connected is very large. hierarchical data when for example we have a self-foreign key and it could be difficult if we try to do a lot of joins Then involving schema, if our data structures is not well defined yet
and it will may suffer some changes in the future. quite interesting for that because it's not so fixed. And then real-time insights. An example of this could be a recommendation system or prevention fraud payments. About complex data relationships, I'm gonna bring an example of this later. And for hierarchical data I have I bring a small example. So one powerful feature of Neil4 Shane is the ability to query for paths of arbitration
5:50 Variable-Length Paths
length. This allows us to find a connection between nodes that we don't know what the con and that they were there. or even without knowing the length of these connections. So for example, if we have this database when the labels are employee and the relationships are manage and we wanted to know other people that Shane managed, we can know this with this simple query without adding the maximum length of the path. So the result of the query it will be it will be the same graph as here, but if we put for example
6:36 Graph Algorithms
a three It will be from Shane to Shane because it will be three relationships between them There are a lot of grass algorithms that we can use in Al-4G. I just bring three. The first one is centrality, that is used to determine the importance of distinct nodes in a network. Then we have community detection that are used to evaluate how groups of notes are cluster or partition it. Then we have path finding. They could be find the path between two or more nodes or evaluate if a path exists. And of course we can use more of
7:23 Cypher Query Language
one of these algorithms together, so that's kind of interesting and give us more insights about our database. So let's talk about Cypher. Cipher is a high-level query language based on pattern matching It 's used to create nodes, update, and the date information and query graph. So for example for nodes we have the variable, the variable, the variable between parentheses and then we have to define the labels to identify them. And optionally we can add the properties. And then for edges, the edges have to be between brackets. in this example and between two nodes
and for example here in between n and b and we have to of course add the direction between the this nodes And optionally we can add the properties to identifying this relationship too. So there are a lot of clauses in Cypher, but I'll bring some examples of them. For example, match is to specify the pattern to search for in the database The clauses were is to add contrast to the patterns. Otherwise is to specify that the output should be sorted, could be ascending or descending. Return defines what to include in the query result and limit is to constrain the number of rows in the output.
So it's readable and I think that if you read it it's you can understand the query. That's I think that's fantastic. And here it's a small example that we want to know all the people that then loves so in this example this is a variable that can be for example come he can love cards or whatever the labels can be in this graph and we will return all the and will return all the per the per the labels that he loves Well, so let's talk about
9:44 NeoModel Data Modeling
new model. What if we like our existing stack, but still want to benefit of a graph database? NoModel is an object graph maper for Ne4Shape graph database. It's familiar with Django model style definitions. So here is the shape of the data of Parada 's paper. So first we have the entity That is the offshore legal entity, this could be a company or a trust that was created in a low task jurisdiction Then we have an officer that's a person or a company who plays a role in an offshare entity, such a beneficiary or a shareholder. And then we have the intermediary
that is a go between an offshore corporation and an offshore Sorry, an offshore corporation and a person that wants it. And then we have address that are all the address that was found in the IC IG database and others are other entities that plays a role in this database. So these are all the possible relationships, but this I only used a pair of them. So for example, for entity, we have like
the relationships that this can be defined using relationship from, relationship to, or relationships. So as I said before, it's quite similar to how we work in Python. I mean nodes are defined in the same way. and classes are defined in Python with the only difference that we must define as neomodal property objects. And we have to also define the what is which is gonna be the ID. And this example is not ID And we just write unique Idrib property. And this is the same but for officer. So if we see here
for entities we have a relationship to And this is the name of the relationship. Here we import the model. So we are an officer. We have entities and relationship to. But if we go back from to entity, we have officer that has a relationship from and it's the same name of relationship. So here we have from officer to entity the relationship From entity and of course for every model we have to serialize the connections and this is how we serialize them, the relationships And this
12:45 Shortest-Path Queries
a house this is how you and the database looks like This is a query made with cipher. For example, if we want to discover the shortest path between two officers. This is how can we do it? Here we have the name that contains Ross Junior. This is the null. And the other one is a contains grant. You have to believe me, this contains grants. And this is how we use all shortest paths. And this will give you all the shortest paths navigating with
13:31 NeoModel Query API
these relationships and maximum length of 10 And then we return it. So it's quite interesting how we can it visualize the data. And Neo model contains an IP an API for querying sets of nodes without having to write cipher. So for example we have filter and the filter method borrows the same shango filter format. For example, less than, greater than Then we have get as the same as Django, has, that is to know if a node has a relationship or not, ordered by
to order by the query. And then if we want to query a more complex filter, we can import Q from NeoModel. And this could be an example of that, how to use it. And if we want to still use cipher, we can we have to import DV from the model and we have to write the query. This is the almost the same query between if we're doing it with the APR or we are doing with cipher. And this is a more complex query that's the same that I showed you before.
I sh I brings just a part of the result. I think that's about neo model is that The query doesn't bring us all the nodes that are between these two nodes. Just bring us the first. The start node, so in this case is Ross Junior and then the end of the note that in this case is Bloomberg Anthony Grant And the size is the size between this path. So in this example is 6 So I think that's it's a work in progress traversing data and traversing the graph with new
15:49 Conclusions
model And a conclusion well, there are a lot of conclusions. First of all, graph database are the worst way, are the best way to explore the relationships between entities It's much more intuitive to use this for this purpose than SQL database. And a person that was working with the Paradise papers said that if we were work if we were looking at the documents and we will never find out all these complex the rel relationships. So and Neo4Shape help for that. And of course the data visualization, I think that's great and that you can see
how your query is visualize. Of course different insights and as I say before if we use this If we use centrality or if we use pathfinding or of that, it can be give us more powerful insights of our graph database. So I think that's all. I have to say thank you for hearing me and being today here. So and the questions will be addressed in the hallway. Thanks.