Пример #1
0
 /// <summary>
 /// Initializes a new <see cref="ResultTypeInfo"/> instance
 /// </summary>
 /// <param name="name">The name of the type</param>
 /// <param name="displayTemplate">Display template metadata</param>
 /// <param name="resultSource">Result source metadata</param>
 public ResultTypeInfo(string name, DisplayTemplateInfo displayTemplate, ResultSourceInfo resultSource)
 {
     this.Name = name;
     this.DisplayProperties = new List <ManagedPropertyInfo>();
     this.Rules             = new List <ResultTypeRuleInfo>();
     this.DisplayTemplate   = displayTemplate;
     this.ResultSource      = resultSource;
 }
Пример #2
0
 /// <summary>
 /// Initializes a new <see cref="ResultTypeInfo"/> instance
 /// </summary>
 /// <param name="name">The name of the type</param>
 /// <param name="displayTemplate">Display template metadata</param>
 /// <param name="resultSource">Result source metadata</param>
 public ResultTypeInfo(string name, DisplayTemplateInfo displayTemplate, ResultSourceInfo resultSource)
 {
     this.Name = name;
     this.DisplayProperties = new List<ManagedPropertyInfo>();
     this.Rules = new List<ResultTypeRuleInfo>();
     this.DisplayTemplate = displayTemplate;
     this.ResultSource = resultSource;
 }
Пример #3
0
        public void EnsureResultSource_WhenSortingByRank_ShouldApplySpecifiedRankingModel()
        {
            const string ResultSourceName = "Test Result Source";

            // Arrange
            using (var testScope = SiteTestScope.BlankSite())
            {
                var resultSourceInfo = new ResultSourceInfo()
                {
                    Name = ResultSourceName,
                    Level = SearchObjectLevel.SPSite,
                    Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}",
                    SortSettings = new Dictionary<string, SortDirection>()
                    {
                        { BuiltInManagedProperties.Rank.Name, SortDirection.Descending }
                    },
                    RankingModelId = BuiltInRankingModels.DefaultSearchModelId,
                    UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource
                };

                using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                {
                    var searchHelper = injectionScope.Resolve<ISearchHelper>();
                    var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection);
                    var federationManager = new FederationManager(ssa);
                    var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb);

                    // Act
                    searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo);

                    // Assert
                    var source = federationManager.GetSourceByName(ResultSourceName, searchOwner);

                    Assert.IsNotNull(source);
                    Assert.AreEqual(ResultSourceName, source.Name);
                    Assert.AreEqual(BuiltInRankingModels.DefaultSearchModelId.ToString(), source.QueryTransform.OverrideProperties["RankingModelId"]);
                }
            }
        }
Пример #4
0
        public void EnsureResultSource_WhenRevertingAppendedQuery_ShouldRevertToPreviousQuery()
        {
            const string ResultSourceName = "Test Result Source";
            const string Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}";
            const string AppendedQuery = "{?{|owstaxidmetadataalltagsinfo:{User.SPSResponsibility}}}";

            using (var testScope = SiteTestScope.BlankSite())
            {
                // Arrange
                var resultSourceInfo = new ResultSourceInfo()
                {
                    Name = ResultSourceName,
                    Level = SearchObjectLevel.SPSite,
                    Query = Query,
                    UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource
                };

                var appendedResultSourceInfo = new ResultSourceInfo()
                {
                    Name = ResultSourceName,
                    Level = SearchObjectLevel.SPSite,
                    Query = AppendedQuery,
                    UpdateMode = ResultSourceUpdateBehavior.AppendToQuery
                };

                var revertedResultSourceInfo = new ResultSourceInfo()
                {
                    Name = ResultSourceName,
                    Level = SearchObjectLevel.SPSite,
                    Query = AppendedQuery,
                    UpdateMode = ResultSourceUpdateBehavior.RevertQuery
                };

                using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                {
                    var searchHelper = injectionScope.Resolve<ISearchHelper>();
                    var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection);
                    var federationManager = new FederationManager(ssa);
                    var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb);

                    // Act
                    searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo);
                    searchHelper.EnsureResultSource(testScope.SiteCollection, appendedResultSourceInfo);
                    searchHelper.EnsureResultSource(testScope.SiteCollection, revertedResultSourceInfo);

                    // Assert
                    var source = federationManager.GetSourceByName(ResultSourceName, searchOwner);

                    Assert.IsNotNull(source);
                    Assert.IsNotNull(source.QueryTransform);
                    Assert.AreEqual(ResultSourceName, source.Name);
                    Assert.AreEqual(Query, source.QueryTransform.QueryTemplate);
                }
            }
        }
Пример #5
0
        public void EnsureResultSource_WhenNotSortingByRank_ShouldNotApplyARankingModel()
        {
            const string ResultSourceName = "Test Result Source";

            // Arrange
            using (var testScope = SiteTestScope.BlankSite())
            {
                var resultSourceInfo = new ResultSourceInfo()
                {
                    Name = ResultSourceName,
                    Level = SearchObjectLevel.SPSite,
                    Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}",
                    RankingModelId = BuiltInRankingModels.DefaultSearchModelId,
                    UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource
                };

                using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                {
                    var searchHelper = injectionScope.Resolve<ISearchHelper>();

                    // Act
                    searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo);

                    // Assert

                    // Exception should have been thrown already
                    Assert.IsTrue(false);
                }
            }
        }
Пример #6
0
 /// <summary>
 /// Delete a result source
 /// </summary>
 /// <param name="contextSite">The context site collection</param>
 /// <param name="resultSourceInfo">The result source info object</param>
 public void DeleteResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
 {
     // Get the search service application for the current site
     this.DeleteResultSource(contextSite, resultSourceInfo.Name, resultSourceInfo.Level);
 }
Пример #7
0
        /// <summary>
        /// Ensure a result source
        /// </summary>
        /// <param name="contextSite">The context SPSite object</param>
        /// <param name="resultSourceInfo">The result source configuration object</param>
        /// <returns>The name of the result source</returns>
        public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
        {
            Source resultSource = null;
            var updateMode = resultSourceInfo.UpdateMode;

            var sortCollection = new SortCollection();

            if (resultSourceInfo.SortSettings != null)
            {            
                foreach (var sortSetting in resultSourceInfo.SortSettings)
                {
                    sortCollection.Add(sortSetting.Key, sortSetting.Value);
                }
            }

            var queryProperties = new QueryTransformProperties();
            queryProperties["SortList"] = sortCollection;

            // If the SortCollection contains "Rank" as one of its keys, specifiy the ranking model to be used. If a ranking model is
            // specified but sorting by Rank is not in the sort setting, throw an exception.
            if (resultSourceInfo.RankingModelId != Guid.Empty)
            {
                if ((resultSourceInfo.SortSettings != null && !resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) || resultSourceInfo.SortSettings == null)
                {
                    throw new ArgumentException(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            "You can't specify a ranking model id ({0}) if you are not sorting by rank. Make sure to include Rank as the first Sorting Key in the sort settings if you want to use a ranking model.",
                            resultSourceInfo.RankingModelId));
                }

                queryProperties["RankingModelId"] = resultSourceInfo.RankingModelId.ToString();
            }
            else if (resultSourceInfo.SortSettings != null && resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name))
            {
                queryProperties["RankingModelId"] = BuiltInRankingModels.DefaultSearchModelId.ToString();
            }

            // Get the search service application for the current site
            var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite);
            if (searchServiceApplication != null)
            {
                if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource))
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication, 
                        resultSourceInfo.Name, 
                        resultSourceInfo.Level, 
                        resultSourceInfo.SearchProvider, 
                        contextSite.RootWeb, 
                        resultSourceInfo.Query,
                        queryProperties, 
                        true, 
                        resultSourceInfo.IsDefaultResultSourceForOwner);
                }
                else
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication, 
                        resultSourceInfo.Name, 
                        resultSourceInfo.Level, 
                        resultSourceInfo.SearchProvider, 
                        contextSite.RootWeb, 
                        resultSourceInfo.Query,
                        queryProperties, 
                        false, 
                        resultSourceInfo.IsDefaultResultSourceForOwner);

                    var searchQuery = string.Empty;
                    if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery))
                    {
                        searchQuery = resultSourceInfo.Query;
                    }
                    else if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            // Check if appended query is already found on the current result source query template
                            // Note: remain case sensitive because the revert query option is also case sensitive.
                            if (!resultSource.QueryTransform.QueryTemplate.Contains(resultSourceInfo.Query))
                            {
                                searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query;
                            }
                        }
                        else
                        {
                            searchQuery = resultSourceInfo.Query;
                        }
                    }
                    else if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            searchQuery = resultSource.QueryTransform.QueryTemplate.Replace(resultSourceInfo.Query, string.Empty).Trim();
                        }
                    }

                    resultSource.CreateQueryTransform(queryProperties, searchQuery);
                    resultSource.Commit();
                }
            }

            return resultSource;
        }
Пример #8
0
 /// <summary>
 /// Delete a result source
 /// </summary>
 /// <param name="contextSite">The context site collection</param>
 /// <param name="resultSourceInfo">The result source info object</param>
 public void DeleteResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
 {
     // Get the search service application for the current site
     this.DeleteResultSource(contextSite, resultSourceInfo.Name, resultSourceInfo.Level);
 }
Пример #9
0
        /// <summary>
        /// Ensure a result source
        /// </summary>
        /// <param name="contextSite">The context SPSite object</param>
        /// <param name="resultSourceInfo">The result source configuration object</param>
        /// <returns>The name of the result source</returns>
        public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
        {
            Source resultSource = null;
            var    updateMode   = resultSourceInfo.UpdateMode;

            var sortCollection = new SortCollection();

            if (resultSourceInfo.SortSettings != null)
            {
                foreach (var sortSetting in resultSourceInfo.SortSettings)
                {
                    sortCollection.Add(sortSetting.Key, sortSetting.Value);
                }
            }

            var queryProperties = new QueryTransformProperties();

            queryProperties["SortList"] = sortCollection;

            // If the SortCollection contains "Rank" as one of its keys, specifiy the ranking model to be used. If a ranking model is
            // specified but sorting by Rank is not in the sort setting, throw an exception.
            if (resultSourceInfo.RankingModelId != Guid.Empty)
            {
                if ((resultSourceInfo.SortSettings != null && !resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) || resultSourceInfo.SortSettings == null)
                {
                    throw new ArgumentException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  "You can't specify a ranking model id ({0}) if you are not sorting by rank. Make sure to include Rank as the first Sorting Key in the sort settings if you want to use a ranking model.",
                                  resultSourceInfo.RankingModelId));
                }

                queryProperties["RankingModelId"] = resultSourceInfo.RankingModelId.ToString();
            }
            else if (resultSourceInfo.SortSettings != null && resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name))
            {
                queryProperties["RankingModelId"] = BuiltInRankingModels.DefaultSearchModelId.ToString();
            }

            // Get the search service application for the current site
            var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite);

            if (searchServiceApplication != null)
            {
                if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource))
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        true,
                        resultSourceInfo.IsDefaultResultSourceForOwner);
                }
                else
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        false,
                        resultSourceInfo.IsDefaultResultSourceForOwner);

                    var searchQuery = string.Empty;
                    if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery))
                    {
                        searchQuery = resultSourceInfo.Query;
                    }
                    else if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            // Check if appended query is already found on the current result source query template
                            // Note: remain case sensitive because the revert query option is also case sensitive.
                            if (!resultSource.QueryTransform.QueryTemplate.Contains(resultSourceInfo.Query))
                            {
                                searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query;
                            }
                        }
                        else
                        {
                            searchQuery = resultSourceInfo.Query;
                        }
                    }
                    else if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            searchQuery = resultSource.QueryTransform.QueryTemplate.Replace(resultSourceInfo.Query, string.Empty).Trim();
                        }
                    }

                    resultSource.CreateQueryTransform(queryProperties, searchQuery);
                    resultSource.Commit();
                }
            }

            return(resultSource);
        }
Пример #10
0
        /// <summary>
        /// Ensure a result source
        /// </summary>
        /// <param name="contextSite">The context SPSite object</param>
        /// <param name="resultSourceInfo">The result source configuration object</param>
        /// <returns>The name of the result source</returns>
        public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
        {
            Source resultSource = null;
            var    updateMode   = resultSourceInfo.UpdateMode;

            var sortCollection = new SortCollection();

            if (resultSourceInfo.SortSettings != null)
            {
                foreach (KeyValuePair <string, SortDirection> sortSetting in resultSourceInfo.SortSettings)
                {
                    sortCollection.Add(sortSetting.Key, sortSetting.Value);
                }
            }

            var queryProperties = new QueryTransformProperties();

            queryProperties["SortList"] = sortCollection;

            // Get the search service application for the current site
            var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite);

            if (searchServiceApplication != null)
            {
                if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource))
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        true,
                        resultSourceInfo.IsDefaultResultSourceForOwner);
                }
                else
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        false,
                        resultSourceInfo.IsDefaultResultSourceForOwner);

                    string searchQuery = string.Empty;

                    if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery))
                    {
                        searchQuery = resultSourceInfo.Query;
                    }

                    if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            var rgx = new Regex(resultSourceInfo.Query);
                            if (!rgx.IsMatch(resultSource.QueryTransform.QueryTemplate))
                            {
                                searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query;
                            }
                        }
                        else
                        {
                            searchQuery = resultSourceInfo.Query;
                        }
                    }

                    if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            var rgx = new Regex(resultSourceInfo.Query);
                            searchQuery = rgx.Replace(resultSource.QueryTransform.QueryTemplate, string.Empty);
                        }
                    }

                    resultSource.CreateQueryTransform(queryProperties, searchQuery);
                    resultSource.Commit();
                }
            }

            return(resultSource);
        }
Пример #11
0
        /// <summary>
        /// Ensure a result source
        /// </summary>
        /// <param name="contextSite">The context SPSite object</param>
        /// <param name="resultSourceInfo">The result source configuration object</param>
        /// <returns>The name of the result source</returns>
        public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo)
        {
            Source resultSource = null;
            var updateMode = resultSourceInfo.UpdateMode;

            var sortCollection = new SortCollection();

            if (resultSourceInfo.SortSettings != null)
            {
                foreach (KeyValuePair<string, SortDirection> sortSetting in resultSourceInfo.SortSettings)
                {
                    sortCollection.Add(sortSetting.Key, sortSetting.Value);
                }
            }

            var queryProperties = new QueryTransformProperties();
            queryProperties["SortList"] = sortCollection;

            // Get the search service application for the current site
            var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite);
            if (searchServiceApplication != null)
            {
                if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource))
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        true,
                        resultSourceInfo.IsDefaultResultSourceForOwner);
                }
                else
                {
                    resultSource = InnerEnsureResultSource(
                        searchServiceApplication,
                        resultSourceInfo.Name,
                        resultSourceInfo.Level,
                        resultSourceInfo.SearchProvider,
                        contextSite.RootWeb,
                        resultSourceInfo.Query,
                        queryProperties,
                        false,
                        resultSourceInfo.IsDefaultResultSourceForOwner);

                    string searchQuery = string.Empty;

                    if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery))
                    {
                        searchQuery = resultSourceInfo.Query;
                    }

                    if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            var rgx = new Regex(resultSourceInfo.Query);
                            if (!rgx.IsMatch(resultSource.QueryTransform.QueryTemplate))
                            {
                                searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query;
                            }
                        }
                        else
                        {
                            searchQuery = resultSourceInfo.Query;
                        }
                    }

                    if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery))
                    {
                        if (resultSource.QueryTransform != null)
                        {
                            var rgx = new Regex(resultSourceInfo.Query);
                            searchQuery = rgx.Replace(resultSource.QueryTransform.QueryTemplate, string.Empty);
                        }
                    }

                    resultSource.CreateQueryTransform(queryProperties, searchQuery);
                    resultSource.Commit();
                }
            }

            return resultSource;
        }