Exemplo n.º 1
0
        private static async Task DeleteIndexIfExistsAsync(string indexName, SearchIndexClient indexClient)
        {
            try
            {
                await indexClient.GetIndexAsync(indexName);

                await indexClient.DeleteIndexAsync(indexName);
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                //if the specified index not exist, 404 will be thrown.
            }
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get a function to access the key field of a search document.
        /// </summary>
        /// <param name="async">Whether to run sync or async.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A Task representing the operation.</returns>
        private async Task GetKeyFieldAccessorAsync(bool async, CancellationToken cancellationToken)
        {
            // Case 1: The user provided an explicit accessor and we're done
            if (KeyFieldAccessor != null)
            {
                return;
            }

            // Case 2: Infer the accessor from FieldBuilder
            try
            {
                FieldBuilder builder = new FieldBuilder {
                    Serializer = SearchClient.Serializer
                };
                IDictionary <string, SearchField>  fields   = builder.BuildMapping(typeof(T));
                KeyValuePair <string, SearchField> keyField = fields.FirstOrDefault(pair => pair.Value.IsKey == true);
                if (!keyField.Equals(default(KeyValuePair <string, SearchField>)))
                {
                    KeyFieldAccessor = CompileAccessor(keyField.Key);
                    return;
                }
            }
            catch
            {
                // Ignore any errors because this type might not have been
                // designed with FieldBuilder in mind
            }

            // Case 3: Fetch the index to find the key
            Exception failure = null;

            try
            {
                // Call the service to find the name of the key
                SearchIndexClient indexClient = SearchClient.GetSearchIndexClient();
                SearchIndex       index       = async ?
                                                await indexClient.GetIndexAsync(IndexName, cancellationToken).ConfigureAwait(false) :
                                                indexClient.GetIndex(IndexName, cancellationToken);

                SearchField keyField = index.Fields.Single(f => f.IsKey == true);
                string      key      = keyField.Name;

                if (typeof(T).IsAssignableFrom(typeof(SearchDocument)))
                {
                    // Case 3a: If it's a dynamic SearchDocument, lookup
                    // the name of the key in the dictionary
                    KeyFieldAccessor = (T doc) => (doc as SearchDocument)?.GetString(key);
                    return;
                }
                else
                {
                    // Case 3b: We'll see if there's a property with the
                    // same name and use that as the accessor
                    if (typeof(T).GetProperty(key) != null ||
                        typeof(T).GetField(key) != null)
                    {
                        KeyFieldAccessor = CompileAccessor(key);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                // We'll provide any exceptions as a hint because it could
                // be something like using the wrong API Key type when
                // moving from SearchClient up to SearchIndexClient that
                // potentially could be addressed if the user really wanted
                failure = ex;
            }

            // Case 4: Throw and tell the user to provide an explicit accessor.
            throw new InvalidOperationException(
                      $"Failed to discover the Key field of document type {typeof(T).Name} for Azure Cognitive Search index {IndexName}.  " +
                      $"Please set {typeof(SearchIndexingBufferedSenderOptions<T>).Name}.{nameof(SearchIndexingBufferedSenderOptions<T>.KeyFieldAccessor)} explicitly.",
                      failure);
        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
                    }
                }
            });
        }
Exemplo n.º 5
0
        public async Task <SearchIndex> GetIndexAsync(string indexName)
        {
            var index = await _indexClient.GetIndexAsync(indexName);

            return(index.Value);
        }