次の方法で共有


Full-Text 検索の実装

フルテキスト検索は SQL Server のインスタンスごとに使用でき、 FullTextService オブジェクトによって SMO で表されます。 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 での Full-Text Search Service の作成

このコード例では、AdventureWorks2012 サンプル データベースの 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 arguments 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 での Full-Text Search Service の作成#

このコード例では、AdventureWorks2012 サンプル データベースの 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 arguments 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 で Full-Text Search サービスを作成する

このコード例では、AdventureWorks2012 サンプル データベースの 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 AdventureWorks2012  
  
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()