Partager via


Mise en œuvre de la recherche Full-Text

La recherche en texte intégral est disponible par instance de SQL Server et est représentée dans SMO par l’objet FullTextService . L’objet FullTextService se trouve sous l’objet Server . Il est utilisé pour gérer les options de configuration du service Recherche en texte intégral Microsoft. L’objet FullTextCatalogCollection appartient à l’objet Database et il s’agit d’une collection d’objets FullTextCatalog qui représentent des catalogues de texte intégral définis pour la base de données. Vous ne pouvez avoir qu’un seul index de recherche en texte intégral défini pour chaque table, contrairement aux index normaux. Il s’agit d’un FullTextIndexColumn objet dans l’objet Table .

Pour créer un service de recherche en texte intégral, vous devez disposer d’un catalogue de texte intégral défini sur la base de données et d’un index de recherche en texte intégral défini sur l’une des tables de la base de données.

Tout d’abord, créez un catalogue de texte intégral sur la base de données en appelant le FullTextCatalog constructeur et en spécifiant le nom du catalogue. Ensuite, créez l’index de recherche en texte intégral en appelant le constructeur et en spécifiant la table sur laquelle il doit être créé. Vous pouvez ensuite ajouter des colonnes d’index pour l’index de recherche en texte intégral, à l’aide de l’objet FullTextIndexColumn et en fournissant le nom de la colonne dans la table. Ensuite, définissez la CatalogName propriété sur le catalogue que vous avez créé. Enfin, appelez la Create méthode et créez l’index de recherche en texte intégral sur l’instance de SQL Server.

Exemple :

Pour utiliser un exemple de code fourni, vous devez choisir l’environnement de programmation, le modèle de programmation et le langage de programmation dans lequel créer votre application. Pour plus d’informations, consultez Créer un projet SMO Visual Basic dans Visual Studio .NET ou créer un projet SMO Visual C# dans Visual Studio .NET.

Création d’un service de recherche Full-Text en Visual Basic

Cet exemple de code crée un catalogue de recherche en texte intégral pour la ProductCategory table dans l’exemple de base de données AdventureWorks2012. Il crée ensuite un index de recherche en texte intégral sur la colonne Name de la ProductCategory table. L’index de recherche en texte intégral nécessite qu’il existe un index unique déjà défini sur la colonne.

' 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  

Création d’un service de recherche Full-Text dans Visual C#

Cet exemple de code crée un catalogue de recherche en texte intégral pour la ProductCategory table dans l’exemple de base de données AdventureWorks2012. Il crée ensuite un index de recherche en texte intégral sur la colonne Name de la ProductCategory table. L’index de recherche en texte intégral nécessite qu’il existe un index unique déjà défini sur la colonne.

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

Création d’un service de recherche Full-Text dans PowerShell

Cet exemple de code crée un catalogue de recherche en texte intégral pour la ProductCategory table dans l’exemple de base de données AdventureWorks2012. Il crée ensuite un index de recherche en texte intégral sur la colonne Name de la ProductCategory table. L’index de recherche en texte intégral nécessite qu’il existe un index unique déjà défini sur la colonne.

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