summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-08-29 18:26:08 +1000
committerJake Mannens <jake@asger.xyz>2023-08-29 18:26:08 +1000
commit524c015d77bcb9cb762d8470c6de80d9a2c6b9cb (patch)
treea84cad31ce7ca04487c38e80dba427e670482700
parent7712b507a47b41f3c069bcf671d4592dcdc0f713 (diff)
Improved search engine performance and added minimal functionality for searching implicit tags
-rw-r--r--Services/SearchService.cs30
1 files changed, 20 insertions, 10 deletions
diff --git a/Services/SearchService.cs b/Services/SearchService.cs
index 8a109ea..e8e497d 100644
--- a/Services/SearchService.cs
+++ b/Services/SearchService.cs
@@ -7,10 +7,17 @@ public interface ISearchService {
}
public class SearchService : ISearchService {
+ private ITagService tagService;
+
private IDbContextFactory<HBContext> dbFactory;
- public SearchService(IDbContextFactory<HBContext> dbFactory) =>
- this.dbFactory = dbFactory;
+ public SearchService(
+ IDbContextFactory<HBContext> dbFactory,
+ ITagService tagService) {
+
+ this.tagService = tagService;
+ this.dbFactory = dbFactory;
+ }
public Media[] Search(string query) {
var db = dbFactory.CreateDbContext();
@@ -18,29 +25,32 @@ public class SearchService : ISearchService {
query = query.ToLower();
var matchedTag = db.TagDefinitions
- .FirstOrDefault(td => td.Name.ToLower() == query)?.Guid;
+ .FirstOrDefault(td => td.Name.ToLower() == query);
- Guid[] guids;
+ int[] tags;
if(matchedTag is not null) {
- guids = new[] { (Guid) matchedTag };
+ tags = tagService
+ .TagsThatImply(matchedTag)
+ .Select(td => td.ObjectId)
+ .ToArray();
} else {
- guids = db.TagDefinitions
+ // TODO: expand scope to all tags that imply
+ tags = db.TagDefinitions
.Where(td => td.Name.ToLower().Contains(query))
- .Select(td => td.Guid)
+ .Select(td => td.ObjectId)
.ToArray();
}
return db.Media
.Include(m => m.Tags)
- .ThenInclude(m => m.TagDefinition)
.AsEnumerable()
- .Where(m => m.Tags.IntersectBy(guids, t => t.TagDefinition.Guid).Any())
+ .Where(m => m.Tags.IntersectBy(tags, t => t.TagDefinitionId).Any())
.Concat(db.Media
.Where(m =>
(m.ShortDescription != null && m.ShortDescription.ToLower().Contains(query)) ||
(m.LongDescription != null && m.LongDescription.ToLower().Contains(query))))
- .DistinctBy(m => m.Guid)
+ .DistinctBy(m => m.ObjectId)
.ToArray();
}
}