Finding all Stories tagged with combinations of Tags my users are interested in, while using non-trivial boolean logic to find the matches

Consider a PostgreSQL database containing

(1) N groups of Tags

Group 1: Adults, Kids
Group 2: Sci-fi, Fantasy, Drama
Group 3: Human, Alien
Group 3: Digital, Paperback
...

(2) N different Stories with various combinations of those Tags assigned – describing what the Story is about:

Story 1: Adults, Kids, Fantasy, Drama, Human, Digital, Paperback,
Story 2: Adults, Kids, Sci-Fi, Digital
Story 3: Digital, Adults, Human
...

And (3) N different people again with various combinations of Tags assigned – describing what the Person is interested in:

Person 1: Adults, Kids, Sci-Fi, Fantasy, Drama, Human, Alien, Digital
Person 2: Kids, Sci-fi, Fantasy
Person 3: Sci-Fi, Digital, Paperback

Neither side always uses Tags from all groups – sometimes a group is represented by multiple tags (Person 1: Adults AND Kids). Sometimes, one or more groups are not used at all (Person 3 not using Groups 1, 2 and 3; Story 3 not using Group 2)

I’m trying to load a list of all Stories related to People through their mutual Tag combination where:

  1. the Person.tags side is the authoritative: if a selected tag doesn’t exist in Story.tags, the Story is deemed unrelated, and Tags on Story.tags side that don’t exist in Person.tags are ignored
  2. [the tricky bit that gets me]: At least one Tag from each Group on the Person.tags must be present in Story.tags, but not all

In other words, I need the following logic to be applied:

Person 1: (Adults OR Kids) AND (Sci-Fi OR Fantasy OR Drama) AND (Human OR Alien) AND (Digital), matching Story 1
Person 2: (Kids) AND (Sci-fi OR Fantasy), matching Story 1 and 2
Person 3: (Sci-Fi) AND (Digital OR Paperback), matching Story 2 and 3

I’ve tried a few scenarios, but the best I could come up was using the intersection of Tags on both sides to drive the match – which was incorrect because for instance Person 3 was matched with Story 1 because of the “Digital” Tag present in Story 1, but Person 3 also needs the “Sci-Fi” tag present, which isn’t the case of Story 1, hence Story 1 should have been not linked with Person 3.

I’m able to construct a correct query for a single person in my application, but only after reading the Person.array to learn what Tags are there, hence how does the WHERE statement look like for that specific person. I can’t do that when I want to find matches of multiple people with multiple stories in a single query.


I think there could be some way along the lines of CROSS JOINing all of the possible combinations of the Tags all Persons are interested in, then joining it with a flattened list of Story Tags:

For person 1, that means:

Adults, Scifi, Human, Digital
Adults, Fantasy, Human, Digital
Adults, Drama, Human, Digital
Adults, Scifi, Alien, Digital
Adults, Fantasy, Alien, Digital
Adults, Drama, Alien, Digital
Kids, Scifi, Human, Digital
Kids, Fantasy, Human, Digital
Kids, Drama, Human, Digital
Kids, Scifi, Alien, Digital
Kids, Fantasy, Alien, Digital
Kids, Drama, Alien, Digital

Note how the Tag Groups are important – it’s not just a cross join of all Tags, there is no Adults, Kids, Scifi, Fantasy, Drama, Human, Alien combination, for example.

For Person 3, that means:

Sci-Fi, Digital
Sci-Fi, Paperback

I could then inner join the combinations of Person.tags with the Story.tags, only accepting Story records where all Tags on the Person side are found in the list of Tags on the Story side. Some Stories could be linked to one person multiple times through different Tag combinations, but I could just select DISTINCT Stories to get around that.

My guts tell me https://www.postgresql.org/docs/current/tutorial-window.html might be the answer, but it’s way outside of what I comfortably know today.

Am I at least going in the right direction? Any guidance would be greatly appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật