예제 #1
0
        public string CreateIndex()
        {
            // Create the Azure Search index based on the included schema
            try
            {
                //Create the index definition
                var definition = new Index()
                {
                    Name   = _indexName,
                    Fields = new[]
                    {
                        new Field("DocumentID", DataType.String)
                        {
                            IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true
                        },
                        new Field("NodeAliasPath", DataType.String)
                        {
                            IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true
                        },
                        new Field("QuoteAuthor", DataType.String)
                        {
                            IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true
                        },
                        new Field("QuoteText", DataType.String)
                        {
                            IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true
                        },
                        new Field("QuoteDate", DataType.String)
                        {
                            IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true
                        }
                    }
                };

                definition.Suggesters = new List <Suggester> {
                    new Suggester()
                    {
                        Name         = "quoteauthor",
                        SearchMode   = SuggesterSearchMode.AnalyzingInfixMatching,
                        SourceFields = new List <string> {
                            "QuoteAuthor"
                        }
                    }
                };

                List <ScoringProfile> lstScoringProfiles = new List <ScoringProfile>();
                TextWeights           twauthor           = new TextWeights();
                twauthor.Weights.Add("QuoteAuthor", 100);
                TextWeights twtext = new TextWeights();
                twtext.Weights.Add("QuoteText", 100);

                lstScoringProfiles.Add(new ScoringProfile()
                {
                    Name        = "QuoteAuthor",
                    TextWeights = twauthor
                });
                lstScoringProfiles.Add(new ScoringProfile()
                {
                    Name        = "QuoteText",
                    TextWeights = twtext
                });

                definition.ScoringProfiles = lstScoringProfiles;



                _searchClient.Indexes.Create(definition);



                return("Index created!");
            }
            catch (Exception ex)
            {
                return("There was an issue creating the index: {0}\r\n" + ex.Message.ToString());
            }
        }
예제 #2
0
        private DocumentSearchResult <Account> BoostLastName(SearchServiceClient serviceClient)
        {
            //var textWeights = new TextWeights();

            //textWeights.Weights = new Dictionary<string, double>();
            //textWeights.Weights.Add("firstName", 2);
            //textWeights.Weights.Add("lastName", 5);

            //var accountIndexDefinition = new Index
            //{
            //    Name = indexName,
            //    Fields = FieldBuilder.BuildForType<Account>()
            //};

            //accountIndexDefinition.ScoringProfiles = new List<ScoringProfile>
            //{
            //    new ScoringProfile
            //    {
            //        Name = "boostLastName",
            //        TextWeights = textWeights
            //    }
            //};

            //serviceClient.Indexes.CreateOrUpdate(accountIndexDefinition);

            //DocumentSearchResult<Account> results;

            try
            {
                var index = serviceClient.Indexes.Get(indexName);
                #region Add Text Weights
                var textWeights = new TextWeights();

                textWeights.Weights = new Dictionary <string, double>();
                textWeights.Weights.Add("firstName", 2);
                textWeights.Weights.Add("lastName", 5);

                index.ScoringProfiles = new List <ScoringProfile>
                {
                    new ScoringProfile
                    {
                        Name        = "boostLastName",
                        TextWeights = textWeights
                    }
                };
                serviceClient.Indexes.CreateOrUpdate(index);
                #endregion
                //Since we are updating the main index in the upper code, we need to get the searchIndexClient here again
                SearchIndexClient searchIndexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(queryKey));
                SearchParameters  parameters        = new SearchParameters
                {
                    Select         = new[] { "firstName", "lastName" },
                    ScoringProfile = "boostLastName"
                };

                var result = searchIndexClient.Documents.Search <Account>("Hughes", parameters);
                _logger.Information("BoostLastName {@result}", result);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
                throw ex;
            }
        }