We can use the cypher directive on query fields. So at the root level, one example that's might be nice is for full text index. So here we can create a full text index in the database that allows us to do fuzzy matching. So if we search for graph but we misspell it, we add this tilda, this is we've seen index, we've seen it as a text engine. This allows us to do fuzzy matching in the database.
Well, we can use that in a query field to search for book titles using this fuzzy matching full text index. So that when we're searching for books, even if we misspell it, we'll still get the results that we're looking for. Now we can also use this on mutation fields if we have some custom logic. We didn't really talk much about the mutations that are generated by the Neo4j GraphQL library. But if you want to create some specific mutations in addition to the ones generated for us, we can do that with cipher directives. For custom resolvers, basically the way this works is we add an ignore directive in our schema that tells the Neo4j GraphQL library that we are not going to try to fetch this data as part of the generated cipher query. You're basically saying that we are defining a resolver, we are responsible for fetching this data.
Okay, cool. Here is our last exercise. We have about 12 minutes left. So I think that should be just enough time. So, what we're going to do in this one, it says, use the article recommendation query from the first exercise to add a similar field on the article type. So we, because we had that issue with provisioning our oranges, we actually skipped the article recommendation query. That's okay, we'll do that. Basically what we wanted to do was write a cipher query that would recommend similar articles. So we'll do that, and then we'll take that cipher query and we're going to add that to a similar field on the article type. So now as we're resolving articles, we'll be able to see similar articles based on however we define that overlapping topics, overlapping organizations, we'll figure that out. But then we'll have that functionality, that custom logic in our GraphQL API, and then you'll need to update the articles React components to add that field to our data fetching and render that for the user. So for each article, you can see other recommended similar articles.
So let's start by going to Neo4j browser, do I have that open here? Here it is. And this is the news.graph.zone and username and password is just newsgraph, all lowercase, newsgraph, newsgraph, something like that. So how would we write a recommendation query? Well, let's start by just grabbing an article. Okay, so here's, we're saying match articles and then with allows us just kind of pipe things through or we can do aggregations or limiting, filtering. So we're gonna just spring through one article. So here's one article. And now I want to write a query to find similar articles. So if I'm reading this article, which, what is this about? Immunity cells in bone marrow, something about COVID, okay? So how would I find similar articles? Well, one way would be to look at the topics of this article. So what are the topics of this article? Let's make this a bit bigger. So coronavirus, vaccination, antibodies, immune system, science, bone marrow. We could also look at the organization, some Moderna, some NATURE article, and then a photo. Photo is not gonna be useful, photos are just specific for the article. So one way that I could define similarity would be, well, let's look at articles that are of similar topic. So you're reading this article about bone marrow, are there other articles about bone marrow? Oh, maybe you're all interested in this article about, what is this some disease that Colin Powell had that was in his bone marrow and had something to do with COVID. You may be interested in that. Well, we have a bunch of different topics though. So we don't wanna recommend just one, we wanna find the article that has the most number of overlapping topics and that's what we're gonna recommend. So how would we write that? Well, it looks something like this, match a and then traverse out has topic. And this is gonna be our topic node here. And then we wanna traverse out along the incoming has topic relationship to our recommended articles. So here's we basically follow this traversal from the article resolving to a topic, to another article that is the recommendation. But it's really like the candidate field for recommendations. So this can be kind of a two-step, one is get all of the candidate for recommendations, which are basically any articles with overlapping topics. And then we need to score those. So let's bring through the recommendations, and then we're going to do an aggregation. So we'll say count star as none. So what's going on here, Cypher has an implicit group by functionality built into it. If you're familiar with SQL and you have explicit group bys in SQL, where you can do aggregations grouped by some value, that's implicit in Cypher. So we use the with command, which we said is kind of like a pipe, allows us to do aggregations or limiting, ordering, those sorts of things. So here, this is kind of like saying, give me an implicit group by REC. So for each of these recommended articles, cause we're gonna have a whole bunch of these, right? Like we're gonna be a whole bunch of articles that have one or more topic nodes in common. I mean, if we double click on coronavirus, it was probably like 1000 or something, right? So we wanna score these by counting, we say count star, which is the number of paths that we've found for the specific article. So this is basically counting the number of overlapping topics from the article, the single article that we're resolving here to our candidate recommended article. So now num is the score essentially for our recommendations. So we can return the recommendations ordered by num in descending order and let's just like limited to 10. If we run that we get our 10 recommended articles. If we look at these, like at these in table view, so we have immune cells, COVID booster shots, scientific understanding variants. So these are all similar to the article that we're resolving, which was that something about bone marrow and vaccines. Okay, this looks pretty good. So let's copy this part of our query and go back to our code here. And I'm gonna go to my GraphQL API code and an article, I'm going to add, actually let's do this as a type extension. So I'm gonna say extend type article. This type extension just makes it easier to read so we don't have a bunch of fields cramped together. Typically you'd use this. If we had this part in another file, we can extend the types that exist in our schema. But I'm just gonna do it here for readability.
Comments