Ejemplo n.º 1
0
        private static async Task <bool> DeleteIndexingResources()
        {
            Console.WriteLine("Deleting Data Source, Index, Indexer, Skillset and SynonymMap if they exist...");
            try
            {
                await _searchIndexerClient.DeleteDataSourceConnectionAsync(DataSourceName);

                await _searchIndexClient.DeleteIndexAsync(IndexName);

                await _searchIndexerClient.DeleteIndexerAsync(IndexerName);

                await _searchIndexerClient.DeleteSkillsetAsync(SkillsetName);

                await _searchIndexClient.DeleteSynonymMapAsync(SynonymMapName);
            }
            catch (Exception ex)
            {
                if (DebugMode)
                {
                    Console.WriteLine("Error deleting resources: {0}", ex.Message);
                }
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public static async Task DeleteIndexerIfExistsAsync(string name, SearchIndexerClient idxclient)
        {
            Console.WriteLine($"Deleting search indexer {name}...");

            await idxclient.GetIndexerNamesAsync();

            {
                await idxclient.DeleteIndexerAsync(name);
            }
        }
Ejemplo n.º 3
0
        public async Task Upload()
        {
            var existingSearchTerms = (await _tableStorage.GetAllAsync <GlobalSearchTerm>("searchTerms")).ToList();

            var i = 1;

            Parallel.ForEach(existingSearchTerms, new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            }, async existingSearchTerm =>
            {
                await _tableStorage.DeleteAsync("searchTerms", existingSearchTerm);
                Console.WriteLine($"Deleted {i} of {existingSearchTerms.Count} existing search terms.");
                i++;
            });

            foreach (var globalSearchTerm in _globalSearchTermRepository.SearchTerms)
            {
                globalSearchTerm.LanguageEnum = _localization.Language;
            }

            await _tableStorage.AddBatchAsync <GlobalSearchTerm>("searchTerms",
                                                                 _globalSearchTermRepository.SearchTerms.Where(s =>
                                                                                                               s.PartitionKey == ContentType.ExpandedContent.ToString()),
                                                                 new BatchOperationOptions { BatchInsertMethod = BatchInsertMethod.Insert });

            await _tableStorage.AddBatchAsync <GlobalSearchTerm>("searchTerms",
                                                                 _globalSearchTermRepository.SearchTerms.Where(s =>
                                                                                                               s.PartitionKey == ContentType.Core.ToString()),
                                                                 new BatchOperationOptions { BatchInsertMethod = BatchInsertMethod.Insert });

            var index = await _searchIndexClient.GetIndexAsync("searchterms-index");

            await _searchIndexClient.DeleteIndexAsync("searchterms-index");

            await _searchIndexClient.CreateIndexAsync(index);

            var oldIndexer = await _searchIndexerClient.GetIndexerAsync("searchterms-indexer");

            await _searchIndexerClient.DeleteIndexerAsync(oldIndexer);

            await _searchIndexerClient.CreateIndexerAsync(oldIndexer);

            await _searchIndexerClient.RunIndexerAsync("searchterms-indexer");
        }
Ejemplo n.º 4
0
        public async Task CreateDoubleEncryptedIndex()
        {
            string keyVaultUrl = TestEnvironment.KeyVaultUrl;

            if (string.IsNullOrEmpty(keyVaultUrl))
            {
                Assert.Ignore("A Key Vault was not deployed");
            }

            // Create the key and persist the name and version.
            KeyVaultKey key = await CreateEncryptionKey(keyVaultUrl);

            Environment.SetEnvironmentVariable("KEYVAULT_URL", keyVaultUrl);
            Environment.SetEnvironmentVariable("KEYVAULT_KEY_NAME", key.Name);
            Environment.SetEnvironmentVariable("KEYVAULT_KEY_VERSION", key.Properties.Version);

            // Persist the service principal.
            Environment.SetEnvironmentVariable("APPLICATION_ID", TestEnvironment.ClientId);
            Environment.SetEnvironmentVariable("APPLICATION_SECRET", TestEnvironment.RecordedClientSecret);

            // Create the blob container and persist connection information.
            await using SearchResources resources = await SearchResources.CreateWithBlobStorageAndIndexAsync(this, populate : true);

            Environment.SetEnvironmentVariable("STORAGE_CONNECTION_STRING", resources.StorageAccountConnectionString);
            Environment.SetEnvironmentVariable("STORAGE_CONTAINER_NAME", resources.BlobContainerName);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            // Define clean up tasks to be invoked in reverse order added.
            Stack <Func <Task> > cleanUpTasks = new Stack <Func <Task> >();

            try
            {
                #region Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Index
                // Create a credential to connect to Key Vault and use a specific key version created previously.
                SearchResourceEncryptionKey encryptionKey = new SearchResourceEncryptionKey(
                    new Uri(Environment.GetEnvironmentVariable("KEYVAULT_URL")),
                    Environment.GetEnvironmentVariable("KEYVAULT_KEY_NAME"),
                    Environment.GetEnvironmentVariable("KEYVAULT_KEY_VERSION"))
                {
                    ApplicationId     = Environment.GetEnvironmentVariable("APPLICATION_ID"),
                    ApplicationSecret = Environment.GetEnvironmentVariable("APPLICATION_SECRET"),
                };

                // Create a connection to our storage blob container using the credential.
                string dataSourceConnectionName = "hotels-data-source";
#if !SNIPPET
                dataSourceConnectionName = Recording.Random.GetName();
#endif
                SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection(
                    dataSourceConnectionName,
                    SearchIndexerDataSourceType.AzureBlob,
                    Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"),
                    new SearchIndexerDataContainer(
                        Environment.GetEnvironmentVariable("STORAGE_CONTAINER_NAME")
                        )
                    )
                {
                    EncryptionKey = encryptionKey
                };

                // Create an indexer to process documents from the blob container into the index.
                // You can optionally configure a skillset to use cognitive services when processing documents.
                // Set the SearchIndexerSkillset.EncryptionKey to the same credential if you use a skillset.
                string indexName   = "hotels";
                string indexerName = "hotels-indexer";
#if !SNIPPET
                indexName   = resources.IndexName;
                indexerName = Recording.Random.GetName();
#endif
                SearchIndexer indexer = new SearchIndexer(
                    indexerName,
                    dataSourceConnectionName,
                    indexName)
                {
                    EncryptionKey = encryptionKey,

                    // Map the fields in our documents we want to index.
                    FieldMappings =
                    {
                        new FieldMapping("hotelId"),
                        new FieldMapping("hotelName"),
                        new FieldMapping("description"),
                        new FieldMapping("tags"),
                        new FieldMapping("address")
                    },
                    Parameters = new IndexingParameters
                    {
                        // Tell the indexer to parse each blob as a separate JSON document.
                        IndexingParametersConfiguration = new IndexingParametersConfiguration
                        {
                            ParsingMode = BlobIndexerParsingMode.Json
                        }
                    }
                };

                // Now connect to our Search service and set up the data source and indexer.
                // Documents already in the storage blob will begin indexing immediately.
                Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
                AzureKeyCredential credential = new AzureKeyCredential(
                    Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

                SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential);
#if !SNIPPET
                indexerClient = resources.GetIndexerClient();
#endif
                indexerClient.CreateDataSourceConnection(dataSourceConnection);
#if !SNIPPET
                cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName));
#endif
                indexerClient.CreateIndexer(indexer);
#if !SNIPPET
                cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName));
#endif
                #endregion Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Index

                await WaitForIndexingAsync(indexerClient, indexerName);

                #region Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Query
                // Create a SearchClient and search for luxury hotels. In production, be sure to use the query key.
                SearchClient searchClient = new SearchClient(endpoint, "hotels", credential);
#if !SNIPPET
                searchClient = resources.GetSearchClient();
                bool found = false;
#endif
                Response <SearchResults <Hotel> > results = searchClient.Search <Hotel>("luxury hotels");
                foreach (SearchResult <Hotel> result in results.Value.GetResults())
                {
                    Hotel hotel = result.Document;
#if !SNIPPET
                    found = true;
#endif

                    Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})");
                    Console.WriteLine($"  Description: {hotel.Description}");
                }
                #endregion Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Query

                Assert.IsTrue(found, "No luxury hotels were found in index");
            }
            finally
            {
                // We want to await these individual to create a deterministic order for playing back tests.
                foreach (Func <Task> cleanUpTask in cleanUpTasks)
                {
                    await cleanUpTask();
                }
            }
        }
        public async Task CreateIndexerAsync()
        {
            await using SearchResources resources = await SearchResources.CreateWithBlobStorageAsync(this, populate : true);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);
            Environment.SetEnvironmentVariable("STORAGE_CONNECTION_STRING", resources.StorageAccountConnectionString);
            Environment.SetEnvironmentVariable("STORAGE_CONTAINER", resources.BlobContainerName);
            Environment.SetEnvironmentVariable("COGNITIVE_SERVICES_KEY", resources.CognitiveServicesKey);

            // Define clean up tasks to be invoked in reverse order added.
            Stack <Func <Task> > cleanUpTasks = new Stack <Func <Task> >();

            try
            {
                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateSynonymMap
                // Create a new SearchIndexClient
                Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
                AzureKeyCredential credential = new AzureKeyCredential(
                    Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
                SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential);
#if !SNIPPET
                indexClient = resources.GetIndexClient(new SearchClientOptions());
#endif

                // Create a synonym map from a file containing country names and abbreviations
                // using the Solr format with entry on a new line using \n, for example:
                // United States of America,US,USA\n
                string synonymMapName = "countries";
#if !SNIPPET
                synonymMapName = Recording.Random.GetName();
#endif
                string synonymMapPath = "countries.txt";
#if !SNIPPET
                synonymMapPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Samples", "countries.txt");
#endif

                SynonymMap synonyms;
#if SNIPPET
                using (StreamReader file = File.OpenText(synonymMapPath))
                {
                    synonyms = new SynonymMap(synonymMapName, file);
                }
#else
                synonyms = new SynonymMap(synonymMapName, CountriesSolrSynonymMap);
#endif

                await indexClient.CreateSynonymMapAsync(synonyms);

                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateSynonymMap

                // Make sure our synonym map gets deleted, which is not deleted when our
                // index is deleted when our SearchResources goes out of scope.
                cleanUpTasks.Push(() => indexClient.DeleteSynonymMapAsync(synonymMapName));

                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndex
                // Create the index
                string indexName = "hotels";
#if !SNIPPET
                indexName = Recording.Random.GetName();
#endif
                SearchIndex index = new SearchIndex(indexName)
                {
                    Fields =
                    {
                        new SimpleField("hotelId",                    SearchFieldDataType.String)
                        {
                            IsKey = true,                             IsFilterable = true, IsSortable  = true
                        },
                        new SearchableField("hotelName")
                        {
                            IsFilterable = true,                      IsSortable   = true
                        },
                        new SearchableField("description")
                        {
                            AnalyzerName = LexicalAnalyzerName.EnLucene
                        },
                        new SearchableField("descriptionFr")
                        {
                            AnalyzerName = LexicalAnalyzerName.FrLucene
                        },
                        new SearchableField("tags",                   collection: true)
                        {
                            IsFilterable = true,                      IsFacetable  = true
                        },
                        new ComplexField("address")
                        {
                            Fields =
                            {
                                new SearchableField("streetAddress"),
                                new SearchableField("city")
                                {
                                    IsFilterable = true,             IsSortable                 = true, IsFacetable = true
                                },
                                new SearchableField("stateProvince")
                                {
                                    IsFilterable = true,             IsSortable                 = true, IsFacetable = true
                                },
                                new SearchableField("country")
                                {
                                    SynonymMapNames = new[] { synonymMapName },IsFilterable               = true, IsSortable  = true,IsFacetable          = true
                                },
                                new SearchableField("postalCode")
                                {
                                    IsFilterable = true,             IsSortable                 = true, IsFacetable = true
                                }
                            }
                        }
                    }
                };

                await indexClient.CreateIndexAsync(index);

                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndex

                // Make sure our synonym map gets deleted, which is not deleted when our
                // index is deleted when our SearchResources goes out of scope.
                cleanUpTasks.Push(() => indexClient.DeleteIndexAsync(indexName));

                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateDataSourceConnection
                // Create a new SearchIndexerClient
                SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential);
#if !SNIPPET
                indexerClient = resources.GetIndexerClient();
#endif

                string dataSourceConnectionName = "hotels";
#if !SNIPPET
                dataSourceConnectionName = Recording.Random.GetName();
#endif
                SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection(
                    dataSourceConnectionName,
                    SearchIndexerDataSourceType.AzureBlob,
                    Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"),
                    new SearchIndexerDataContainer(Environment.GetEnvironmentVariable("STORAGE_CONTAINER")));

                await indexerClient.CreateDataSourceConnectionAsync(dataSourceConnection);

                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateDataSourceConnection

                // Make sure our data source gets deleted, which is not deleted when our
                // index is deleted when our SearchResources goes out of scope.
                cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName));

                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Skillset
                // Translate English descriptions to French.
                // See https://docs.microsoft.com/azure/search/cognitive-search-skill-text-translation for details of the Text Translation skill.
                TextTranslationSkill translationSkill = new TextTranslationSkill(
                    inputs: new[]
                {
                    new InputFieldMappingEntry("text")
                    {
                        Source = "/document/description"
                    }
                },
                    outputs: new[]
                {
                    new OutputFieldMappingEntry("translatedText")
                    {
                        TargetName = "descriptionFrTranslated"
                    }
                },
                    TextTranslationSkillLanguage.Fr)
                {
                    Name    = "descriptionFrTranslation",
                    Context = "/document",
                    DefaultFromLanguageCode = TextTranslationSkillLanguage.En
                };

                // Use the human-translated French description if available; otherwise, use the translated description.
                // See https://docs.microsoft.com/azure/search/cognitive-search-skill-conditional for details of the Conditional skill.
                ConditionalSkill conditionalSkill = new ConditionalSkill(
                    inputs: new[]
                {
                    new InputFieldMappingEntry("condition")
                    {
                        Source = "= $(/document/descriptionFr) == null"
                    },
                    new InputFieldMappingEntry("whenTrue")
                    {
                        Source = "/document/descriptionFrTranslated"
                    },
                    new InputFieldMappingEntry("whenFalse")
                    {
                        Source = "/document/descriptionFr"
                    }
                },
                    outputs: new[]
                {
                    new OutputFieldMappingEntry("output")
                    {
                        TargetName = "descriptionFrFinal"
                    }
                })
                {
                    Name    = "descriptionFrConditional",
                    Context = "/document",
                };

                // Create a SearchIndexerSkillset that processes those skills in the order given below.
                string skillsetName = "translations";
#if !SNIPPET
                skillsetName = Recording.Random.GetName();
#endif
                SearchIndexerSkillset skillset = new SearchIndexerSkillset(
                    skillsetName,
                    new SearchIndexerSkill[] { translationSkill, conditionalSkill })
                {
                    CognitiveServicesAccount = new CognitiveServicesAccountKey(
                        Environment.GetEnvironmentVariable("COGNITIVE_SERVICES_KEY")),
                    KnowledgeStore = new SearchIndexerKnowledgeStore(
                        Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"),
                        new List <SearchIndexerKnowledgeStoreProjection>()),
                };

                await indexerClient.CreateSkillsetAsync(skillset);

                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Skillset

                // Make sure our skillset gets deleted, which is not deleted when our
                // index is deleted when our SearchResources goes out of scope.
                cleanUpTasks.Push(() => indexerClient.DeleteSkillsetAsync(skillsetName));

                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndexer
                string indexerName = "hotels";
#if !SNIPPET
                indexerName = Recording.Random.GetName();
#endif
                SearchIndexer indexer = new SearchIndexer(
                    indexerName,
                    dataSourceConnectionName,
                    indexName)
                {
                    // We only want to index fields defined in our index, excluding descriptionFr if defined.
                    FieldMappings =
                    {
                        new FieldMapping("hotelId"),
                        new FieldMapping("hotelName"),
                        new FieldMapping("description"),
                        new FieldMapping("tags"),
                        new FieldMapping("address")
                    },
                    OutputFieldMappings =
                    {
                        new FieldMapping("/document/descriptionFrFinal")
                        {
                            TargetFieldName = "descriptionFr"
                        }
                    },
                    Parameters = new IndexingParameters
                    {
                        // Tell the indexer to parse each blob as a separate JSON document.
                        IndexingParametersConfiguration = new IndexingParametersConfiguration
                        {
                            ParsingMode = BlobIndexerParsingMode.Json
                        }
                    },
                    SkillsetName = skillsetName
                };

                // Create the indexer which, upon successful creation, also runs the indexer.
                await indexerClient.CreateIndexerAsync(indexer);

                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndexer

                // Make sure our indexer gets deleted, which is not deleted when our
                // index is deleted when our SearchResources goes out of scope.
                cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName));

                // Wait till the indexer is done.
                await WaitForIndexingAsync(indexerClient, indexerName);

                #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Query
                // Get a SearchClient from the SearchIndexClient to share its pipeline.
                SearchClient searchClient = indexClient.GetSearchClient(indexName);
#if !SNIPPET
                searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions()));
#endif

                // Query for hotels with an ocean view.
                SearchResults <Hotel> results = await searchClient.SearchAsync <Hotel>("ocean view");

#if !SNIPPET
                bool found = false;
#endif
                await foreach (SearchResult <Hotel> result in results.GetResultsAsync())
                {
                    Hotel hotel = result.Document;
#if !SNIPPET
                    if (hotel.HotelId == "6")
                    {
                        Assert.IsNotNull(hotel.DescriptionFr); found = true;
                    }
#endif

                    Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})");
                    Console.WriteLine($"  Description (English): {hotel.Description}");
                    Console.WriteLine($"  Description (French):  {hotel.DescriptionFr}");
                }
                #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Query

                Assert.IsTrue(found, "Expected hotel #6 not found in search results");
            }
            finally
            {
                // We want to await these individual to create a deterministic order for playing back tests.
                foreach (Func <Task> cleanUpTask in cleanUpTasks)
                {
                    await cleanUpTask();
                }
            }
        }
        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
                    }
                }
            });
        }
Ejemplo n.º 7
0
        public static async Task <SearchIndexer> CreateIndexerAsync(SearchIndexerClient indexerClient, SearchIndexerDataSourceConnection dataSource, SearchIndexerSkillset skillSet, SearchIndex index)
        {
            IndexingParameters indexingParameters = new IndexingParameters()
            {
                MaxFailedItems         = -1,
                MaxFailedItemsPerBatch = -1,
            };

            indexingParameters.IndexingParametersConfiguration = new IndexingParametersConfiguration();
            indexingParameters.IndexingParametersConfiguration.DataToExtract = BlobIndexerDataToExtract.ContentAndMetadata;
            indexingParameters.IndexingParametersConfiguration.ParsingMode   = BlobIndexerParsingMode.Text;

            string        indexerName = index.Name + "-indexer";
            SearchIndexer indexer     = new SearchIndexer(indexerName, dataSource.Name, index.Name)
            {
                Description  = index.Name + " Indexer",
                SkillsetName = skillSet.Name,
                Parameters   = indexingParameters
            };

            FieldMappingFunction mappingFunction = new FieldMappingFunction("base64Encode");

            mappingFunction.Parameters.Add("useHttpServerUtilityUrlTokenEncode", true);

            indexer.FieldMappings.Add(new FieldMapping("metadata_storage_path")
            {
                TargetFieldName = "metadata_storage_path",
                MappingFunction = mappingFunction
            });

            //indexer.FieldMappings.Add(new FieldMapping("metadata_storage_name")
            //{
            //    TargetFieldName = "FileName"
            //});

            //indexer.FieldMappings.Add(new FieldMapping("content")5
            //{
            //    TargetFieldName = "Content"
            //});

            //indexer.OutputFieldMappings.Add(new FieldMapping("/document/pages/*/organizations/*")
            //{
            //    TargetFieldName = "organizations"
            //});
            //indexer.OutputFieldMappings.Add(new FieldMapping("/document/pages/*/keyPhrases/*")
            //{
            //    TargetFieldName = "keyPhrases"
            //});
            //indexer.OutputFieldMappings.Add(new FieldMapping("/document/languageCode")
            //{
            //    TargetFieldName = "languageCode"
            //});

            try
            {
                await indexerClient.GetIndexerAsync(indexer.Name);

                await indexerClient.DeleteIndexerAsync(indexer.Name);
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                //if the specified indexer not exist, 404 will be thrown.
            }

            try
            {
                await indexerClient.CreateIndexerAsync(indexer);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Failed to create the indexer\n Exception message: {0}\n", ex.Message);
                ExitProgram("Cannot continue without creating an indexer");
            }

            return(indexer);
        }