コード例 #1
0
        private void InitializeConcertsIndex(SearchIndexClient serviceClient)
        {
            // Create the index that will contain the searchable data from the concerts.
            var concertsIndex = new SearchIndex(IndexNameConcerts)
            {
                Fields = new[]
                {
                    new SearchField(nameof(Concert.Id), SearchFieldDataType.String)
                    {
                        IsKey        = true,
                        IsSearchable = false
                    },
                    new SearchField(nameof(Concert.Artist), SearchFieldDataType.String)
                    {
                        AnalyzerName = LexicalAnalyzerName.EnMicrosoft,
                        IsSearchable = true,
                    },
                    new SearchField(nameof(Concert.Genre), SearchFieldDataType.String)
                    {
                        AnalyzerName = LexicalAnalyzerName.EnMicrosoft,
                        IsSearchable = true,
                        IsFilterable = true,
                        IsFacetable  = true
                    },
                    new SearchField(nameof(Concert.Location), SearchFieldDataType.String)
                    {
                        AnalyzerName = LexicalAnalyzerName.EnMicrosoft,
                        IsSearchable = true,
                        IsFilterable = true,
                        IsFacetable  = true
                    },
                    new SearchField(nameof(Concert.Title), SearchFieldDataType.String)
                    {
                        AnalyzerName = LexicalAnalyzerName.EnMicrosoft,
                        IsSearchable = true,
                    },
                    new SearchField(nameof(Concert.Description), SearchFieldDataType.String)
                    {
                        AnalyzerName = LexicalAnalyzerName.EnMicrosoft,
                        IsSearchable = true,
                    },
                    new SearchField(nameof(Concert.Price), SearchFieldDataType.Double)
                    {
                        IsSearchable = false,
                        IsFilterable = true,
                        IsFacetable  = true,
                        IsSortable   = true,
                    },
                    new SearchField(nameof(Concert.StartTime), SearchFieldDataType.DateTimeOffset)
                    {
                        IsSearchable = false,
                        IsSortable   = true,
                        IsFilterable = true
                    },
                },
                DefaultScoringProfile = "default-scoring",
            };

            var suggester = new SearchSuggester("default-suggester", new[] { nameof(Concert.Artist), nameof(Concert.Location), nameof(Concert.Title) });

            concertsIndex.Suggesters.Add(suggester);
            concertsIndex.ScoringProfiles.Add(new ScoringProfile("default-scoring")
            {
                // Add a lot of weight to the artist and above average weight to the title.
                TextWeights = new TextWeights(new Dictionary <string, double> {
                    { nameof(Concert.Artist), 2.0 },
                    { nameof(Concert.Title), 1.5 }
                })
            });

            serviceClient.CreateOrUpdateIndex(concertsIndex);

            var searchIndexerClient = new SearchIndexerClient(this.searchServiceUri, this.azureKeyCredential);

            // Create the data source that connects to the SQL Database account containing the consult requests.
            var concertsDataSource = new SearchIndexerDataSourceConnection(IndexNameConcerts, SearchIndexerDataSourceType.AzureSql, this.concertsSqlDatabaseConnectionString, new SearchIndexerDataContainer("Concerts"))
            {
                DataChangeDetectionPolicy = new SqlIntegratedChangeTrackingPolicy()
            };

            searchIndexerClient.CreateOrUpdateDataSourceConnection(concertsDataSource);

            // Create the indexer that will pull the data from the database into the search index.
            var concertsIndexer = new SearchIndexer(name: IndexNameConcerts, dataSourceName: IndexNameConcerts, targetIndexName: IndexNameConcerts)
            {
                Schedule = new IndexingSchedule(TimeSpan.FromMinutes(5))
            };

            searchIndexerClient.CreateOrUpdateIndexer(concertsIndexer);
        }