diff --git a/src/Client/Client.cs b/src/Client/Client.cs index 61c7dd1..66ab162 100644 --- a/src/Client/Client.cs +++ b/src/Client/Client.cs @@ -89,7 +89,7 @@ public class Client public async Task EntityQueryAsync(string searchdomain, string query) { - return await GetUrlAndProcessJson(GetUrl($"{baseUri}/Entity", "Query", apiKey, new Dictionary() + return await GetUrlAndProcessJson(GetUrl($"{baseUri}/Searchdomain", "Query", apiKey, new Dictionary() { {"searchdomain", searchdomain}, {"query", query} diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index 71a03a3..2698f6a 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -24,32 +24,6 @@ public class EntityController : ControllerBase _databaseHelper = databaseHelper; } - [HttpGet("Query")] - public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) - { - 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 EntityQueryResults() {Results = [], 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 EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } - List<(float, string)> results = searchdomain_.Search(query, topN); - List queryResults = [.. results.Select(r => new EntityQueryResult - { - Name = r.Item2, - Value = r.Item1, - Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null - })]; - return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); - } - [HttpPost("Index")] public ActionResult Index([FromBody] List? jsonEntities) { @@ -95,21 +69,10 @@ public class EntityController : ControllerBase if (returnEmbeddings && !returnModels) { _logger.LogError("Invalid request for {searchdomain} - embeddings return requested but without models - not possible!", [searchdomain]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); - } - 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 EntityListResults() {Results = [], 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 EntityListResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); + return BadRequest(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); EntityListResults entityListResults = new() {Results = [], Success = true}; foreach (Entity entity in searchdomain_.entityCache) { @@ -150,19 +113,8 @@ public class EntityController : ControllerBase [HttpGet("Delete")] public ActionResult Delete(string searchdomain, string entityName) { - 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 EntityDeleteResults() {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 EntityDeleteResults() {Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Entity? entity_ = SearchdomainHelper.CacheGetEntity(searchdomain_.entityCache, entityName); if (entity_ is null) diff --git a/src/Server/Controllers/SearchdomainController.cs b/src/Server/Controllers/SearchdomainController.cs index a89c0b1..66d9aef 100644 --- a/src/Server/Controllers/SearchdomainController.cs +++ b/src/Server/Controllers/SearchdomainController.cs @@ -99,6 +99,21 @@ public class SearchdomainController : ControllerBase return Ok(new SearchdomainUpdateResults(){Success = true}); } + [HttpGet("Query")] + public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) + { + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + List<(float, string)> results = searchdomain_.Search(query, topN); + List queryResults = [.. results.Select(r => new EntityQueryResult + { + Name = r.Item2, + Value = r.Item1, + Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null + })]; + return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); + } + [HttpPost("UpdateSettings")] public ActionResult UpdateSettings(string searchdomain, [FromBody] SearchdomainSettings request) {