Beispiel #1
0
        private async Task CreateIndexAsync(
            CancellationToken ct)
        {
            var index = AzureIndexDefinition.Create(searchClient.IndexName);

            await indexClient.CreateOrUpdateIndexAsync(index, true, true, ct);
        }
Beispiel #2
0
        static async Task CreateIndexAsync(SearchIndexClient clientIndex)
        {
            Console.WriteLine("Creating (or updating) search index");
            SearchIndex index  = new BookSearchIndex(SEARCH_INDEX_NAME);
            var         result = await clientIndex.CreateOrUpdateIndexAsync(index);

            Console.WriteLine(result);
        }
Beispiel #3
0
        private static async Task CreateIndex(string indexName, SearchIndexClient adminClient)
        {
            FieldBuilder fieldBuilder = new FieldBuilder();
            var          searchFields = fieldBuilder.Build(typeof(Review));

            var definition = new SearchIndex(indexName, searchFields);

            await adminClient.CreateOrUpdateIndexAsync(definition);
        }
Beispiel #4
0
 public async Task CreateIndexAsync(SearchIndex index)
 {
     try
     {
         await _searchIndexClient.CreateOrUpdateIndexAsync(index);
     }
     catch (Exception)
     {
         throw new Exception("Cognitive Search APIs -> Failed To Create Index!");
     }
 }
Beispiel #5
0
        private async Task <SearchClient> CreateOrUpdateIndexAsync <T>(string indexName = null)
        {
            indexName = string.IsNullOrEmpty(indexName) ? typeof(T).Name : indexName;

            var fieldBuilder = new FieldBuilder();
            var searchFields = fieldBuilder.Build(typeof(T));
            var definition   = new SearchIndex(indexName, searchFields);
            await _searchIndexClient.CreateOrUpdateIndexAsync(definition);

            return(_searchIndexClient.GetSearchClient(indexName));
        }
Beispiel #6
0
        public static async Task Create(SearchIndexClient indexClient, SearchIndexName searchIndexName)
        {
            var fieldBuilder = new FieldBuilder();
            IList <SearchField>?searchFields = fieldBuilder.Build(typeof(IndexableProduct));

            var definition = new SearchIndex(searchIndexName.Value, searchFields);

            var suggester = new SearchSuggester("sg", new[] { nameof(IndexableProduct.Title), nameof(IndexableProduct.Description) });

            definition.Suggesters.Add(suggester);

            var _ = await indexClient.CreateOrUpdateIndexAsync(definition);
        }
Beispiel #7
0
        public async Task CreateIndexAsync(string indexName)
        {
            Azure.Search.Documents.Indexes.FieldBuilder fieldBuilder = new Azure.Search.Documents.Indexes.FieldBuilder();
            var searchFields = fieldBuilder.Build(typeof(Hotel));

            var definition = new SearchIndex(indexName, searchFields);

            var suggester = new SearchSuggester("sg", new[] { "HotelName", "Category", "Address/City", "Address/StateProvince" });

            definition.Suggesters.Add(suggester);

            var index = await _indexClient.CreateOrUpdateIndexAsync(definition);
        }
Beispiel #8
0
        private Task CreateIndexAsync(string indexName)
        {
            var suggester = new SearchSuggester("sg", new[] { "Name", "FacilityName" });

            var definition = new SearchIndex(indexName)
            {
                Fields = new List <SearchField>
                {
                    new SearchField("Id", SearchFieldDataType.String)
                    {
                        IsKey = true
                    },
                    new SearchField("FacilityId", SearchFieldDataType.Int32)
                    {
                        IsFilterable = true
                    },
                    new SearchField("Name", SearchFieldDataType.String)
                    {
                        IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft
                    },
                    new SearchField("Description", SearchFieldDataType.String)
                    {
                        IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft
                    },
                    new SearchField("FacilityName", SearchFieldDataType.String)
                    {
                        IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft
                    },
                    new SearchField("FacilityDescription", SearchFieldDataType.String)
                    {
                        IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft
                    },
                    new SearchField("Location", SearchFieldDataType.GeographyPoint)
                    {
                        IsFilterable = true
                    },
                    new SearchField("RoomCount", SearchFieldDataType.Int32)
                    {
                        IsFilterable = true
                    },
                    new SearchField("Images", SearchFieldDataType.Collection(SearchFieldDataType.String))
                    {
                        IsFilterable = false
                    }
                }
            };

            definition.Suggesters.Add(suggester);

            return(client.CreateOrUpdateIndexAsync(definition));
        }
Beispiel #9
0
        public async Task RunAsync()
        {
            try
            {
                AnsiConsole.MarkupLine("[green]Seeding azure search...[/]");
                var fields = new FieldBuilder().Build(typeof(ProductDocument));
                var index  = new SearchIndex(nameof(ProductDocument).ToLower(), fields);
                await _searchIndexClient.CreateOrUpdateIndexAsync(index);

                var searchClient = _searchIndexClient.GetSearchClient(nameof(ProductDocument).ToLower());
                await Task.WhenAll(_seedData.Select(async product => await IndexProduct(searchClient, product)));
            }
            catch (Exception exception)
            {
                AnsiConsole.WriteException(exception, ExceptionFormats.ShortenEverything);
            }
            finally
            {
                AnsiConsole.MarkupLine("[green]Seeded azure search.[/]");
            }
        }
        public async Task <int> BuildIndexAsync(IEnumerable <SearchLocationIndex> searchLocations)
        {
            logger.LogInformation($"Starting to build index for {searchLocations.Count()}");
            try
            {
                var searchIndexClient = new SearchIndexClient(azureSearchIndexConfig.EndpointUri, GetAzureKeyCredential());
                var searchClient      = new SearchClient(azureSearchIndexConfig.EndpointUri, azureSearchIndexConfig.LocationSearchIndex, GetAzureKeyCredential());
                var fieldBuilder      = new FieldBuilder();
                var searchFields      = fieldBuilder.Build(typeof(SearchLocationIndex));
                var definition        = new SearchIndex(azureSearchIndexConfig.LocationSearchIndex, searchFields);
                var suggester         = new SearchSuggester(suggestorName, new[] { nameof(SearchLocationIndex.LocationName) });
                definition.Suggesters.Add(suggester);

                logger.LogInformation("created search objects and creating index");
                await searchIndexClient.CreateOrUpdateIndexAsync(definition).ConfigureAwait(false);

                logger.LogInformation("Created search index and uploading documents");

                var batch = IndexDocumentsBatch.Upload(searchLocations);
                IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch).ConfigureAwait(false);

                var failedRecords = result.Results.Where(r => !r.Succeeded);
                if (failedRecords.Any())
                {
                    var sampleFailedRecord = failedRecords.FirstOrDefault();
                    var sampleMessage      = $"{failedRecords.Count()} have failed to upload to the index, sample failed record  message {sampleFailedRecord.ErrorMessage}, Status = {sampleFailedRecord.Status}";
                    logger.LogError(sampleMessage);
                    throw new DfcIndexUploadException("sampleMessage");
                }

                logger.LogInformation($"Created search index and uploaded {result.Results.Count} documents");

                return(result.Results.Count);
            }
            catch (Exception ex)
            {
                logger.LogError("Building index had an error", ex);
                throw;
            }
        }
Beispiel #11
0
        //public static SearchIndex CreateSearchIndex(string key, string endpoint, string indexName, IList<SearchField> fields)
        //{
        //    // Create a service client
        //    AzureKeyCredential credential = new AzureKeyCredential(key);
        //    SearchIndexClient client = new SearchIndexClient(new Uri(endpoint), credential);

        //    // Create the index using FieldBuilder.
        //    SearchIndex index = new SearchIndex(indexName, fields);

        //    return client.CreateIndex(index).Value;



        public static async Task <SearchIndex> CreateSearchIndexAsync(string key, string endpoint, string indexName, IList <SearchField> fields)
        {
            // Create a service client
            AzureKeyCredential credential = new AzureKeyCredential(key);
            SearchIndexClient  client     = new SearchIndexClient(new Uri(endpoint), credential);

            // Create the index using FieldBuilder.
            SearchIndex index = new SearchIndex(indexName, fields);

            try
            {
                var result = await client.CreateOrUpdateIndexAsync(index);

                return(result.Value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to create index\n Exception message: {0}\n", ex.Message);
                ExitProgram("Cannot continue without a search index");
            }
            return(null);
        }
        static async Task CreateSearchResources(AppSettings settings)
        {
            SearchIndexClient indexClient = new SearchIndexClient(settings.SearchEndpointUri, settings.SearchKeyCredential);

            Console.WriteLine("Deleting search index {0} if exists...", SEARCH_ACL_INDEX_NAME);
            try
            {
                await indexClient.GetIndexAsync(SEARCH_ACL_INDEX_NAME);

                await indexClient.DeleteIndexAsync(SEARCH_ACL_INDEX_NAME);
            }
            catch (RequestFailedException)
            {
                // Index didn't exist - continue
            }

            Console.WriteLine("Creating search index {0}...", SEARCH_ACL_INDEX_NAME);
            await indexClient.CreateOrUpdateIndexAsync(
                new SearchIndex(SEARCH_ACL_INDEX_NAME, fields : new[]
            {
                new SearchField("key", SearchFieldDataType.String)
                {
                    IsKey = true
                },
                new SearchField("metadata_storage_path", SearchFieldDataType.String),
                new SearchField("content", SearchFieldDataType.String)
            }));

            Console.WriteLine("Creating search data source {0}...", SEARCH_ACL_DATASOURCE_NAME);
            SearchIndexerClient indexerClient = new SearchIndexerClient(settings.SearchEndpointUri, settings.SearchKeyCredential);
            await indexerClient.CreateOrUpdateDataSourceConnectionAsync(
                new SearchIndexerDataSourceConnection(
                    name : SEARCH_ACL_DATASOURCE_NAME,
                    type : SearchIndexerDataSourceType.AzureBlob,
                    connectionString : "ResourceId=" + settings.DataLakeResourceID,
                    container : new SearchIndexerDataContainer(name : DATA_LAKE_FILESYSTEM_NAME)));

            Console.WriteLine("Deleting search indexer {0} if exists...", SEARCH_ACL_INDEXER_NAME);
            try
            {
                await indexerClient.GetIndexerAsync(SEARCH_ACL_INDEXER_NAME);

                await indexerClient.DeleteIndexerAsync(SEARCH_ACL_INDEXER_NAME);
            }
            catch (RequestFailedException)
            {
                // Indexer didn't exist - continue
            }

            Console.WriteLine("Creating search indexer {0}...", SEARCH_ACL_INDEXER_NAME);
            await indexerClient.CreateIndexerAsync(
                new SearchIndexer(
                    name : SEARCH_ACL_INDEXER_NAME,
                    dataSourceName : SEARCH_ACL_DATASOURCE_NAME,
                    targetIndexName : SEARCH_ACL_INDEX_NAME)
            {
                Parameters = new IndexingParameters
                {
                    MaxFailedItems = -1,
                    IndexingParametersConfiguration = new IndexingParametersConfiguration
                    {
                        ParsingMode = BlobIndexerParsingMode.Text
                    }
                }
            });
        }
        static async Task Main(string[] args)
        {
            var serviceName = Environment.GetEnvironmentVariable("AzureSearchName");
            var indexName   = "models";
            var apiKey      = Environment.GetEnvironmentVariable("AzureSearchKey");

            var serviceEndpoint   = new Uri($"https://{serviceName}.search.windows.net/");
            var credential        = new AzureKeyCredential(apiKey);
            var searchIndexClient = new SearchIndexClient(serviceEndpoint, credential);
            var searchClient      = new SearchClient(serviceEndpoint, indexName, credential);

            if (AnsiConsole.Capabilities.SupportLinks)
            {
                AnsiConsole.MarkupLine(
                    $"[link=https://github.com/bovrhovn/azure-search-lang-synonym-sample/tree/main/src/ASDemo]Demo for index {indexName}[/]!");
            }

            var index = new SearchIndex(indexName)
            {
                Fields =
                {
                    new SimpleField("uniqueId",       SearchFieldDataType.String)
                    {
                        IsKey = true,                 IsFilterable= true, IsSortable = true
                    },
                    new SearchableField("name")
                    {
                        IsFilterable = true,          IsSortable = true
                    },
                    new SearchableField("polishName")
                    {
                        IsFilterable = true,          IsSortable = true
                    },
                    new SimpleField("updated",        SearchFieldDataType.DateTimeOffset)
                    {
                        IsFilterable = true,          IsSortable = true
                    },
                    new SearchableField("keyPhrases", true)
                    {
                        IsFilterable = true
                    }
                }
            };

            HorizontalRule("Creating index");

            AnsiConsole.WriteLine("Creating or updating index, if changed");
            await searchIndexClient.CreateOrUpdateIndexAsync(index);

            AnsiConsole.WriteLine("Operation completed!");

            var addingData = Environment.GetEnvironmentVariable("AddingData");

            bool.TryParse(addingData, out bool addData);

            if (addData)
            {
                HorizontalRule("Adding data to the index");
                AnsiConsole.WriteLine($"Adding data to the search index in {indexName}");
                await AddDataAsync(searchClient);

                AnsiConsole.WriteLine("Completed adding data");
            }

            HorizontalRule("Executing search queries");
            AnsiConsole.WriteLine("Query #1: Using OData queries");

            var queryManager = new QueryManager();
            var response     = await queryManager.QueryDataAsync(searchClient, "*", new SearchOptions
            {
                Filter            = "name eq 'styles'",
                IncludeTotalCount = true
            });

            WriteDocuments(response);

            HorizontalRule("Query #2: search with polish version");
            response = await queryManager.QueryDataAsync(searchClient, "plik", new SearchOptions
            {
                Filter            = "",
                OrderBy           = { "updated desc" },
                IncludeTotalCount = true
            });

            WriteDocuments(response);

            HorizontalRule("Query #3: search keywords");
            response = await queryManager.QueryDataAsync(searchClient, "EF04", new SearchOptions
            {
                OrderBy           = { "updated desc" },
                IncludeTotalCount = true
            });

            WriteDocuments(response);

            HorizontalRule("Query #4: fuzzy search");
            response = await queryManager.QueryDataAsync(searchClient, "sytles~", new SearchOptions
            {
                SearchMode        = SearchMode.Any,
                QueryType         = SearchQueryType.Full,
                OrderBy           = { "updated desc" },
                IncludeTotalCount = true
            });

            WriteDocuments(response);

            HorizontalRule("Query #5: fielded search");
            response = await queryManager.QueryDataAsync(searchClient, "keyPhrases:cycle OR keyPhrases:tag", new SearchOptions
            {
                SearchMode        = SearchMode.Any,
                QueryType         = SearchQueryType.Full,
                OrderBy           = { "updated desc" },
                IncludeTotalCount = true
            });

            WriteDocuments(response);

            AnsiConsole.WriteLine("--> press any field to exit");
            System.Console.Read();
        }
        // This sample shows how ETags work by performing conditional updates and deletes
        // on an Azure Search index.
        static async Task Main(string[] args)
        {
            IConfigurationBuilder builder       = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot    configuration = builder.Build();

            SearchIndexClient indexClient = CreateSearchIndexClient(configuration);

            Console.WriteLine("Deleting index...\n");
            DeleteTestIndexIfExists(indexClient);

            // Every top-level resource in Azure Search has an associated ETag that keeps track of which version
            // of the resource you're working on. When you first create a resource such as an index, its ETag is
            // empty.
            SearchIndex index = DefineTestIndex();

            Console.WriteLine(
                $"Test searchIndex hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");

            // Once the resource exists in Azure Search, its ETag will be populated. Make sure to use the object
            // returned by the SearchIndexClient! Otherwise, you will still have the old object with the
            // blank ETag.
            //Console.WriteLine("Creating index...\n");
            index = indexClient.CreateIndex(index);
            Console.WriteLine($"Test index created; Its ETag should be populated. ETag: '{index.ETag}'");


            // ETags let you do some useful things you couldn't do otherwise. For example, by using an If-Match
            // condition, we can update an index using CreateOrUpdateIndexAsync() and be guaranteed that the update will only
            // succeed if the index already exists.
            index.Fields.Add(new SearchField("name", SearchFieldDataType.String)
            {
                AnalyzerName = LexicalAnalyzerName.EnMicrosoft
            });
            index = indexClient.CreateOrUpdateIndex(index);

            index = await indexClient.CreateOrUpdateIndexAsync(index);

            Console.WriteLine(
                $"Test searchIndex updated; Its ETag should have changed since it was created. ETag: '{index.ETag}'");

            // More importantly, ETags protect you from concurrent updates to the same resource. If another
            // client tries to update the resource, it will fail as long as all clients are using the right
            // access conditions.
            SearchIndex indexForClientUpdate       = index;
            SearchIndex indexForClientUpdateFailed = indexClient.GetIndex("test");

            Console.WriteLine("Simulating concurrent update. To start, both clients see the same ETag.");
            Console.WriteLine($"ClientUpdate ETag: '{indexForClientUpdate.ETag}' ClientUpdateFailed ETag: '{indexForClientUpdateFailed.ETag}'");

            // indexForClientUpdate successfully updates the index.
            indexForClientUpdate.Fields.Add(new SearchField("a", SearchFieldDataType.Int32));
            indexForClientUpdate = indexClient.CreateOrUpdateIndex(indexForClientUpdate);

            Console.WriteLine($"Test index updated by ClientUpdate; ETag: '{indexForClientUpdate.ETag}'");

            // indexForClientUpdateFailed tries to update the index, but fails, thanks to the ETag check.
            try
            {
                indexForClientUpdateFailed.Fields.Add(new SearchField("b", SearchFieldDataType.Boolean));
                indexClient.CreateOrUpdateIndex(indexForClientUpdateFailed);

                Console.WriteLine("Whoops; This shouldn't happen");
                Environment.Exit(1);
            }
            catch (RequestFailedException e) when(e.Status == 400)
            {
                Console.WriteLine("ClientUpdateFailed failed to update the index, as expected.");
            }

            // You can also use access conditions with Delete operations. For example, you can implement an
            // atomic version of the DeleteTestIndexIfExists method from this sample like this:
            Console.WriteLine("Deleting index...\n");
            indexClient.DeleteIndex("test");

            // This is slightly better than using the Exists method since it makes only one round trip to
            // Azure Search instead of potentially two. It also avoids an extra Delete request in cases where
            // the resource is deleted concurrently, but this doesn't matter much since resource deletion in
            // Azure Search is idempotent.

            // And we're done! Bye!
            Console.WriteLine("Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }
        public void CreateDataSource()
        {
            //creating index
            SearchIndexClient   indexClient   = new SearchIndexClient(Uri, keyCredential);
            SearchIndexerClient indexerClient = new SearchIndexerClient(Uri, keyCredential);

            Console.WriteLine("Creating index...");
            FieldBuilder fieldBuilder = new FieldBuilder();
            var          searchFields = fieldBuilder.Build(typeof(InteractionDetails));
            var          searchIndex  = new SearchIndex("interaction-sql-idx", searchFields);

            // If we have run the sample before, this index will be populated
            // We can clear the index by deleting it if it exists and creating
            // it again
            CleanupSearchIndexClientResources(indexClient, searchIndex);

            indexClient.CreateOrUpdateIndexAsync(searchIndex);
            //Creating data source

            Console.WriteLine("Creating data source...");

            var dataSource =
                new SearchIndexerDataSourceConnection(
                    "interaction-sql-ds",
                    SearchIndexerDataSourceType.AzureSql,
                    azureSQLConnectionStr,
                    new SearchIndexerDataContainer("[Interaction]"));

            indexerClient.CreateOrUpdateDataSourceConnection(dataSource);

            //Creating indexer
            Console.WriteLine("Creating Azure SQL indexer...");

            //var schedule = new IndexingSchedule(TimeSpan.FromDays(1))
            //{
            //    StartTime = DateTimeOffset.Now
            //};

            var parameters = new IndexingParameters()
            {
                BatchSize              = 100,
                MaxFailedItems         = 0,
                MaxFailedItemsPerBatch = 0
            };



            // Indexer declarations require a data source and search index.
            // Common optional properties include a schedule, parameters, and field mappings
            // The field mappings below are redundant due to how the Hotel class is defined, but
            // we included them anyway to show the syntax
            var indexer = new SearchIndexer("interaction-sql-idxr", dataSource.Name, searchIndex.Name)
            {
                Description = "Data indexer",
                Schedule    = new IndexingSchedule(TimeSpan.FromMinutes(5)),
                Parameters  = parameters,
            };

            indexerClient.CreateOrUpdateIndexerAsync(indexer);

            Console.WriteLine("Running Azure SQL indexer...");

            try
            {
                indexerClient.RunIndexerAsync(indexer.Name);
            }
            catch (CloudException e) when(e.Response.StatusCode == (HttpStatusCode)429)
            {
                Console.WriteLine("Failed to run indexer: {0}", e.Response.Content);
            }

            // Wait 5 seconds for indexing to complete before checking status
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(5000);
        }