What Is My Current Project?
Currently, I am working on developing a file tagging program using JavaScript with Electron. I have decided to incorporate SQLite into the project for data management. However, I am facing a challenge when it comes to implementing tag-based searches. Since SQL and SQLite are relatively new to me, I am unsure if it is feasible to achieve this solely through queries. Can anyone provide guidance on how I can execute searches based on tags as outlined below?
Specifics of Searching:
After exploring FTS3/4 options, I have discovered that while they meet my requirements, wildcard searches seem to be unsupported.
- Find files with all specified tags:
blue_sky AND green_grass
- Discover files without specific tags:
NOT blue_sky AND NOT green_grass
- Detect files with any of the given tags:
green_sky OR blue_sky
- Look for files with wildcards anywhere in a tag:
*sky AND *grass AND *bl*e*
- Combine the above search criteria:
blue_sky AND green*
/green_grass AND blue_sky OR green_sky
Database Schema:
The structure may undergo modifications
CREATE TABLE files (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE tags (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE file_tags (
id INTEGER PRIMARY KEY,
file_id INTEGER,
tag_id INTEGER
);
Examples:
INSERT INTO files (name) VALUES ('file_1.png');
INSERT INTO files (name) VALUES ('file_2.png');
INSERT INTO files (name) VALUES ('file_3.png');
INSERT INTO files (name) VALUES ('file_4.png');
INSERT INTO tags (name) VALUES ('blue_sky');
INSERT INTO tags (name) VALUES ('green_sky');
INSERT INTO tags (name) VALUES ('green_grass');
INSERT INTO tags (name) VALUES ('blue_grass');
INSERT INTO tags (name) VALUES ('greenish_blue_sky');
INSERT INTO file_tags (file_id, tag_id) VALUES(file1_id, blue_sky_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file1_id, green_grass_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file2_id, blue_sky_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file2_id, blue_grass_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file3_id, greenish_blue_sky_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file4_id, green_sky_id);
INSERT INTO file_tags (file_id, tag_id) VALUES(file4_id, blue_grass_id);
Query: blue_sky and green_grass
result: file_1
Query: blue_sky or green_sky
result: file_1, file_2, file_4
Query:
blue_sky and green_grass or blue_grass
result:
file_1, file_2
Query: *ish*
result: file_3
Query: *bl*e*
result: file_1, file_2, file_3, file_4
Query: *sky and not blue_grass
result: file_1, file3
Note: If SQLite does not appear suitable for solving this problem, I am open to alternative suggestions.