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/Searchdomain.cs b/src/Server/Searchdomain.cs index 1df5dc7..edd5072 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 Shared.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.AccessDateTimes.Add(DateTime.Now); + return [.. cachedResult.Results.Select(r => (r.Score, r.Name))]; + } + if (!embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings)) { queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider); @@ -181,8 +187,13 @@ 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)]; + List searchResult = new( + [.. results.Select(r => + new ResultItem(r.Item1, r.Item2 ))] + ); + searchCache[query] = new DateTimedSearchResult(DateTime.Now, searchResult); + return results; } public static List GetModels(List entities) @@ -217,4 +228,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() diff --git a/src/Server/Views/Home/Index.cshtml b/src/Server/Views/Home/Index.cshtml index a2ae3fa..cd45f1a 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 - -
*@ @@ -182,10 +177,52 @@ + + + + \ No newline at end of file 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; } +}