/// <summary>
 /// Creates a new Azure Search indexer.  (see
 /// https://msdn.microsoft.com/library/azure/dn946899.aspx for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Search.IIndexerOperations.
 /// </param>
 /// <param name='indexer'>
 /// Required. The definition of the indexer to create.
 /// </param>
 /// <returns>
 /// Response from a Create, Update, or Get Indexer request. If
 /// successful, it includes the full definition of the indexer that
 /// was created, updated, or retrieved.
 /// </returns>
 public static IndexerDefinitionResponse Create(this IIndexerOperations operations, Indexer indexer)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IIndexerOperations)s).CreateAsync(indexer);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        private static void SyncDataFromAzureSQL()
        {
            // This will use the Azure Search Indexer to synchronize data from Azure SQL to Azure Search
            Console.WriteLine("{0}", "Creating Data Source...\n");
            var dataSource =
                new DataSource()
                {
                    Name = UsgsDataSource,
                    Description = "USGS Dataset",
                    Type = DataSourceType.AzureSql,
                    Credentials = new DataSourceCredentials("Server=tcp:azs-playground.database.windows.net,1433;Database=usgs;User ID=reader;Password=EdrERBt3j6mZDP;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"),
                    Container = new DataContainer("GeoNamesRI")
                };

            try
            {
                _searchClient.DataSources.Create(dataSource);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating data source: {0}", ex.Message);
                return;
            }

            Console.WriteLine("{0}", "Creating Indexer and syncing data...\n");

            var indexer =
                new Indexer()
                {
                    Name = UsgsIndexer,
                    Description = "USGS data indexer",
                    DataSourceName = dataSource.Name,
                    TargetIndexName = GeoNamesIndex
                };

            try
            {
                _searchClient.Indexers.Create(indexer);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating and running indexer: {0}", ex.Message);
                return;
            }

            bool running = true;
            Console.WriteLine("{0}", "Synchronization running...\n");
            while (running)
            {
                IndexerExecutionInfo status = null;

                try
                {
                    status = _searchClient.Indexers.GetStatus(indexer.Name);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error polling for indexer status: {0}", ex.Message);
                    return;
                }

                IndexerExecutionResult lastResult = status.LastResult;
                if (lastResult != null)
                {
                    switch (lastResult.Status)
                    {
                        case IndexerExecutionStatus.InProgress:
                            Console.WriteLine("{0}", "Synchronization running...\n");
                            Thread.Sleep(1000);
                            break;

                        case IndexerExecutionStatus.Success:
                            running = false;
                            Console.WriteLine("Synchronized {0} rows...\n", lastResult.ItemCount);
                            break;

                        default:
                            running = false;
                            Console.WriteLine("Synchronization failed: {0}\n", lastResult.ErrorMessage);
                            break;
                    }
                }
            }
        }
        private static void CreateAndSyncIndexer()
        {
            // Create a new indexer and sync it
            try
            {
                var creds = new DataSourceCredentials("Server=tcp:azs-playground.database.windows.net,1433;Database=usgs;User ID=reader;Password=EdrERBt3j6mZDP;Trusted_Connection=False;Encrypt=True;Connection Timeout=30");
                DataSource ds = new DataSource("usgs-datasource", DataSourceType.AzureSql, creds, new DataContainer("GeoNamesRI"));
                ds.Description = "USGS Dataset";

                Indexer idx = new Indexer();
                idx.Name = "usgs-indexer";
                idx.Description = "USGS data indexer";
                idx.DataSourceName = "usgs-datasource";
                idx.TargetIndexName = "geonames";
                idx.Parameters = new IndexingParameters();
                idx.Parameters.MaxFailedItems = 10;
                idx.Parameters.MaxFailedItemsPerBatch = 5;
                idx.Parameters.Base64EncodeKeys = false;

                //Delete indexer and datasource if it existed
                _searchClient.DataSources.Delete("usgs-datasource");
                _searchClient.Indexers.Delete("usgs-indexer");

                //Create indexer and datasource
                _searchClient.DataSources.Create(ds);
                _searchClient.Indexers.Create(idx);

                //Launch the sync and then monitor progress until complete
                AzureOperationResponse response = _searchClient.Indexers.Run("usgs-indexer");
                IndexerGetStatusResponse statusResponse;
                bool running = true;
                
                Console.WriteLine("{0}", "Synchronization running...\n");
                while (running)
                {
                    statusResponse = _searchClient.Indexers.GetStatus("usgs-indexer");
                    if (statusResponse.StatusCode != HttpStatusCode.OK)
                    {
                        Console.WriteLine("Error polling for indexer status.  Status Code: {0}", response.StatusCode.ToString());
                        return;
                    }

                    if (statusResponse.ExecutionInfo.LastResult != null)
                    {
                        switch (statusResponse.ExecutionInfo.LastResult.Status.ToString())
                        {
                            case "InProgress":
                                Console.WriteLine("{0}", "Synchronization running...\n");
                                Thread.Sleep(3000);
                                break;

                            case "Success":
                                running = false;
                                Console.WriteLine("Synchronized {0} rows...\n", statusResponse.ExecutionInfo.LastResult.ItemCount.ToString());
                                break;

                            default:
                                running = false;
                                Console.WriteLine("Synchronization failed: {0}\n", statusResponse.ExecutionInfo.LastResult.ErrorMessage.ToString());
                                break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating indexer: {0}: \n", ex.Message.ToString());
            }

        }
 /// <summary>
 /// Creates a new Azure Search indexer.  (see
 /// https://msdn.microsoft.com/library/azure/dn946899.aspx for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Search.IIndexerOperations.
 /// </param>
 /// <param name='indexer'>
 /// Required. The definition of the indexer to create.
 /// </param>
 /// <returns>
 /// Response from a Create, Update, or Get Indexer request. If
 /// successful, it includes the full definition of the indexer that
 /// was created, updated, or retrieved.
 /// </returns>
 public static Task<IndexerDefinitionResponse> CreateAsync(this IIndexerOperations operations, Indexer indexer)
 {
     return operations.CreateAsync(indexer, CancellationToken.None);
 }
        private static void CreateAndSyncIndexer(string dbConnectionString, string dataSourceName, string dataSourceDescription, string targetCollection, string indexName, string indexerName, string indexerDescription)
        {
            // Create a new indexer and sync it
            try
            {
                var creds = new DataSourceCredentials(dbConnectionString);
                DataSource ds = new DataSource(dataSourceName, DataSourceType.DocumentDb, creds, new DataContainer(targetCollection));
                ds.Description = dataSourceDescription;

                Indexer idx = new Indexer();
                idx.Name = indexerName;
                idx.Description = indexerDescription;
                idx.DataSourceName = dataSourceName;
                idx.TargetIndexName = indexName;
                idx.Parameters = new IndexingParameters();
                idx.Parameters.MaxFailedItems = 10;
                idx.Parameters.MaxFailedItemsPerBatch = 5;
                idx.Parameters.Base64EncodeKeys = false;

                //Delete indexer and datasource if it existed
                _searchClient.DataSources.Delete(dataSourceName);
                _searchClient.Indexers.Delete(indexerName);

                //Create indexer and datasource
                _searchClient.DataSources.Create(ds);
                _searchClient.Indexers.Create(idx);

                //Launch the sync and then monitor progress until complete
                AzureOperationResponse response = _searchClient.Indexers.Run(indexerName);
                IndexerGetStatusResponse statusResponse;
                bool running = true;

                Console.WriteLine("{0}", "Synchronization running...\n");
                while (running)
                {
                    statusResponse = _searchClient.Indexers.GetStatus(indexerName);
                    if (statusResponse.StatusCode != HttpStatusCode.OK)
                    {
                        Console.WriteLine("Error polling for indexer status.  Status Code: {0}", response.StatusCode.ToString());
                        return;
                    }

                    if (statusResponse.ExecutionInfo.LastResult != null)
                    {
                        switch (statusResponse.ExecutionInfo.LastResult.Status.ToString())
                        {
                            case "InProgress":
                                Console.WriteLine("{0}", "Synchronization running...\n");
                                Thread.Sleep(3000);
                                break;

                            case "Success":
                                running = false;
                                Console.WriteLine("Synchronized {0} rows...\n", statusResponse.ExecutionInfo.LastResult.ItemCount.ToString());
                                break;

                            default:
                                running = false;
                                Console.WriteLine("Synchronization failed: {0}\n", statusResponse.ExecutionInfo.LastResult.ErrorMessage.ToString());
                                break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating indexer: {0}: \n", ex.Message.ToString());
            }
        }