public async Task <IActionResult> GetAllDocuments() { try { var queryResponse = await _elasticClient .CountAsync <object>(s => s.Index(IndexName)); var recordsCount = queryResponse.Count; var allRecords = await _elasticClient .SearchAsync <object>(s => s.Index(IndexName).Size(Convert.ToInt32(recordsCount)).MatchAll()); if (queryResponse.IsValid) { var responseObject = new Entities.SearchResponse { RecordCount = recordsCount, Records = allRecords.Documents }; return(Ok(responseObject)); } else { return(BadRequest(queryResponse.ServerError.Error)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> GetSorting([FromBody] PostRequestBody postRequestBody) { try { var sortOrder = postRequestBody.SortOrder != null && postRequestBody.SortOrder == "ASC" ? SortOrder.Ascending : SortOrder.Descending; var queryResponse = await _elasticClient.SearchAsync <object>(x => x.Index(IndexName) .Sort(ss => ss.Field(postRequestBody.SortField, sortOrder))); if (queryResponse.IsValid) { var responseObject = new Entities.SearchResponse { RecordCount = queryResponse.HitsMetadata.Total.Value, Records = queryResponse.Documents }; return(Ok(JsonConvert.SerializeObject(responseObject))); } else { return(BadRequest(queryResponse.ServerError.Error)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> GetPagination([FromBody] PostRequestBody postRequestBody) { try { int from = 0; if (postRequestBody.PageIndex.HasValue) { from = (postRequestBody.PageIndex.Value - 1) * postRequestBody.PageSize.Value; } var queryResponse = await _elasticClient.SearchAsync <object>(x => x.Index(IndexName) .From(from) .Size(postRequestBody.PageSize)); if (queryResponse.IsValid) { var responseObject = new Entities.SearchResponse { RecordCount = queryResponse.HitsMetadata.Total.Value, Records = queryResponse.Documents }; return(Ok(JsonConvert.SerializeObject(responseObject))); } else { return(BadRequest(queryResponse.ServerError.Error)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }