by TheSpiciestDev · 4y ago · 0 comments
My example/requirement: I have a user wanting to find best-matching blog posts. Every post is tagged with a given category. There could be 100+ categories in the blog system and a blog post could be tagged with any number of these system categories. A user wants to see all posts tagged with "angular", "nestjs", "cypress" and "nx". The resulting list should return and be sorted by the best matches, to those of least relevance. So, posts that include all four tags should be up top and as the user browses down the results, there are posts with less matching tags.
What I've seen with SQL looks expensive, especially if you search with more and more tags. I may just not know what to search for though, re. SQL. Is there a query against a graph database that could accomplish this?
with tag_names := {"angular", "nestjs", "cypress", "nx"},
select BlogPost {
title,
tag_names := .tags.name,
match_count := count((select .tags filter .name in tag_names))
}
order by .match_count desc;
Which would give you a result like this: [
{
title: 'All the frameworks!',
tag_names: ['angular', 'nestjs', 'cypress', 'nx'],
match_count: 4,
},
{
title: 'Nest + Cypress',
tag_names: ['nestjs', 'cypress'],
match_count: 2,
},
{
title: 'NX is cool',
tag_names: ['nx'],
match_count: 1,
},
];So to only get blog posts with matching tags we would need to add a filter „match_count > 0“, right?
Update: I am very excited about EdgeDB :)
with tag_names := {"angular", "nestjs", "cypress", "nx"},
select BlogPost {
title,
tag_names := .tags.name,
match_count := count((select .tags filter .name in tag_names))
}
filter .match_count > 0
order by .match_count desc;SQL definitely must be replaced, but the silly pseudo English syntax is one of the things we want to get rid of, not retain.
Found this: https://github.com/edgedb/edgedb-rust
> Every post is tagged with a given category. There could be 100+ categories in the blog system and a blog post could be tagged with any number of these system categories.
My point is, I don’t see SQL query as expensive for this kind of use case. There are easy and native ways to do it.
In case you would like a top notch performance, Redis might be a way to do it. Even a reverse-index would achieve great performance.
+---------+-------+---------+-------+-----------+
| post_id | title | content | other | fields... |
+--------+------+
| tag_id | name |
+------------+---------+--------+
| tagging_id | post_id | tag_id |
But it seems like the core of the request is still something like: SELECT post_id, count(1) AS count
FROM taggings
WHERE tag_id IN (3, 8, 255)
GROUP BY post_id
ORDER BY count DESC
(off the top of my head; I haven't checked this for any kind of correctness)And I don't see why that query suffers as you add tags...?
------------
EDIT responding to below [HN believes I am a problem user who should only be allowed to make so many comments per day]:
< that is pretty much what I meant by “I see just one table” as you don’t need any joins
Well, assuming you're doing this because a user is interacting with your site via some kind of web interface, you can set the interface up to deliver you tag_id values directly, but you'll still need to do a join with the posts table so you can present a list of posts back to the user instead of a list of internal post_id values.
So I guess
SELECT t.post_id, count(1) AS count, p.title, p.url
FROM taggings t JOIN posts p ON t.post_id = p.post_id
...I just happen to have a very similar requirement to yours and was also wondering.