Skip to content

Conversation

TrigamDev
Copy link
Contributor

@TrigamDev TrigamDev commented Sep 21, 2025

Fixes searching <Tag A> AND <Tag A> returning no results, when it should return the same results as <Tag A> (#951).

I'm not very familiar with the backend and haven't done very in-depth testing, so please point out if this causes any unintended side effects.

Summary

Tweaked the __separate_tags() method in src/tagstudio/core/library/alchemy/visitors.py to, rather than a list, use a set for collecting the tag IDs of each term of an AST ANDList and ORList to prevent duplicate tag IDs from being returned.

Cause of Bug

The SQL query built from the search <Tag A> AND <Tag A> will always fail due to this section:

WHERE tag_entries.tag_id IN (1, 1)
GROUP BY tag_entries.entry_id
HAVING count(DISTINCT tag_entries.tag_id) = 2

Since it's getting rows where count(DISTINCT tag_entries.tag_id) = 2, but it's only searching for rows with a single tag ID, count(DISTINCT tag_entries.tag_id) will always equal 1.

This doesn't apply just to ANDLists containing two of the same tag, any ANDList containing at least two duplicate tags will cause the query to fail. So a search such as <Tag A> AND <Tag B> AND <Tag A> AND <Tag A> would also fail.
However, if the duplicate tag is ambiguous, the SQL query built filters entries in a fundamentally different way that doesn't suffer the same issue, and the search acts as expected.

Tasks Completed

  • Platforms Tested:
    • Windows x86
    • Windows ARM
    • macOS x86
    • macOS ARM
    • Linux x86
    • Linux ARM
  • Tested For:
    • Basic functionality
    • PyInstaller executable

@CyanVoxel CyanVoxel added TagStudio: Search The TagStudio search engine Status: Review Needed A review of this is needed labels Sep 23, 2025
@CyanVoxel CyanVoxel moved this to 🏓 Ready for Review in TagStudio Development Sep 23, 2025
@Computerdores Computerdores moved this from 🏓 Ready for Review to 🍃 Pending Merge in TagStudio Development Sep 24, 2025
@CyanVoxel CyanVoxel added Status: Mergeable The code is ready to be merged and removed Status: Review Needed A review of this is needed labels Oct 8, 2025
@CyanVoxel CyanVoxel added this to the Alpha v9.5.6 milestone Oct 8, 2025
@CyanVoxel
Copy link
Member

Thank you for your work on this!

@CyanVoxel CyanVoxel merged commit db7b126 into TagStudioDev:main Oct 8, 2025
5 checks passed
@github-project-automation github-project-automation bot moved this from 🍃 Pending Merge to ✅ Done in TagStudio Development Oct 8, 2025
@CyanVoxel CyanVoxel removed the Status: Mergeable The code is ready to be merged label Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TagStudio: Search The TagStudio search engine
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants