Bagikan melalui


Menerapkan Pencarian Teks Lengkap

Berlaku untuk:SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse AnalyticsSQL database di Microsoft Fabric

Pencarian teks lengkap tersedia per instans SQL Server dan diwakili dalam SMO oleh FullTextService objek . Objek FullTextService berada di bawah objek Server . Ini digunakan untuk mengelola opsi konfigurasi untuk Microsoft Full Text layanan Pencarian. Objek FullTextCatalogCollection milik Database objek dan merupakan kumpulan FullTextCatalog objek yang mewakili katalog teks lengkap yang ditentukan untuk database. Anda hanya dapat memiliki satu indeks teks lengkap yang ditentukan untuk setiap tabel, tidak seperti indeks normal. Ini diwakili oleh FullTextIndexColumn objek dalam Table objek.

Untuk membuat layanan pencarian teks lengkap, Anda harus memiliki katalog teks lengkap yang ditentukan pada database dan indeks pencarian teks lengkap yang ditentukan pada salah satu tabel dalam database.

Pertama, buat katalog teks lengkap pada database dengan memanggil FullTextCatalog konstruktor dan menentukan nama katalog. Kemudian, buat indeks teks lengkap dengan memanggil konstruktor dan tentukan tabel tempat indeks tersebut akan dibuat. Anda kemudian dapat menambahkan kolom indeks untuk indeks teks lengkap, dengan menggunakan FullTextIndexColumn objek dan memberikan nama kolom dalam tabel. Kemudian, atur CatalogName properti ke katalog yang telah Anda buat. Terakhir, panggil Create metode dan buat indeks teks lengkap pada instans SQL Server.

Example

Untuk menggunakan contoh kode apa pun yang disediakan, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.

Membuat Layanan Pencarian Teks Lengkap di Visual Basic

Contoh kode ini membuat katalog pencarian teks lengkap untuk ProductCategory tabel dalam database sampel AdventureWorks2025. Kemudian membuat indeks pencarian teks lengkap pada kolom Nama dalam ProductCategory tabel. Indeks pencarian teks lengkap mengharuskan ada indeks unik yang sudah ditentukan pada 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  

Membuat Layanan Pencarian Teks Lengkap di Visual C#

Contoh kode ini membuat katalog pencarian teks lengkap untuk ProductCategory tabel dalam database sampel AdventureWorks2025. Kemudian membuat indeks pencarian teks lengkap pada kolom Nama dalam ProductCategory tabel. Indeks pencarian teks lengkap mengharuskan ada indeks unik yang sudah ditentukan pada 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();  
   }  
}  

Membuat Layanan Pencarian Teks Lengkap di PowerShell

Contoh kode ini membuat katalog pencarian teks lengkap untuk ProductCategory tabel dalam database sampel AdventureWorks2025. Kemudian membuat indeks pencarian teks lengkap pada kolom Nama dalam ProductCategory tabel. Indeks pencarian teks lengkap mengharuskan ada indeks unik yang sudah ditentukan pada 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()