コード例 #1
0
        public IndexerFixture()
        {
            SearchServiceClient searchClient = this.GetSearchServiceClient();

            TargetIndexName = TestUtilities.GenerateName();

            DataSourceName = TestUtilities.GenerateName();

            var index = new Index(
                TargetIndexName,
                new[]
                {
                    new Field("feature_id", DataType.String) { IsKey = true },
                });

            AzureOperationResponse createResponse = searchClient.Indexes.Create(index);
            Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

            var dataSource = new DataSource(
                DataSourceName,
                DataSourceType.AzureSql,
                new DataSourceCredentials(AzureSqlReadOnlyConnectionString),
                new DataContainer("GeoNamesRI"));

            createResponse = searchClient.DataSources.Create(dataSource);
            Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);
        }
 /// <summary>
 /// Creates a new Azure Search datasource.  (see
 /// https://msdn.microsoft.com/library/azure/dn946876.aspx for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Search.IDataSourceOperations.
 /// </param>
 /// <param name='dataSource'>
 /// Required. The definition of the datasource to create.
 /// </param>
 /// <returns>
 /// Response from a Create, Update, or Get DataSource request. If
 /// successful, it includes the full definition of the datasource that
 /// was created, updated, or retrieved.
 /// </returns>
 public static DataSourceDefinitionResponse Create(this IDataSourceOperations operations, DataSource dataSource)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDataSourceOperations)s).CreateAsync(dataSource);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
コード例 #3
0
        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;
                    }
                }
            }
        }
 /// <summary>
 /// Creates a new Azure Search datasource.  (see
 /// https://msdn.microsoft.com/library/azure/dn946876.aspx for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Search.IDataSourceOperations.
 /// </param>
 /// <param name='dataSource'>
 /// Required. The definition of the datasource to create.
 /// </param>
 /// <returns>
 /// Response from a Create, Update, or Get DataSource request. If
 /// successful, it includes the full definition of the datasource that
 /// was created, updated, or retrieved.
 /// </returns>
 public static Task<DataSourceDefinitionResponse> CreateAsync(this IDataSourceOperations operations, DataSource dataSource)
 {
     return operations.CreateAsync(dataSource, CancellationToken.None);
 }
コード例 #5
0
        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());
            }
        }