如何:为搜索架构中的类别检索已爬网属性

可以使用企业级搜索管理对象模型来访问共享服务提供程序 (SSP) 的搜索元数据(使用 Schema 对象)的已爬网属性类别。然后,可以使用 Category 对象的 GetAllCrawledProperties 方法,检索与每个类别关联的已爬网属性的列表。

下面的过程演示如何通过控制台应用程序编写已爬网属性及其属性集 GUID 以及变量数据类型的完整列表。

设置应用程序以使用企业级搜索管理对象模型

  1. 在应用程序中,设置对以下 DLL 的引用:

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. 在控制台应用程序的类文件中,将下面的 using 语句添加到含有其他命名空间指令的代码的顶部附近。

    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    

验证用户是否为类别名称指定了值,如果未指定值,则为用户提供用法帮助

  1. 创建函数将用法信息写出到控制台窗口。

    static void Usage()
    {
       Console.WriteLine("Retrieve Crawled Properties by Category Sample");
       Console.WriteLine("Usage: GetCrawledPropertiesSample.exe CategoryName");
    }
    
  2. 在控制台应用程序的 Main() 函数中,添加代码以检查 args[] 参数中的项目数是否等于 1;如果不是(表示未指定值来标识类别),则调用上一步中定义的 Usage() 函数。

    if (args.Length != 1)
    {
    Usage();
    return;
    }
    

为 SSP 的搜索架构检索类别并显示该类别的已爬网属性的列表

  1. 使用下面的代码为类别名称创建本地字符串变量,并将其设置为在 args[] 参数中指定的值:

    string strCategoryName = args[0];
    
  2. 使用下面的代码检索 SSP 的搜索上下文的 Schema 对象。有关检索搜索上下文的方法的详细信息,请参阅如何:返回搜索服务提供程序的搜索上下文

    /*
    Replace <SiteName> with the name of a site using the SSP
    */
    string strURL = "http://<SiteName>";
    Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
    
  3. 通过使用下面的代码检索搜索架构的类别集合:

    CategoryCollection categories = sspSchema.AllCategories;
    
  4. 使用下面的代码确定类别集合是否包含指定的类别,如果未包含,则显示集合中的类别的列表:

    if (categories.Contains(strCategoryName) != true)
    {
        Console.WriteLine(strCategoryName + " does not exist in Schema.");
        Console.WriteLine("Valid Schema Categories are:");
        foreach (Category category in categories)
        {
            Console.WriteLine(category.Name);
        }
        return;
    }
    
  5. 如果该类别存在,则使用下面的代码检索该类别的已爬网属性的列表,并将列表中每个已爬网属性的名称、属性集 GUID 和变量类型写入到控制台中:

    foreach (Category category in categories)
    {
        if (category.Name == strCategoryName)
        {
            foreach (CrawledProperty property in category.GetAllCrawledProperties())
            {
                Console.WriteLine("");
                Console.Write("NAME: " + property.Name);
                Console.Write("   PROPSET: " + property.Propset.ToString());
                Console.Write("   VARIANTTYPE: " + property.VariantType.ToString());
            }
        }
    }
    

示例

以下是控制台应用程序类示例的完整代码。

先决条件

  • 确保已创建一个共享服务提供程序。

项目引用

运行此示例之前,在控制台应用程序的代码项目中添加下面的项目引用:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.Search

using System;
using System.Collections;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace GetCrawledPropertiesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Usage();
                    return;
                }

                string strCategoryName = args[0];
                /*
                Replace <SiteName> with the name of a site using the SSP
                */
                string strURL = "http://<SiteName>";
                Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
                CategoryCollection categories = sspSchema.AllCategories;

                if (categories.Contains(strCategoryName) != true)
                {
                    Console.WriteLine(strCategoryName + " does not exist in Schema.");
                    Console.WriteLine("Valid Schema Categories are:");
                    foreach (Category category in categories)
                    {
                        Console.WriteLine(category.Name);
                    }
                    return;
                }

                foreach (Category category in categories)
                {
                    if (category.Name == strCategoryName)
                    {
                        foreach (CrawledProperty property in category.GetAllCrawledProperties())
                        {
                            Console.WriteLine("");
                            Console.Write("NAME: " + property.Name);
                            Console.Write("   PROPSET: " + property.Propset.ToString());
                            Console.Write("   VARIANTTYPE: " + property.VariantType.ToString());
                        }
                    }
                }
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Usage();
            }
        }
 
        static void Usage()
        {
            Console.WriteLine("Retrieve Crawled Properties by Category Sample");
            Console.WriteLine("Usage: GetCrawledPropertiesSample.exe CategoryName");
        }
    }
}