Use exclude subquery for recursive all tag search

This commit is contained in:
Cameron
2024-12-06 11:04:08 -05:00
parent ff13a57d0e
commit 4a91c6344a

View File

@@ -356,12 +356,17 @@ impl TagDao for SqliteTagDao {
) -> anyhow::Result<Vec<String>> { ) -> anyhow::Result<Vec<String>> {
use diesel::dsl::*; use diesel::dsl::*;
let exclude_subquery = tagged_photo::table
.filter(tagged_photo::tag_id.eq_any(exclude_tag_ids.clone()))
.select(tagged_photo::photo_name)
.into_boxed();
tagged_photo::table tagged_photo::table
.filter(tagged_photo::tag_id.eq_any(tag_ids.clone())) .filter(tagged_photo::tag_id.eq_any(tag_ids.clone()))
.filter(tagged_photo::tag_id.ne_all(exclude_tag_ids)) .filter(tagged_photo::photo_name.ne_all(exclude_subquery))
.group_by(tagged_photo::photo_name) .group_by(tagged_photo::photo_name)
.select((tagged_photo::photo_name, count(tagged_photo::tag_id))) .select((tagged_photo::photo_name, count(tagged_photo::tag_id)))
.having(count_distinct(tagged_photo::tag_id).eq(tag_ids.len() as i64)) .having(count_distinct(tagged_photo::tag_id).ge(tag_ids.len() as i64))
.select(tagged_photo::photo_name) .select(tagged_photo::photo_name)
.get_results::<String>(&mut self.connection) .get_results::<String>(&mut self.connection)
.with_context(|| format!("Unable to get Tagged photos with ids: {:?}", tag_ids)) .with_context(|| format!("Unable to get Tagged photos with ids: {:?}", tag_ids))
@@ -384,7 +389,6 @@ impl TagDao for SqliteTagDao {
.filter(tagged_photo::photo_name.ne_all(exclude_subquery)) .filter(tagged_photo::photo_name.ne_all(exclude_subquery))
.group_by(tagged_photo::photo_name) .group_by(tagged_photo::photo_name)
.select((tagged_photo::photo_name, count(tagged_photo::tag_id))) .select((tagged_photo::photo_name, count(tagged_photo::tag_id)))
// .having(count_distinct(tagged_photo::tag_id).eq(tag_ids.len() as i64))
.select(tagged_photo::photo_name) .select(tagged_photo::photo_name)
.get_results::<String>(&mut self.connection) .get_results::<String>(&mut self.connection)
.with_context(|| format!("Unable to get Tagged photos with ids: {:?}", tag_ids)) .with_context(|| format!("Unable to get Tagged photos with ids: {:?}", tag_ids))