From the course: Vector Databases in Practice: Deep Dive
Establishing relationships with references
- [Instructor] So far, you've seen how to define our movie data collection as well as how to populate it, but this didn't include a common aspect of data sets, which is relationships. In real life, data objects are often related to each other in some way. A product might belong to a company, a book has an author, and a restaurant or a movie might have reviews. So how do we model these relationships? Well, that's where references, also called cross references, come in. In Weaviate, references are created by defining them between collections. In our data set, we happen to have reviews for our movies. So let's model this relationship between these two collections. Each movie can have one or more reviews, of course, and when we find the movie, we would like our users to be able to see what reviews are associated with each one. We can do this by creating a reference property called hasReview that'll point each movie to its corresponding reviews. So far, we've defined the movie collection only. To implement our relationships, we'll need to add the collection definition for reviews before adding references to them from movies. This will let us retrieve reviews that are associated with each movie without directly searching for any reviews. To make this more concrete, let's have a look at some code examples. Here's one that prepared earlier, modifying our collection creation script. We'll just start by deleting our previously created data and start again. This time through, we'll add a review collection first. It's a pretty simple collection definition with the same vectorizer and generative module configurations and just the one property for the review body. And then we come to our movie collection. Now this collection definition is identical to what we defined before, but this time we'll make a modification to specify the reference or the relationship between movies and reviews. And we'll do that by specifying a value for the reference parameter. We'll define a reference property instance with a name hasReview. And all we need to do then is to point it to the review collection by providing the name of the collection, which I've done here by reference. And that's it. If you run this code, it'll create those two collections in your database. Data import code here has been updated in the same way as well. Now instead of importing data for the one collection, we'll import data for two collections, but the import process largely remains the same. For each collection, we iterate through the movie data and import them in sets. This goes for reviews, as you see in this code block. This iteration is just so that it happens that there are three reviews here for each movie in our data set, and they're called Critic Review 1, 2, or 3. And just as before, we can add the objects by using the insert_many method. And again, the movie import process has remained largely the same. But interestingly, you'll notice that we here loop through the review data while importing movie objects. So why might we do that? Well, that's so that we can add them as references to each movie. What we're doing here is to generate IDs or reviews in the same way as we did further above while importing the reviews. And then we create the references here to each review from the movies so that the relationships between the movies and the reviews are created when we create the movie objects. If we run the script, we'll have a full data set again, but this time with a set of reviews as well as movies that refer to them. So that's a quick tour of references. This is a more advanced topic for sure, but once again, the key thing to remember is not the syntax, but the fact that you can model relationship between data sets using references. Well done. You've built quite a fully featured database with multiple collections and references already. I think we've covered quite a lot of ground. So let's catch our breath with a very brief recap.
Contents
-
-
-
-
Create your own database3m 33s
-
Work with Weaviate3m 11s
-
Create an object collection3m 39s
-
Basic data import in Weaviate3m 51s
-
Establishing relationships with references4m 25s
-
Recap: Building a vector database2m 40s
-
Challenge: Add another object collection2m 14s
-
Solution: Add another object collection5m 4s
-
-
-
-