Only 50 items are displayed when displaying search results with the azure search service

184 Views Asked by At

When I get the result of a search I see this

enter image description here

That is, I get 298 results but only 50 values

I use this in my web API

private async Task<SearchResults<Oferta>> RunQueryAsync(SearchData model)
{
  InitSearch();
  var options = new SearchOptions()
  {
    IncludeTotalCount = true
  };

  options.HighlightFields.Add("content");
  options.HighlightPreTag = "<mark>";
  options.HighlightPostTag = "</mark>";
  options.Select.Add("metadata_storage_path");
  options.Select.Add("content");

  model.resultList = await _searchClient.SearchAsync<Oferta>(model.searchText, options).ConfigureAwait(false);

  model.resultList.GetResults().ToList().ForEach(r => {

  .....
  })

return model.resultList;   

In the loop I access the 298 returned elements and in all of them I decode the value of Document.metadata_storage_path since it is an index that analyzes files stored in blobs of a storage account, but when returning model.resulList only appears in the first 50 the decoded value in the remainder is still base 64 encoded

enter image description here

Any idea, please?

Thanks

2

There are 2 best solutions below

1
Gaurav Mantri On

Please add Size to your SearchOptions and set its value to a number less than or equal to 1000. Your code would be something like:

var options = new SearchOptions()
{
    IncludeTotalCount = true,
    Size = 1000
};

Size parameter maps to $top query parameter in Search Documents REST API. If no value is specified for the $top parameter, then 50 documents are returned by default.

enter image description here

0
kintela On

With this code I get all the results even if they are more than 1000 And the calling front end receives all the values with the correctly decoded path

private async Task<List<SearchResult<Oferta>>> RunQueryAsync(SearchData model)
{
  InitSearch();
  var options = new SearchOptions()
  {
    IncludeTotalCount = true
  };

  options.HighlightFields.Add("content");
  options.HighlightPreTag = "<mark>";
  options.HighlightPostTag = "</mark>";
  options.Select.Add("metadata_storage_path");
  options.Select.Add("content");

  int currentPage = 0;
  int pageSize = 50;
  bool hasMoreResults = true;
  List<SearchResult<Oferta>> allResults = new List<SearchResult<Oferta>>();

  while (hasMoreResults)
  {
    options.Skip = currentPage * pageSize;
    options.Size = pageSize;

    model.resultList = await _searchClient.SearchAsync<Oferta>(model.searchText, options).ConfigureAwait(false);

    var currentResults = model.resultList.GetResults().ToList();
    currentResults.ForEach(r => {
      string pathCodificado = r.Document.metadata_storage_path;

      r.Document.metadata_storage_path = DecodeBase64(r.Document.metadata_storage_path);
      if (r.Document.metadata_storage_path != "")
      {
        r.Document.metadata_storage_path = r.Document.metadata_storage_path.Substring(62);
        r.Document.year = ExtraerAño(r.Document.metadata_storage_path);
      }
      else//Se ha producido algun error decodificando
      {
        r.Document.metadata_storage_path = pathCodificado;
        r.Document.year = -1;
      }
    });

    allResults.AddRange(currentResults);

    hasMoreResults = model.resultList.TotalCount > (currentPage + 1) * pageSize;

    currentPage++;
  }

    
  return allResults;
}

Pagination is performed in the RunQueryAsync method and returns a complete list of decoded results