From e4a711fcbd290dd111e2af1f5fef4544998d00e4 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sat, 20 Dec 2025 20:13:28 +0100 Subject: [PATCH] Added filtering query output to top n elements --- src/Server/Controllers/EntityController.cs | 4 ++-- src/Server/Searchdomain.cs | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index 4765311..36ac1a8 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -25,7 +25,7 @@ public class EntityController : ControllerBase } [HttpGet("Query")] - public ActionResult Query(string searchdomain, string query) + public ActionResult Query(string searchdomain, string query, int? topN) { Searchdomain searchdomain_; try @@ -40,7 +40,7 @@ public class EntityController : ControllerBase _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); } - var results = searchdomain_.Search(query); + List<(float, string)> results = searchdomain_.Search(query, topN); List queryResults = [.. results.Select(r => new EntityQueryResult { Name = r.Item2, diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index 019eb79..7566363 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -154,7 +154,7 @@ public class Searchdomain embeddingCache = []; // TODO remove this and implement proper remediation to improve performance } - public List<(float, string)> Search(string query) + public List<(float, string)> Search(string query, int? topN = null) { if (searchCache.TryGetValue(query, out DateTimedSearchResult cachedResult)) { @@ -190,9 +190,14 @@ public class Searchdomain } result.Add((entity.probMethod(datapointProbs), entity.name)); } - List<(float, string)> results = [.. result.OrderByDescending(s => s.Item1)]; + IEnumerable<(float, string)> sortedResults = result.OrderByDescending(s => s.Item1); + if (topN is not null) + { + sortedResults = sortedResults.Take(topN ?? 0); + } + List<(float, string)> results = [.. sortedResults]; List searchResult = new( - [.. results.Select(r => + [.. sortedResults.Select(r => new ResultItem(r.Item1, r.Item2 ))] ); searchCache[query] = new DateTimedSearchResult(DateTime.Now, searchResult);