Condividi tramite


Implementazione della ricerca full-text

La ricerca full-text è disponibile per istanza di SQL Server ed è rappresentata in SMO dall'oggetto FullTextService. L'oggetto FullTextService si trova sotto l'oggetto Server. Viene utilizzato per gestire le opzioni di configurazione per il servizio di ricerca full-text di Microsoft. L'oggetto FullTextCatalogCollection appartiene all'oggetto Database e si tratta di una raccolta di oggetti FullTextCatalog che rappresentano cataloghi full-text definiti per il database. È possibile disporre di un solo indice full-text definito per ogni tabella, a differenza degli indici normali. Questo indice è rappresentato da un oggetto FullTextIndexColumn nell'oggetto Table.

Per creare un servizio di ricerca full-text, è necessario disporre di un catalogo full-text definito nel database e di un indice di ricerca full-text definito in una delle tabelle del database.

Creare innanzitutto un catalogo full-text nel database chiamando il costruttore FullTextCatalog e specificando il nome del catalogo. Successivamente, creare l'indice full-text chiamando il costruttore e specificando la tabella nella quale deve essere creato. È quindi possibile aggiungere colonne per l'indice full-text tramite l'oggetto FullTextIndexColumn e fornendo il nome della colonna all'interno della tabella. Successivamente, impostare la proprietà CatalogName sul catalogo creato. Infine, chiamare il metodo Create e creare l'indice full-text nell'istanza di SQL Server.

Esempio

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente, il modello e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere Procedura: Creazione di un progetto Visual Basic SMO in Visual Studio .NET o Procedura: Creazione di un progetto Visual C# SMO in Visual Studio .NET.

Creazione di un servizio di ricerca full-text in Visual Basic

Nel codice di esempio seguente viene creato un catalogo di ricerca full-text per la tabella ProductCategory nel database di esempio AdventureWorks2008R2. Successivamente, viene creato un indice di ricerca full-text nella colonna Name della tabella ProductCategory. Per l'indice di ricerca full-text è necessario che nella colonna sia già definito un indice univoco.

' 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

Creazione di un servizio di ricerca full-text in Visual C#

Nel codice di esempio seguente viene creato un catalogo di ricerca full-text per la tabella ProductCategory nel database di esempio AdventureWorks2008R2. Successivamente, viene creato un indice di ricerca full-text nella colonna Name della tabella ProductCategory. Per l'indice di ricerca full-text è necessario che nella colonna sia già definito un indice univoco.

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

Creazione di un servizio di ricerca full-text in PowerShell

Nel codice di esempio seguente viene creato un catalogo di ricerca full-text per la tabella ProductCategory nel database di esempio AdventureWorks2008R2. Successivamente, viene creato un indice di ricerca full-text nella colonna Name della tabella ProductCategory. Per l'indice di ricerca full-text è necessario che nella colonna sia già definito un indice univoco.

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