Delen via


Full-Text zoeken implementeren

Van toepassing op:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL-database in Microsoft Fabric Preview

Zoeken in volledige tekst is beschikbaar per exemplaar van SQL Server en wordt weergegeven in SMO door het FullTextService object. Het FullTextService object bevindt zich onder het Server-object . Het wordt gebruikt voor het beheren van de configuratieopties voor de microsoft Full Text Search-service. Het FullTextCatalogCollection object behoort tot het Database object en het is een verzameling FullTextCatalog objecten die volledige-tekstcatalogussen vertegenwoordigen die zijn gedefinieerd voor de database. U kunt slechts één volledige-tekstindex definiëren voor elke tabel, in tegenstelling tot normale indexen. Dit wordt vertegenwoordigd door een FullTextIndexColumn object in het Table object.

Als u een zoekservice voor volledige tekst wilt maken, moet u een volledige tekstcatalogus hebben gedefinieerd voor de database en een zoekindex voor volledige tekst die is gedefinieerd in een van de tabellen in de database.

Maak eerst een catalogus met volledige tekst in de database door de FullTextCatalog constructor aan te roepen en de catalogusnaam op te geven. Maak vervolgens de index voor volledige tekst door de constructor aan te roepen en de tabel op te geven waarop deze moet worden gemaakt. Vervolgens kunt u indexkolommen toevoegen voor de volledige-tekstindex door het FullTextIndexColumn object te gebruiken en de naam van de kolom in de tabel op te geven. Stel vervolgens de CatalogName eigenschap in op de catalogus die u hebt gemaakt. Roep tot slot de Create methode aan en maak de volledige-tekstindex op het exemplaar van SQL Server.

Example

Als u een codevoorbeeld wilt gebruiken dat is opgegeven, moet u de programmeeromgeving, de programmeersjabloon en de programmeertaal kiezen waarin u uw toepassing wilt maken. Zie Een Visual C# SMO-project maken in Visual Studio .NETvoor meer informatie.

Een Full-Text Search Service maken in Visual Basic

In dit codevoorbeeld wordt een zoekcatalogus met volledige tekst gemaakt voor de ProductCategory tabel in de voorbeelddatabase AdventureWorks2022. Vervolgens wordt er een zoekindex voor volledige tekst gemaakt in de kolom Naam in de ProductCategory tabel. De zoekindex voor volledige tekst vereist dat er al een unieke index is gedefinieerd in de kolom.

' 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 variable 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  

Een Full-Text Search Service maken in Visual C#

In dit codevoorbeeld wordt een zoekcatalogus met volledige tekst gemaakt voor de ProductCategory tabel in de voorbeelddatabase AdventureWorks2022. Vervolgens wordt er een zoekindex voor volledige tekst gemaakt in de kolom Naam in de ProductCategory tabel. De zoekindex voor volledige tekst vereist dat er al een unieke index is gedefinieerd in de kolom.

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

Een Full-Text Search Service maken in PowerShell

In dit codevoorbeeld wordt een zoekcatalogus met volledige tekst gemaakt voor de ProductCategory tabel in de voorbeelddatabase AdventureWorks2022. Vervolgens wordt er een zoekindex voor volledige tekst gemaakt in de kolom Naam in de ProductCategory tabel. De zoekindex voor volledige tekst vereist dat er al een unieke index is gedefinieerd in de kolom.

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