private async Task <IEnumerable <AzureSearchVenueModel> > GetVenues(ILogger log, IEnumerable <CourseRun> runs = null)
        {
            IVenueServiceWrapper    service   = new VenueServiceWrapper(_venueServiceSettings);
            IEnumerable <CourseRun> venueruns = runs?.Where(x => x.VenueId != null);

            log.LogInformation($"Getting data for { venueruns?.Count().ToString() ?? "all" } venues");

            // Get all venues to save time & RUs if there's too many to get by Id
            if (venueruns == null || venueruns.Count() > _searchServiceSettings.ThresholdVenueCount)
            {
                return(await service.GetVenues());
            }
            else
            {
                List <AzureSearchVenueModel> venues = new List <AzureSearchVenueModel>();
                foreach (CourseRun r in venueruns)
                {
                    if (!venues.Any(x => x.id == r.VenueId.Value))
                    {
                        AzureSearchVenueModel venue = await service.GetById <AzureSearchVenueModel>(r.VenueId.Value);

                        if (venue != null)
                        {
                            venues.Add(venue);
                        }
                    }
                }
                log.LogInformation($"Successfully retrieved data for {venues.Count()} venues");
                return(venues);
            }
        }
        public async Task RepopulateSearchIndex(ILogger log)
        {
            var allProviders = new ProviderServiceWrapper(_providerServiceSettings, new HttpClient())
                               .GetLiveProvidersForAzureSearch()
                               .GroupBy(p => p.UnitedKingdomProviderReferenceNumber)
                               .ToDictionary(g => g.Key, g => g.First());

            var venueService = new VenueServiceWrapper(_venueServiceSettings);
            var allVenues    = (await venueService.GetVenues())
                               .ToDictionary(v => v.id.Value, v => v);

            var searchServiceWrapper = new SearchServiceWrapper(log, _searchServiceSettings);

            var indexedCount = 0;

            using (var client = _cosmosDbHelper.GetClient())
            {
                var collectionLink = UriFactory.CreateDocumentCollectionUri(
                    _cosmosDbSettings.DatabaseId,
                    _settings.CoursesCollectionId);

                // N.B. We deliberately don't filter out non-live courses here;
                // UploadBatch needs to be passed those documents so it can remove them from the index
                var query = client.CreateDocumentQuery <Course>(
                    collectionLink,
                    new FeedOptions()
                {
                    EnableCrossPartitionQuery = true
                })
                            .AsDocumentQuery();

                while (query.HasMoreResults)
                {
                    var result = await query.ExecuteNextAsync <Document>();

                    var indexed = await searchServiceWrapper.UploadBatch(allProviders, allVenues, result);

                    indexedCount += indexed.Count();
                }
            }

            log.LogInformation($"Indexed {indexedCount} records.");
        }