Share via


[方法] コンテンツ ソースのクロール スケジュールをプログラムで構成する

Microsoft Office SharePoint Server 2007 でのエンタープライズ検索 では、検索サービスの共有サービス プロバイダ (SSP) に対して構成するコンテンツ ソースにより、検索インデックス サービスがクロールするコンテンツを指定します。エンタープライズ検索 の管理オブジェクト モデルを使用して、SSP のコンテンツ ソース コレクションに新しいコンテンツ ソースを追加できます。詳細については、「[方法] コンテンツ ソースを追加する」を参照してください。

コンテンツのコンテンツ ソースを追加することは、作業の一部にすぎません。コンテンツをコンテンツ インデックスに含めるには、検索インデックス コンポーネントがコンテンツを実際にクロールすることも必要です。

[ContenSource] クラスの適切なメソッドを呼び出すことで、コンテンツ ソースのサイト全体のクロールまたは増分クロールを手動で開始したり、クロールの一時停止、再開、停止を行ったりできます。詳細については、「[方法] コンテンツ ソースのクロールをプログラム的に管理する」を参照してください。

ただし、コンテンツ ソースのコンテンツを継続して定期的にクロールする必要がある場合は、クロール スケジュールを設定することをお勧めします。これは、エンタープライズ検索 管理オブジェクト モデルを使用して行うこともできます。

以下の手順では、次の方法を説明します。

  • エンタープライズ検索 管理オブジェクトモデルを使用するコンソール アプリケーションをセットアップします。

  • [WeeklySchedule] クラスを使用して、コンテンツ ソースのサイト全体クロールのスケジュールを構成します。

  • [DailySchedule] クラスを使用して、コンテンツ ソースの増分クロールのスケジュールを構成します。

エンタープライズ検索管理オブジェクト モデルを使用するようにアプリケーションをセットアップするには

  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;
    
  3. 利用状況情報をコンソール ウィンドウに書き出す関数を作成します。

    private static void Usage()
    {
    Console.WriteLine("Manage Content Source Crawl Status");
    Console.WriteLine("Usage: ManageCrawlStatus.exe <ContentSource>");
    Console.WriteLine("<ContentSourceName> - Specify the content source name.");
    }
    
  4. コンソール アプリケーションの Main() 関数で、args[] パラメータ内のアイテム数をチェックするコードを追加します。1 より小さい場合は、コンテンツ ソースを識別する値が指定されていないことを意味するので、手順 3 で定義した Usage() 関数を呼び出します。

    if (args.Length < 1 )
    {
       Usage();
       return;
    }
    
  5. 手順 4 のコードに続いて、SSP の検索コンテキストを取得する次のコードを追加します。

    /*
    Replace <SiteName> with the name of a site using the SSP
    */
    string strURL = "http://<SiteName>";
    SearchContext context;
    using (SPSite site = new SPSite(strURL))
    {
        Context = SearchContext.GetContext(site);
    }
    

DailySchedule クラスを使用してクロール スケジュールを作成するには

  1. DailySchedule クラスのインスタンスを作成します。

    DailySchedule daily = new DailySchedule(context);
    
  2. コンテンツ ソースのクロールを開始するとき、およびクロールの頻度を指定するには、DailySchedule プロパティを構成します。次に例を示します。

    //Indicates the schedule starts on the 15th day of the month.
    daily.BeginDay = 15;
    //Indicates the schedule starts in January.
    daily.BeginMonth = 1;
    //Indicates that the schedule starts in 2007.
    daily.BeginYear = 2007;
    //The next two lines of code indicate that the schedule starts at 2:30 in the morning.
    daily.StartHour = 2;
    daily.StartMinute = 30;
    //Indicates that the content should be crawled every day.
    daily.DaysInterval = 1;
    
  3. SSP の検索サービスに対して構成されているコンテンツ ソースのコレクションを取得します。

    Content sspContent = new Content(context);
    ContentSourceCollection sspContentSources = sspContent.ContentSources;
    

WeeklySchedule クラスを使用してクロール スケジュールを作成するには

  1. WeeklySchedule クラスのインスタンスを作成します。

    WeeklySchedule weekly= new WeeklySchedule(context);
    
  2. コンテンツ ソースのクロールを開始するとき、およびクロールの頻度を指定するには、WeeklySchedule プロパティを構成します。次に例を示します。

    //Indicates the schedule starts on the 1st day of the month.
    weekly.BeginDay = 1;
    //Indicates the schedule starts in January.
    weekly.BeginMonth = 1;
    //Indicates that the schedule starts in 2007.
    weekly.BeginYear = 2007;
    //The next two lines of code indicate that the schedule starts at 11:15 at night.
    weekly.StartHour = 23;
    weekly.StartMinute = 15;
    //Indicates that the content should be crawled every week.
    weekly.WeeksInterval = 1;
    

新しいスケジュールを使用するようにコンテンツ ソースを構成するには

  1. args[0] パラメータで指定されている値を取得し、SSP のコンテンツ ソース コレクションにその名前のコンテンツ ソースが含まれることを確認します。

    string strContentSourceName = args[0];
    if(sspContentSources.Exists(strContentSourceName) )
    {
       <…>
    }
    else 
    {
       Console.WriteLine("Content source does not exist.");
    }
    
  2. 指定されている名前のコンテンツ ソースを取得し、FullCrawlSchedule プロパティおよび IncrementalCrawlSchedule プロパティに新しいスケジュールを設定します。

    ContentSource cs = sspContentSources[strContentSourceName];
    cs.IncrementalCrawlSchedule = daily;
    cs.FullCrawlSchedule = weekly;
    cs.Update();
    

以下に、このトピックで説明したサンプル コンソール アプリケーションの完全なコードを示します。

前提条件

  • 共有サービス プロバイダが既に作成されていることを確認します。

プロジェクト参照

このサンプルを実行する前に、コンソール アプリケーション コード プロジェクトに以下のプロジェクト参照を追加します。

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.Search

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;

namespace ManageCrawlStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    Usage();
                    return;
                }
                /*
                    Replace <SiteName> with the name of a site using the Shared Services Provider
                */
                string strURL = "http://<SiteName>";
                SearchContext context;
                using (SPSite site = new SPSite(strURL))
                {
                    Context = SearchContext.GetContext(site);
                }

                DailySchedule daily = new DailySchedule(context);
                //Indicates the schedule starts on the 15th day of the month.
                daily.BeginDay = 15;
                //Indicates the schedule starts in January.
                daily.BeginMonth = 1;
                //Indicates that the schedule starts in 2007.
                daily.BeginYear = 2007;
                //The next two lines of code indicate that the schedule starts at 2:30 in the morning.
                daily.StartHour = 2;
                daily.StartMinute = 30;
                //Indicates that the content should be crawled every day.
                daily.DaysInterval = 1;
                WeeklySchedule weekly = new WeeklySchedule(context);
                //Indicates the schedule starts on the 1st day of the month.
                weekly.BeginDay = 1;
                //Indicates the schedule starts in January.
                weekly.BeginMonth = 1;
                //Indicates that the schedule starts in 2007.
                weekly.BeginYear = 2007;
                //The next two lines of code indicate that the schedule starts at 11:15 at night.
                weekly.StartHour = 23;
                weekly.StartMinute = 15;
                //Indicates that the content should be crawled every week.
                weekly.WeeksInterval = 1;

                string strContentSourceName = args[0];
                Content sspContent = new Content(context);
                ContentSourceCollection sspContentSources = sspContent.ContentSources;

                if (sspContentSources.Exists(strContentSourceName))
                {
                    ContentSource cs = sspContentSources[strContentSourceName];
                    cs.IncrementalCrawlSchedule = daily;
                    cs.FullCrawlSchedule = weekly;
                    cs.Update();
                }
                else
                {
                    Console.WriteLine("Content source does not exist.");
                }
            }

            catch (Exception e)
            {
                e.ToString();
            }
        }

        private static void Usage()
        {
            Console.WriteLine("Configure Crawl Schedule");
            Console.WriteLine("Usage: ConfigureCrawlSchedule.exe <ContentSourceName>");
            Console.WriteLine("<ContentSourceName> - Specify the content source name.");
        }
    }
}

See Also

タスク

[方法] 検索サービス プロバイダに検索コンテキストを返す

[方法] 共有サービス プロバイダのコンテンツ ソースを取得する

[方法] コンテンツ ソースを追加する

[方法] コンテンツ ソースを削除する

[方法] コンテンツ ソースのクロールをプログラム的に管理する

概念

コンテンツ ソースの概要