From 1f67682879cb6a880c380ba039900df8dcca390b Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sun, 14 Dec 2025 22:07:14 +0100 Subject: [PATCH 1/4] Implemented search cache --- src/Server/Models/SearchdomainModels.cs | 19 +++++++++++++++ src/Server/Searchdomain.cs | 32 ++++++++++++++++++++----- src/Server/SearchdomainManager.cs | 4 +++- 3 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/Server/Models/SearchdomainModels.cs diff --git a/src/Server/Models/SearchdomainModels.cs b/src/Server/Models/SearchdomainModels.cs new file mode 100644 index 0000000..e91a755 --- /dev/null +++ b/src/Server/Models/SearchdomainModels.cs @@ -0,0 +1,19 @@ +using MySqlX.XDevAPI.Common; + +namespace Server.Models; +public readonly struct ResultItem(float score, string name) +{ + public readonly float Score = score; + public readonly string Name = name; +} + +public struct DateTimedSearchResult +{ + public List accessTimes; + public SearchResult Results; +} + +public readonly struct SearchResult(List results) +{ + public readonly List Results = results; +} \ No newline at end of file diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index 1df5dc7..602f2cc 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -3,6 +3,7 @@ using System.Data.Common; using ElmahCore.Mvc.Logger; using MySql.Data.MySqlClient; using Server.Helper; +using Server.Models; namespace Server; @@ -13,7 +14,7 @@ public class Searchdomain public AIProvider aIProvider; public string searchdomain; public int id; - public Dictionary)>> searchCache; // Yeah look at this abomination. searchCache[x][0] = last accessed time, searchCache[x][1] = results for x + public Dictionary searchCache; // Key: query, Value: Search results for that query (with timestamp) public List entityCache; public List modelsInUse; public Dictionary> embeddingCache; @@ -22,8 +23,6 @@ public class Searchdomain public SQLHelper helper; private readonly ILogger _logger; - // TODO Add settings and update cli/program.cs, as well as DatabaseInsertSearchdomain() - public Searchdomain(string searchdomain, string connectionString, AIProvider aIProvider, Dictionary> embeddingCache, ILogger logger, string provider = "sqlserver", bool runEmpty = false) { _connectionString = connectionString; @@ -47,6 +46,7 @@ public class Searchdomain public void UpdateEntityCache() { + InvalidateSearchCache(); Dictionary parametersIDSearchdomain = new() { ["id"] = this.id @@ -151,8 +151,14 @@ public class Searchdomain embeddingCache = []; // TODO remove this and implement proper remediation to improve performance } - public List<(float, string)> Search(string query, bool sort=true) + public List<(float, string)> Search(string query) { + if (searchCache.TryGetValue(query, out DateTimedSearchResult cachedResult)) + { + cachedResult.accessTimes.Add(DateTime.Now); + return [.. cachedResult.Results.Results.Select(r => (r.Score, r.Name))]; + } + if (!embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings)) { queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider); @@ -181,8 +187,17 @@ public class Searchdomain } result.Add((entity.probMethod(datapointProbs), entity.name)); } - - return [.. result.OrderByDescending(s => s.Item1)]; // [.. element] = element.ToList() + List<(float, string)> results = [.. result.OrderByDescending(s => s.Item1)]; + SearchResult searchResult = new( + [.. results.Select(r => + new ResultItem(r.Item1, r.Item2 ))] + ); + searchCache[query] = new DateTimedSearchResult + { + accessTimes = [DateTime.Now], + Results = searchResult + }; + return results; } public static List GetModels(List entities) @@ -217,4 +232,9 @@ public class Searchdomain reader.Close(); return this.id; } + + public void InvalidateSearchCache() + { + searchCache = []; + } } diff --git a/src/Server/SearchdomainManager.cs b/src/Server/SearchdomainManager.cs index 855cac7..e354f97 100644 --- a/src/Server/SearchdomainManager.cs +++ b/src/Server/SearchdomainManager.cs @@ -64,7 +64,9 @@ public class SearchdomainManager public void InvalidateSearchdomainCache(string searchdomainName) { - GetSearchdomain(searchdomainName).UpdateEntityCache(); + var searchdomain = GetSearchdomain(searchdomainName); + searchdomain.UpdateEntityCache(); + searchdomain.InvalidateSearchCache(); // TODO implement cache remediation (Suggestion: searchdomain-wide setting for cache remediation / invalidation - ) } public List ListSearchdomains() From 21194f99d3a1cb6dc867c205330b1ea762af5005 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sun, 14 Dec 2025 23:13:34 +0100 Subject: [PATCH 2/4] Implemented search result endpoint --- .../Controllers/SearchdomainController.cs | 23 +++++++++++++++++++ src/Server/Models/SearchdomainModels.cs | 19 --------------- src/Server/Searchdomain.cs | 14 ++++------- src/Shared/Models/SearchdomainModels.cs | 19 +++++++++++++++ src/Shared/Models/SearchdomainResults.cs | 11 +++++++++ 5 files changed, 58 insertions(+), 28 deletions(-) delete mode 100644 src/Server/Models/SearchdomainModels.cs create mode 100644 src/Shared/Models/SearchdomainModels.cs diff --git a/src/Server/Controllers/SearchdomainController.cs b/src/Server/Controllers/SearchdomainController.cs index 4b45bbd..564dd88 100644 --- a/src/Server/Controllers/SearchdomainController.cs +++ b/src/Server/Controllers/SearchdomainController.cs @@ -105,4 +105,27 @@ public class SearchdomainController : ControllerBase } return Ok(new SearchdomainUpdateResults(){Success = true}); } + + [HttpGet("GetSearches")] + public ActionResult GetSearches(string searchdomain) + { + Searchdomain searchdomain_; + try + { + searchdomain_ = _domainManager.GetSearchdomain(searchdomain); + } + catch (SearchdomainNotFoundException) + { + _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); + return Ok(new SearchdomainSearchesResults() { Searches = [], Success = false, Message = "Searchdomain not found" }); + } + catch (Exception ex) + { + _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); + return Ok(new SearchdomainSearchesResults() { Searches = [], Success = false, Message = ex.Message }); + } + Dictionary searchCache = searchdomain_.searchCache; + + return Ok(new SearchdomainSearchesResults() { Searches = searchCache, Success = true }); + } } diff --git a/src/Server/Models/SearchdomainModels.cs b/src/Server/Models/SearchdomainModels.cs deleted file mode 100644 index e91a755..0000000 --- a/src/Server/Models/SearchdomainModels.cs +++ /dev/null @@ -1,19 +0,0 @@ -using MySqlX.XDevAPI.Common; - -namespace Server.Models; -public readonly struct ResultItem(float score, string name) -{ - public readonly float Score = score; - public readonly string Name = name; -} - -public struct DateTimedSearchResult -{ - public List accessTimes; - public SearchResult Results; -} - -public readonly struct SearchResult(List results) -{ - public readonly List Results = results; -} \ No newline at end of file diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index 602f2cc..edd5072 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -3,7 +3,7 @@ using System.Data.Common; using ElmahCore.Mvc.Logger; using MySql.Data.MySqlClient; using Server.Helper; -using Server.Models; +using Shared.Models; namespace Server; @@ -155,8 +155,8 @@ public class Searchdomain { if (searchCache.TryGetValue(query, out DateTimedSearchResult cachedResult)) { - cachedResult.accessTimes.Add(DateTime.Now); - return [.. cachedResult.Results.Results.Select(r => (r.Score, r.Name))]; + cachedResult.AccessDateTimes.Add(DateTime.Now); + return [.. cachedResult.Results.Select(r => (r.Score, r.Name))]; } if (!embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings)) @@ -188,15 +188,11 @@ public class Searchdomain result.Add((entity.probMethod(datapointProbs), entity.name)); } List<(float, string)> results = [.. result.OrderByDescending(s => s.Item1)]; - SearchResult searchResult = new( + List searchResult = new( [.. results.Select(r => new ResultItem(r.Item1, r.Item2 ))] ); - searchCache[query] = new DateTimedSearchResult - { - accessTimes = [DateTime.Now], - Results = searchResult - }; + searchCache[query] = new DateTimedSearchResult(DateTime.Now, searchResult); return results; } diff --git a/src/Shared/Models/SearchdomainModels.cs b/src/Shared/Models/SearchdomainModels.cs new file mode 100644 index 0000000..47656b6 --- /dev/null +++ b/src/Shared/Models/SearchdomainModels.cs @@ -0,0 +1,19 @@ + +using System.Text.Json.Serialization; + +namespace Shared.Models; +public readonly struct ResultItem(float score, string name) +{ + [JsonPropertyName("Score")] + public readonly float Score { get; } = score; + [JsonPropertyName("Name")] + public readonly string Name { get; } = name; +} + +public struct DateTimedSearchResult(DateTime dateTime, List results) +{ + [JsonPropertyName("AccessDateTimes")] + public List AccessDateTimes { get; set; } = [dateTime]; + [JsonPropertyName("Results")] + public List Results { get; set; } = results; +} \ No newline at end of file diff --git a/src/Shared/Models/SearchdomainResults.cs b/src/Shared/Models/SearchdomainResults.cs index 2ccf500..5033753 100644 --- a/src/Shared/Models/SearchdomainResults.cs +++ b/src/Shared/Models/SearchdomainResults.cs @@ -43,3 +43,14 @@ public class SearchdomainDeleteResults [JsonPropertyName("DeletedEntities")] public required int DeletedEntities { get; set; } } + +public class SearchdomainSearchesResults +{ + [JsonPropertyName("Success")] + public required bool Success { get; set; } + + [JsonPropertyName("Message")] + public string? Message { get; set; } + [JsonPropertyName("Searches")] + public required Dictionary Searches { get; set; } +} From 24228c05f2d3676822a9b11c19a5d1a5704b4f51 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sun, 14 Dec 2025 23:14:08 +0100 Subject: [PATCH 3/4] Implemented query listing in front-end --- src/Server/Views/Home/Index.cshtml | 136 ++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 31 deletions(-) diff --git a/src/Server/Views/Home/Index.cshtml b/src/Server/Views/Home/Index.cshtml index a2ae3fa..fc0c771 100644 --- a/src/Server/Views/Home/Index.cshtml +++ b/src/Server/Views/Home/Index.cshtml @@ -1,5 +1,7 @@ @using Server.Models @using System.Web +@using Server.Services +@inject LocalizationService T @model HomeIndexViewModel @{ ViewData["Title"] = "Home Page"; @@ -81,15 +83,17 @@ placeholder="filter" /> - - @*
- Some test query - -
-
- Some other test query - -
*@ +
+ + + + + + + + + +
NameAction
@@ -116,15 +120,6 @@ - - @*
- Someentity - -
-
- Some other test query - -
*@ @@ -186,6 +181,7 @@ \ No newline at end of file