[方法] 管理プロパティの重み設定を変更する
エンタープライズ検索 管理オブジェクト モデルの ManagedProperty() クラスを使用して、管理プロパティに適用された重みを変更できます。この設定は、コンテンツのインデックス作成時に適用されます。
コンソール アプリケーションから管理プロパティの重みを変更するには
アプリケーションで、以下の DLL への参照を設定します。
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
コンソール アプリケーションのクラス ファイルで、他の名前空間ディレクティブを含むコードの上部付近に次の using ステートメントを追加します。
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
利用状況情報をコンソール ウィンドウに書き出す関数を作成します。
static void Usage() { Console.WriteLine("Change Property Weight"); Console.WriteLine("Usage: PropertyWeightSample.exe ManagedPropertyName <WeightValue>"); Console.WriteLine("<WeightValue>: Must be formatted as a float."); }
コンソール アプリケーションの Main() 関数で、コードを追加して args[] パラメータ内の項目数を確認します。これは 2 でなければなりません。これ以外の場合は、手順 3. で定義した Usage() 関数を呼び出します。
if (args.Length != 2) { Usage(); return; }
args[] パラメータで指定した値を取得します。これを使用して、重み設定を変更する管理プロパティの名前および新しい重み設定値を指定します。
string strPropertyName = args[0].ToLower(); float newWeight = Convert.ToSingle(args[1]);
共有サービス プロバイダ (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)));
以下のコードを使用して、管理プロパティのコレクションを取得します。
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
Contains メソッドに返された値を確認して、重みを変更する管理プロパティが管理プロパティのコレクションに含まれているかどうかを判断します。false の場合、プロパティは存在しないため、存在しないことを示すメッセージをコンソールに書き出します。
if (!properties.Contains(strPropertyName)) { Console.WriteLine strPropertyName + " property does not exist."); return; }
Contains メソッドが true を返す場合、管理プロパティをループして、それぞれについて Name プロパティが strPropertyName の値と一致するかどうかを確認し、一致する場合は Weight プロパティを指定された値に変更します。
foreach (ManagedProperty property in properties) { if (property.Name.ToLower() == strPropertyName) { property.Weight = newWeight; Console.WriteLine("Weight value changed for " + strPropertyName + " property."); Console.WriteLine("NAME: " + property.Name + " WEIGHT: " + property.Weight.ToString()); } }
例
以下に、サンプル コンソール アプリケーションの完全なコードを示します。
前提条件
- 共有サービス プロバイダが既に作成されていることを確認します。
プロジェクト参照
このサンプルを実行する前に、コンソール アプリケーション コード プロジェクトに以下のプロジェクト参照を追加します。
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 PropertyWeightSample
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length != 2)
{
Usage();
return;
}
/*
Replace <SiteName> with the name of a site using the SSP
*/
string strURL = "http://<SiteName>";
Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
string strPropertyName = args[0].ToLower();
float newWeight = Convert.ToSingle(args[1]);
if (!properties.Contains(strPropertyName))
{
Console.WriteLine(strPropertyName + " property does not exist.");
return;
}
foreach (ManagedProperty property in properties)
{
if (property.Name.ToLower() == strPropertyName)
{
property.Weight = newWeight;
Console.WriteLine("Weight value changed for " + strPropertyName + " property.");
Console.WriteLine("NAME: " + property.Name + " WEIGHT: " + property.Weight.ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Usage();
}
}
private static void Usage()
{
Console.WriteLine("Change Property Weight");
Console.WriteLine("Usage: PropertyWeightSample.exe ManagedPropertyName <WeightValue>");
Console.WriteLine("<WeightValue>: Must be formatted as a float.");
}
}
}
See Also
参照
Microsoft.Office.Server.Search.Administration.Schema
概念
エンタープライズ検索管理オブジェクト モデルを使うにあたって