Esempio n. 1
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;
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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;
        }