Freigeben über


Einbinden einer Volltextsuche

Volltextsuche ist pro Instanz von SQL Server verfügbar und wird durch das FullTextService-Objekt in SMO dargestellt. Das FullTextService-Objekt befindet sich unter dem Server-Objekt. Es wird verwendet, um die Konfigurationsoptionen für den Dienst Microsoft-Volltextsuche zu verwalten. Das FullTextCatalogCollection-Objekt gehört zum Database-Objekt und ist eine Auflistung von FullTextCatalog-Objekten, die Volltextkataloge darstellen, die für die Datenbank definiert sind. Im Gegensatz zu normalen Indizes kann nur ein Volltextindex für jede Tabelle definiert sein. Dies wird durch ein FullTextIndexColumn-Objekt im Table-Objekt dargestellt.

Um einen Volltextsuchdienst zu erstellen, muss auf der Datenbank ein Volltextkatalog und in einer der Tabellen in der Datenbank ein Index für die Volltextsuche definiert sein.

Erstellen Sie zunächst einen Volltextkatalog auf der Datenbank, indem Sie den FullTextCatalog-Konstruktor aufrufen und einen Katalognamen angeben. Erstellen Sie dann den Volltextindex, indem Sie den Konstruktor aufrufen und die Tabelle angeben, in der dieser erstellt werden soll. Daraufhin können Sie Indexspalten für die Volltextsuche hinzufügen, indem Sie das FullTextIndexColumn-Objekt verwenden und den Namen der Spalte in der Tabelle angeben. Legen Sie dann die CatalogName-Eigenschaft auf den Katalog fest, den Sie erstellt haben. Rufen Sie zum Schluss die Create-Methode auf, und erstellen Sie den Volltextindex auf der Instanz von SQL Server.

Beispiel

Um die bereitgestellten Codebeispiele verwenden zu können, müssen Sie die Programmierumgebung, die Programmiervorlage und die Programmiersprache wählen, in der die Anwendung erstellt werden soll. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines Visual Basic-SMO-Projekts in Visual Studio .NET oder Vorgehensweise: Erstellen eines Visual C#-SMO-Projekts in Visual Studio .NET.

Erstellen eines Volltextsuchdiensts in Visual Basic

In diesem Codebeispiel wird ein Volltextsuchkatalog für die ProductCategory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt. Anschließend wird für die Name-Spalte in der ProductCategory-Tabelle ein Index für die Volltextsuche erstellt. Der Index für die Volltextsuche erfordert, dass bereits ein eindeutiger Index für die Spalte definiert ist.

' 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

Erstellen eines Volltextsuchdiensts in Visual C#

In diesem Codebeispiel wird ein Volltextsuchkatalog für die ProductCategory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt. Anschließend wird für die Name-Spalte in der ProductCategory-Tabelle ein Index für die Volltextsuche erstellt. Der Index für die Volltextsuche erfordert, dass bereits ein eindeutiger Index für die Spalte definiert ist.

// 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();
   }
}

Erstellen eines Volltextsuchdiensts in PowerShell

In diesem Codebeispiel wird ein Volltextsuchkatalog für die ProductCategory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt. Anschließend wird für die Name-Spalte in der ProductCategory-Tabelle ein Index für die Volltextsuche erstellt. Der Index für die Volltextsuche erfordert, dass bereits ein eindeutiger Index für die Spalte definiert ist.

# 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()