private static void PersistToSearch(Guid id, RegisterRestoCommand command, Coordinates coordinates) { using (var ctx = new RestoContext()) { var connection = ApiConnection.Create(CloudConfigurationManager.GetSetting("Azure.Search.ServiceName"), CloudConfigurationManager.GetSetting("Azure.Search.ApiKey")); var indexClient = new IndexManagementClient(connection); var indexName = CloudConfigurationManager.GetSetting("Azure.Search.IndexName"); var restaurant = ctx.Restaurants .Include(r => r.Accommodations.Select(a => a.Accommodation.Translations)) .FirstOrDefault(r => r.Id == id); var operation = new IndexOperation(IndexOperationType.MergeOrUpload, "id", id.ToString()) .WithProperty("name", command.Name) .WithProperty("locality", command.City) .WithProperty("budget", command.Budget) .WithProperty("rating", command.Rating) .WithProperty("street", command.Street) .WithProperty("accommodations", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("en")) .WithProperty("accommodations_fr", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("fr")) .WithProperty("accommodations_nl", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("nl")) .WithProperty("cuisine", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("en")) .WithProperty("cuisine_fr", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("fr")) .WithProperty("cuisine_nl", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("nl")); if (coordinates != null) { operation.WithGeographyPoint("location", coordinates.Longitude, coordinates.Latitude); } var response = indexClient.PopulateAsync(indexName, operation).Result; // Error handling! if (!response.IsSuccess) { Console.WriteLine("Error: " + response.StatusCode); return; } else { var failed = response.Body.Where(r => !r.Status); foreach (var item in failed) { Console.WriteLine("Failed: {0} ({1})", item.Key, item.ErrorMessage); } } Console.WriteLine("Persisted to Search."); } }
private static void Main(string[] args) { var indexName = CloudConfigurationManager.GetSetting("Azure.Search.IndexName"); Log("Starting full sync to: {0}", indexName); var searchClient = GetSearchClient(); while (true) { using (var dbTimer = CallTimer.Start()) { // Get batch of restaurants. var restaurants = GetRestaurants(Skip, Take); dbTimer.Stop(); // No results, EOF. if (restaurants == null) { break; } // Flatten. var operations = new List <IndexOperation>(); foreach (var restaurant in restaurants) { var indexOperation = new IndexOperation(IndexOperationType.MergeOrUpload, "id", restaurant.Id.ToString()); indexOperation .WithProperty("internalName", restaurant.InternalName) .WithProperty("name", restaurant.Name) .WithProperty("postalCode", restaurant.PostalCode) .WithProperty("locality", restaurant.Locality) .WithProperty("street", restaurant.StreetAddress) .WithProperty("website", restaurant.Website) .WithProperty("budget", restaurant.Budget) .WithProperty("rating", restaurant.Rating) .WithProperty("fax", restaurant.Fax) .WithProperty("mobile", restaurant.Mobile) .WithProperty("phoneNumber", restaurant.PhoneNumber) .WithProperty("email", restaurant.Email) .WithProperty("hasImage", restaurant.HasImage) // Translated content. .WithProperty("region", restaurant.Region.TryGet("en")) .WithProperty("region_nl", restaurant.Region.TryGet("nl")) .WithProperty("region_fr", restaurant.Region.TryGet("fr")) .WithProperty("description", restaurant.TryGet(r => r.Description, "en")) .WithProperty("description_fr", restaurant.TryGet(r => r.Description, "fr")) .WithProperty("description_nl", restaurant.TryGet(r => r.Description, "nl")) .WithProperty("closing", restaurant.TryGet(r => r.Closing, "en")) .WithProperty("closing_fr", restaurant.TryGet(r => r.Closing, "fr")) .WithProperty("closing_nl", restaurant.TryGet(r => r.Closing, "nl")) .WithProperty("setting", restaurant.TryGet(r => r.Setting, "en")) .WithProperty("setting_fr", restaurant.TryGet(r => r.Setting, "fr")) .WithProperty("setting_nl", restaurant.TryGet(r => r.Setting, "nl")) // Translated tags. .WithProperty("accommodations", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("en")) .WithProperty("accommodations_fr", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("fr")) .WithProperty("accommodations_nl", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("nl")) .WithProperty("cuisine", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("en")) .WithProperty("cuisine_fr", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("fr")) .WithProperty("cuisine_nl", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("nl")) .WithProperty("paymentFacilities", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("en")) .WithProperty("paymentFacilities_fr", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("fr")) .WithProperty("paymentFacilities_nl", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("nl")); // Add geocoordinates if available. if (restaurant.Longitude.HasValue && restaurant.Latitude.HasValue) { indexOperation.WithGeographyPoint("location", restaurant.Longitude.Value, restaurant.Latitude.Value); } // Add to batch. operations.Add(indexOperation); } using (var searchTimer = CallTimer.Start()) { var response = searchClient.PopulateAsync(indexName, operations.ToArray()).Result; // Error handling! if (!response.IsSuccess) { throw new Exception(response.StatusCode.ToString()); } else { var failed = response.Body.Where(r => !r.Status); foreach (var item in failed) { Log("Failed: {0} ({1})", item.Key, item.ErrorMessage); } } // Move forward. Skip += Take; Processed += restaurants.Count(); // Done! Log("Processed: {0} (Db: {1} s., Search: {2} s.)", Processed, dbTimer.TotalSeconds, searchTimer.TotalSeconds); } } } Log("Done!"); }