Example #1
0
        /// <summary>
        /// Ensure a search result source
        /// </summary>
        /// <param name="ssa">The search service application.</param>
        /// <param name="resultSourceName">The result source name</param>
        /// <param name="level">The search object level.</param>
        /// <param name="searchProvider">The search provider for the result source.</param>
        /// <param name="contextWeb">The SPWeb to retrieve the search context.</param>
        /// <param name="query">The search query in KQL format.</param>
        /// <param name="properties">Query properties.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        /// <param name="isDefaultResultSourceForOwner">Whether this result source will be flagged as the default for the current search owner</param>
        /// <returns>
        /// The result source.
        /// </returns>
        private static Source InnerEnsureResultSource(SearchServiceApplication ssa, string resultSourceName, SearchObjectLevel level, string searchProvider, SPWeb contextWeb, string query, QueryTransformProperties properties, bool overwrite, bool isDefaultResultSourceForOwner)
        {
            var federationManager = new FederationManager(ssa);
            var searchOwner       = new SearchObjectOwner(level, contextWeb);

            var resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner);

            if (resultSource != null && overwrite)
            {
                federationManager.RemoveSource(resultSource);
            }

            if (resultSource == null || overwrite)
            {
                resultSource            = federationManager.CreateSource(searchOwner);
                resultSource.Name       = resultSourceName;
                resultSource.ProviderId = federationManager.ListProviders()[searchProvider].Id;
                resultSource.CreateQueryTransform(properties, query);
                resultSource.Commit();

                if (isDefaultResultSourceForOwner)
                {
                    federationManager.UpdateDefaultSource(resultSource.Id, searchOwner);
                }
            }

            return(resultSource);
        }
Example #2
0
        private SPManagedPropertyInfo GetManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                managedPropertyDefinition = ssa.GetManagedProperty(propertyName, owner);
            }
            else
            {
                this.logger.Warn("Managed Property '{0}' not found.", propertyName);
            }

            return(managedPropertyDefinition);
        }
Example #3
0
        /// <summary>
        /// Get all query rules for a search level.
        /// </summary>
        /// <param name="ssa">The search service.</param>
        /// <param name="level">The search object level.</param>
        /// <param name="contextWeb">The SPWeb context.</param>
        /// <returns>A query rule collection.</returns>
        private static QueryRuleCollection GetQueryRules(SearchServiceApplication ssa, SearchObjectLevel level, SPWeb contextWeb)
        {
            var queryRuleManager = new QueryRuleManager(ssa);
            var searchOwner      = new SearchObjectOwner(level, contextWeb);

            return(queryRuleManager.GetQueryRules(new SearchObjectFilter(searchOwner)));
        }
Example #4
0
        private static Dictionary <string, string> GetManagedPropertyNamesUncached(SPSite site)
        {
            Dictionary <string, string> dictionary        = new Dictionary <string, string>();
            SearchServiceApplication    searchApplication = GetSearchServiceApplication(site);

            if (searchApplication != null)
            {
                SPFieldCollection fields = site.RootWeb.Fields;
                foreach (KeyValuePair <string, string> entry in GetManagedPropertyNames(searchApplication))
                {
                    SPField field;
                    try {
                        field = fields.GetFieldByInternalName(entry.Key);
                    } catch (ArgumentException) {
                        continue;
                    }
                    dictionary.Add(entry.Key, entry.Value);
                    TaxonomyField taxField = CommonHelper.TryCastOrDefault <TaxonomyField>(field);
                    if (taxField != null && taxField.TextField != Guid.Empty)
                    {
                        try {
                            SPField textField = fields[taxField.TextField];
                            dictionary[textField.InternalName] = entry.Value;
                        } catch {
                        }
                    }
                }
            }
            return(dictionary);
        }
Example #5
0
        private static List <MappingInfo> GetInitialCrawledPropertyMappings(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            var mappingCollection = new List <MappingInfo>();

            // If specified to overwrite all crawled property mappings
            // set an empty mapping info list before recreating the mappings.
            // Else if, if specified to append the crawled properties, initialize the
            // mapping collection to the existing mappings on the managed property.
            switch (managedPropertyInfo.UpdateBehavior)
            {
            case ManagedPropertyUpdateBehavior.OverwriteCrawledProperties:
            case ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists:
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                break;

            case ManagedPropertyUpdateBehavior.AppendCrawledProperties:
                mappingCollection = ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner);
                break;
            }

            return(mappingCollection);
        }
Example #6
0
        private void SetCrawledPropertyMappings(
            SPSite site,
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner,
            List <MappingInfo> mappings)
        {
            // Ensure crawl properties mappings
            foreach (var crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
            {
                // Get the crawled property (there may be more than one matching that name)
                var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);
                if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                {
                    foreach (var crawledProperty in matchingCrawledProperties)
                    {
                        // Create mapping information
                        var mapping = new MappingInfo
                        {
                            CrawledPropertyName = crawledProperty.Name,
                            CrawledPropset      = crawledProperty.Propset,
                            ManagedPid          = managedPropertyDefinition.Pid,
                            MappingOrder        = crawledPropertyKeyAndOrder.Value
                        };

                        // If managed property doesn't already contain a mapping for the crawled property, add it
                        if (
                            ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner)
                            .All(m => m.CrawledPropertyName != mapping.CrawledPropertyName))
                        {
                            mappings.Add(mapping);
                        }
                        else
                        {
                            this.logger.Info(
                                "Mapping for managed property {0} and crawled property with name {1} is already exists",
                                managedPropertyDefinition.Name,
                                crawledPropertyKeyAndOrder);
                        }
                    }
                }
                else
                {
                    this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                }
            }

            // Apply mappings to the managed property
            if (mappings.Count > 0)
            {
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappings, owner);
            }
        }
Example #7
0
        /// <summary>
        /// Clears the managed property cache for a specified site collection.
        /// </summary>
        /// <param name="site">A site collection object.</param>
        public static void FlushCache(SPSite site)
        {
            CommonHelper.ConfirmNotNull(site, "site");
            SPSiteIdFactory.Destroy(site.ID);
            SearchServiceApplication searchApplication = GetSearchServiceApplication(site);

            if (searchApplication != null)
            {
                SearchApplicationIdFactory.Destroy(searchApplication.Id);
            }
        }
Example #8
0
        public static void SetMaxRowLimit()
        {
            //SPFarm farm = SPFarm.Local;
            //SearchServiceApplication searchApp = (SearchServiceApplication)farm.Services.
            //    GetValue<SearchQueryAndSiteSettingsService>().Applications.GetValue<SearchServiceApplication>("Search Service 应用程序 1");
            SearchService searchService = SearchService.Service;

            SearchServiceApplication searchApp = searchService.SearchApplications.GetValue <SearchServiceApplication>(new Guid("Search Service 应用程序 1"));

            searchApp.MaxRowLimit = 10000;
            searchApp.Update();
        }
Example #9
0
        private static Dictionary <string, string> GetManagedPropertyNamesUncached(SearchServiceApplication searchApplication)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();
            Schema schema = new Schema(searchApplication);

            foreach (ManagedProperty info in schema.AllManagedProperties)
            {
                foreach (CrawledProperty property in info.GetMappedCrawledProperties(1))
                {
                    Match match = Regex.Match(property.Name, @"^ows_(?:taxId_|[qr]_(?:TEXT|MTXT|BOOL|INTG|GUID|URLH|DATE|HTML|IMGE|CHCS|USER)_)?");
                    if (match.Success)
                    {
                        string fieldName = property.Name.Substring(match.Length);
                        if (!dictionary.ContainsKey(fieldName))
                        {
                            dictionary.Add(fieldName, info.Name);
                        }
                    }
                }
            }
            return(dictionary);
        }
Example #10
0
        private SPManagedPropertyInfo CreateManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (prop.DeleteDisallowed)
                {
                    this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                }
                else
                {
                    prop.DeleteAllMappings();
                    prop.Delete();
                    managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            return(managedPropertyDefinition);
        }
Example #11
0
        private static void EnsureManagedProperties(SPSite site, IEnumerable <ManagedPropertyDefinition> definitions)
        {
            SearchServiceApplication searchApplication = GetSearchServiceApplication(site);

            if (searchApplication != null)
            {
                Schema schema = new Schema(searchApplication);
                IEnumerable <CrawledProperty> allCrawledProperties = schema.AllCategories["SharePoint"].GetAllCrawledProperties().OfType <CrawledProperty>();

                foreach (ManagedPropertyDefinition definition in definitions)
                {
                    int             variantType = VariantTypeDictionary[definition.DataType];
                    CrawledProperty rawProperty = allCrawledProperties.FirstOrDefault(v => v.Name == definition.CrawledPropertyName);
                    if (rawProperty == null || rawProperty.GetMappedManagedProperties().GetEnumerator().MoveNext())
                    {
                        continue;
                    }

                    ManagedProperty property;
                    try {
                        property = schema.AllManagedProperties[definition.MappedPropertyName];
                    } catch (KeyNotFoundException) {
                        property = schema.AllManagedProperties.Create(definition.MappedPropertyName, definition.DataType);
                    }
                    MappingCollection mappings = property.GetMappings();
                    Mapping           mapping  = new Mapping();
                    mapping.CrawledPropset      = rawProperty.Propset;
                    mapping.CrawledPropertyName = rawProperty.Name;
                    mapping.ManagedPid          = property.PID;
                    mappings.Add(mapping);
                    property.SetMappings(mappings);
                    property.Update();
                }
                FlushCache(site);
            }
        }
Example #12
0
 /// <summary>
 /// Gets a mapping of SharePoint field names to managed property names.
 /// </summary>
 /// <param name="searchApplication">A search service application.</param>
 /// <returns>A mapping of SharePoint field names to managed property names.</returns>
 public static IReadOnlyDictionary <string, string> GetManagedPropertyNames(SearchServiceApplication searchApplication)
 {
     CommonHelper.ConfirmNotNull(searchApplication, "searchApplication");
     return(SearchApplicationIdFactory.GetInstance(searchApplication.Id, () => GetManagedPropertyNamesUncached(searchApplication).AsReadOnly()));
 }
Example #13
0
        private void SetCrawledPropertyMappings(
            SPSite site,
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner,
            List<MappingInfo> mappings)
        {
            // Ensure crawl properties mappings
            foreach (var crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
            {
                // Get the crawled property (there may be more than one matching that name)
                var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);
                if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                {
                    foreach (var crawledProperty in matchingCrawledProperties)
                    {
                        // Create mapping information
                        var mapping = new MappingInfo
                        {
                            CrawledPropertyName = crawledProperty.Name,
                            CrawledPropset = crawledProperty.Propset,
                            ManagedPid = managedPropertyDefinition.Pid,
                            MappingOrder = crawledPropertyKeyAndOrder.Value
                        };

                        // If managed property doesn't already contain a mapping for the crawled property, add it
                        if (
                            ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner)
                                .All(m => m.CrawledPropertyName != mapping.CrawledPropertyName))
                        {
                            mappings.Add(mapping);
                        }
                        else
                        {
                            this.logger.Info(
                                "Mapping for managed property {0} and crawled property with name {1} is already exists",
                                managedPropertyDefinition.Name,
                                crawledPropertyKeyAndOrder);
                        }
                    }
                }
                else
                {
                    this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                }
            }

            // Apply mappings to the managed property
            if (mappings.Count > 0)
            {
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappings, owner);
            }
        }
Example #14
0
        private SPManagedPropertyInfo GetManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                managedPropertyDefinition = ssa.GetManagedProperty(propertyName, owner);
            }
            else
            {
                this.logger.Warn("Managed Property '{0}' not found.", propertyName);
            }

            return managedPropertyDefinition;
        }
Example #15
0
        /// <summary>
        /// Get all query rules for a search level.
        /// </summary>
        /// <param name="ssa">The search service.</param>
        /// <param name="level">The search object level.</param>
        /// <param name="contextWeb">The SPWeb context.</param>
        /// <returns>A query rule collection.</returns>
        private static QueryRuleCollection GetQueryRules(SearchServiceApplication ssa, SearchObjectLevel level, SPWeb contextWeb)
        {
            var queryRuleManager = new QueryRuleManager(ssa);
            var searchOwner = new SearchObjectOwner(level, contextWeb);

            return queryRuleManager.GetQueryRules(new SearchObjectFilter(searchOwner));
        }
Example #16
0
        /// <summary>
        /// Ensure a search result source
        /// </summary>
        /// <param name="ssa">The search service application.</param>
        /// <param name="resultSourceName">The result source name</param>
        /// <param name="level">The search object level.</param>
        /// <param name="searchProvider">The search provider for the result source.</param>
        /// <param name="contextWeb">The SPWeb to retrieve the search context.</param>
        /// <param name="query">The search query in KQL format.</param>
        /// <param name="properties">Query properties.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        /// <param name="isDefaultResultSourceForOwner">Whether this result source will be flagged as the default for the current search owner</param>
        /// <returns>
        /// The result source.
        /// </returns>
        private static Source InnerEnsureResultSource(SearchServiceApplication ssa, string resultSourceName, SearchObjectLevel level, string searchProvider, SPWeb contextWeb, string query, QueryTransformProperties properties, bool overwrite, bool isDefaultResultSourceForOwner)
        {
            var federationManager = new FederationManager(ssa);
            var searchOwner = new SearchObjectOwner(level, contextWeb);

            var resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner);

            if (resultSource != null && overwrite)
            {
                federationManager.RemoveSource(resultSource);
            }

            if (resultSource == null || overwrite)
            {
                resultSource = federationManager.CreateSource(searchOwner);
                resultSource.Name = resultSourceName;
                resultSource.ProviderId = federationManager.ListProviders()[searchProvider].Id;
                resultSource.CreateQueryTransform(properties, query);
                resultSource.Commit();

                if (isDefaultResultSourceForOwner)
                {
                    federationManager.UpdateDefaultSource(resultSource.Id, searchOwner);
                }
            }

            return resultSource;
        }
 /// <summary>
 /// Initialize the custom security trimmer
 /// </summary>
 /// <param name="properties">Static properties configured for the trimmer</param>
 /// <param name="searchApplication">Search Service Application instance</param>
 public void Initialize(NameValueCollection properties, SearchServiceApplication searchApplication)
 {
     if (properties.Get("connectionString") != null)
     {
         _connectionString = properties.Get("connectionString");
     }
     else
     {
         throw new ArgumentException("Missing property \"connectionString\"");
     }
 }
Example #18
0
        private SPManagedPropertyInfo CreateManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (prop.DeleteDisallowed)
                {
                    this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                }
                else
                {
                    prop.DeleteAllMappings();
                    prop.Delete();
                    managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            return managedPropertyDefinition;
        }
Example #19
0
 public SearchServiceApplicationInstance(ScriptEngine engine, SearchServiceApplication searchServiceApplication)
     : base(engine)
 {
     m_searchServiceApplication = searchServiceApplication;
     this.PopulateFunctions();
 }
Example #20
0
        private static List<MappingInfo> GetInitialCrawledPropertyMappings(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            var mappingCollection = new List<MappingInfo>();

            // If specified to overwrite all crawled property mappings
            // set an empty mapping info list before recreating the mappings.
            // Else if, if specified to append the crawled properties, initialize the
            // mapping collection to the existing mappings on the managed property.
            switch (managedPropertyInfo.UpdateBehavior)
            {
                case ManagedPropertyUpdateBehavior.OverwriteCrawledProperties:
                case ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists:
                    ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                    break;
                case ManagedPropertyUpdateBehavior.AppendCrawledProperties:
                    mappingCollection = ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner);
                    break;
            }

            return mappingCollection;
        }
Example #21
0
 protected SearchServiceApplicationInstance(ObjectInstance prototype, SearchServiceApplication searchServiceApplication)
     : base(prototype)
 {
     m_searchServiceApplication = searchServiceApplication;
 }