From 524c015d77bcb9cb762d8470c6de80d9a2c6b9cb Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 29 Aug 2023 18:26:08 +1000 Subject: Improved search engine performance and added minimal functionality for searching implicit tags --- Services/SearchService.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'Services/SearchService.cs') 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 dbFactory; - public SearchService(IDbContextFactory dbFactory) => - this.dbFactory = dbFactory; + public SearchService( + IDbContextFactory 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(); } } -- cgit v1.3