Share via


フルテキスト検索の実装

フルテキスト検索は、SQL Server のインスタンスごとに使用することができ、SMO では FullTextService オブジェクトで表現されます。FullTextService オブジェクトは、Server オブジェクトの下位に位置します。このオブジェクトは、Microsoft フルテキスト検索サービスの構成オプションを管理するために使用します。FullTextCatalogCollection オブジェクトは Database オブジェクトに属し、データベースに対して定義されるフルテキスト カタログを表す FullTextCatalog オブジェクトのコレクションです。通常のインデックスとは異なり、フルテキスト インデックスは各テーブルに対して 1 つのみ定義されます。これは、Table オブジェクト内の FullTextIndexColumn オブジェクトで表現されます。

フルテキスト検索サービスを作成するには、データベースにフルテキスト カタログが定義されており、データベース内のいずれかのテーブルにフルテキスト検索インデックスが定義されている必要があります。

まず、FullTextCatalog コンストラクターを呼び出し、カタログ名を指定して、データベースにフルテキスト カタログを作成します。次に、コンストラクターを呼び出し、フルテキスト インデックスを作成するテーブルを指定して、フルテキスト インデックスを作成します。次に、FullTextIndexColumn オブジェクトを使用して、テーブル内の列の名前を指定し、フルテキスト インデックスのインデックス列を追加します。次に、CatalogName プロパティを、作成したカタログに設定します。最後に、Create メソッドを呼び出して、SQL Server のインスタンスにフルテキスト インデックスを作成します。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」または「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください。

Visual Basic でのフルテキスト検索サービスの作成

このコード例では、AdventureWorks2008R2 サンプル データベースの ProductCategory テーブルに対してフルテキスト検索カタログを作成します。次に、ProductCategory テーブル内の Name 列にフルテキスト検索インデックスを作成します。フルテキスト検索インデックスを作成する列には、既に定義されている一意のインデックスが存在する必要があります。

' compile with: 
' /r:Microsoft.SqlServer.SqlEnum.dll 
' /r:Microsoft.SqlServer.Smo.dll 
' /r:Microsoft.SqlServer.ConnectionInfo.dll 
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Sdk.Sfc
Imports Microsoft.SqlServer.Management.Common

Public Class A
   Public Shared Sub Main()
      ' Connect to the local, default instance of SQL Server.
      Dim srv As Server = Nothing
      srv = New Server()

      ' Reference the AdventureWorks database.
      Dim db As Database = Nothing
      db = srv.Databases("AdventureWorks")

      ' Reference the ProductCategory table.
      Dim tb As Table = Nothing
      tb = db.Tables("ProductCategory", "Production")

      ' Define a FullTextCatalog object variable by specifying the parent database and name arguments in the constructor.
      Dim ftc As FullTextCatalog = Nothing
      ftc = New FullTextCatalog(db, "Test_Catalog")
      ftc.IsDefault = True

      ' Create the Full-Text Search catalog on the instance of SQL Server.
      ftc.Create()

      ' Define a FullTextIndex object varaible by supplying the parent table argument in the constructor.
      Dim fti As FullTextIndex = Nothing
      fti = New FullTextIndex(tb)

      ' Define a FullTextIndexColumn object variable by supplying the parent index and column name arguements in the constructor.
      Dim ftic As FullTextIndexColumn = Nothing
      ftic = New FullTextIndexColumn(fti, "Name")

      ' Add the indexed column to the index.
      fti.IndexedColumns.Add(ftic)
      fti.ChangeTracking = ChangeTracking.Automatic

      ' Specify the unique index on the table that is required by the Full Text Search index.
      fti.UniqueIndexName = "AK_ProductCategory_Name"

      ' Specify the catalog associated with the index.
      fti.CatalogName = "Test_Catalog"

      ' Create the Full Text Search index on the instance of SQL Server.
      fti.Create()
   End Sub
End Class

Visual C# でのフルテキスト検索サービスの作成

このコード例では、AdventureWorks2008R2 サンプル データベースの ProductCategory テーブルに対してフルテキスト検索カタログを作成します。次に、ProductCategory テーブル内の Name 列にフルテキスト検索インデックスを作成します。フルテキスト検索インデックスを作成する列には、既に定義されている一意のインデックスが存在する必要があります。

// compile with: 
// /r:Microsoft.SqlServer.SqlEnum.dll 
// /r:Microsoft.SqlServer.Smo.dll 
// /r:Microsoft.SqlServer.ConnectionInfo.dll 
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Common;

public class A {
   public static void Main() {
      // Connect to the local, default instance of SQL Server.
      Server srv = default(Server);
      srv = new Server();

      // Reference the AdventureWorks database.
      Database db = default(Database);
      db = srv.Databases ["AdventureWorks"];

      // Reference the ProductCategory table.
      Table tb = default(Table);
      tb = db.Tables["ProductCategory", "Production"];

      // Define a FullTextCatalog object variable by specifying the parent database and name arguments in the constructor.
      FullTextCatalog ftc = default(FullTextCatalog);
      ftc = new FullTextCatalog(db, "Test_Catalog");
      ftc.IsDefault = true;

      // Create the Full-Text Search catalog on the instance of SQL Server.
      ftc.Create();

      // Define a FullTextIndex object varaible by supplying the parent table argument in the constructor.
      FullTextIndex fti = default(FullTextIndex);
      fti = new FullTextIndex(tb);

      // Define a FullTextIndexColumn object variable by supplying the parent index and column name arguements in the constructor.
      FullTextIndexColumn ftic = default(FullTextIndexColumn);
      ftic = new FullTextIndexColumn(fti, "Name");

      // Add the indexed column to the index.
      fti.IndexedColumns.Add(ftic);
      fti.ChangeTracking = ChangeTracking.Automatic;

      // Specify the unique index on the table that is required by the Full Text Search index.
      fti.UniqueIndexName = "AK_ProductCategory_Name";

      // Specify the catalog associated with the index.
      fti.CatalogName = "Test_Catalog";

      // Create the Full Text Search index on the instance of SQL Server.
      fti.Create();
   }
}

PowerShell でのフルテキスト検索サービスの作成

このコード例では、AdventureWorks2008R2 サンプル データベースの ProductCategory テーブルに対してフルテキスト検索カタログを作成します。次に、ProductCategory テーブル内の Name 列にフルテキスト検索インデックスを作成します。フルテキスト検索インデックスを作成する列には、既に定義されている一意のインデックスが存在する必要があります。

# Example of implementing a full text search on the default instance.
# Set the path context to the local, default instance of SQL Server and database tables

CD \sql\localhost\default\databases
$db = get-item AdventureWorks2008R2

CD AdventureWorks\tables

#Get a reference to the table
$tb = get-item Production.ProductCategory


# Define a FullTextCatalog object variable by specifying the parent database and name arguments in the constructor.

$ftc = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextCatalog -argumentlist $db, "Test_Catalog2"
$ftc.IsDefault = $true

# Create the Full Text Search catalog on the instance of SQL Server.
$ftc.Create()

# Define a FullTextIndex object variable by supplying the parent table argument in the constructor.
$fti = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextIndex -argumentlist $tb

#  Define a FullTextIndexColumn object variable by supplying the parent index 
#  and column name arguments in the constructor.

$ftic = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextIndexColumn -argumentlist $fti, "Name"

# Add the indexed column to the index.
$fti.IndexedColumns.Add($ftic)

# Set change tracking
$fti.ChangeTracking = [Microsoft.SqlServer.Management.SMO.ChangeTracking]::Automatic

# Specify the unique index on the table that is required by the Full Text Search index.
$fti.UniqueIndexName = "AK_ProductCategory_Name"

# Specify the catalog associated with the index.
$fti.CatalogName = "Test_Catalog2"

# Create the Full Text Search Index
$fti.Create()