Share via


Implementar la búsqueda de texto completo

La búsqueda de texto completo está disponible por instancia de SQL Server y se representa en SMO mediante el objeto FullTextService. El objeto FullTextService reside bajo el objeto Server. Se utiliza para administrar las opciones de configuración del servicio de búsqueda de texto completo de Microsoft. El objeto FullTextCatalogCollection pertenece al objeto Database y es una recopilación de los objetos FullTextCatalog que representan los catálogos de texto completo definidos para la base de datos. Sólo puede tener un índice de texto completo definido para cada tabla, a diferencia de los índices normales. Un objeto FullTextIndexColumn representa esto en el objeto Table.

Para crear un servicio de búsqueda de texto completo, debe tener un catálogo de texto completo definido en la base de datos y un índice de búsqueda de texto completo definido en una de las tablas en la base de datos.

Primero, cree un catálogo de texto completo en la base de datos llamando al constructor FullTextCatalog y especificando el nombre del catálogo. A continuación, cree el índice de texto llamando al constructor y especificando la tabla en la que se creará. A continuación, puede agregar columnas de índices al índice de texto completo, utilizando el objeto FullTextIndexColumn y proporcionando el nombre de la columna de la tabla. A continuación, establezca la propiedad CatalogName en el catálogo que ha creado. Por último, llame al método Create y cree el índice de texto completo en la instancia de SQL Server.

Ejemplo

Para utilizar cualquier ejemplo de código que se proporcione, deberá elegir el entorno de programación, la plantilla de programación y el lenguaje de programación en los que crear su aplicación. Para obtener más información, vea Cómo crear un proyecto de Visual Basic SMO en Visual Studio .NET o Cómo crear un proyecto de Visual C# SMO en Visual Studio .NET.

Crear un servicio de búsqueda de texto completo en Visual Basic

En este ejemplo de código se crea un catálogo de búsqueda de texto completo para la tabla ProductCategory de la base de datos de ejemplo AdventureWorks. A continuación, crea un índice de búsqueda de texto completo en la columna Nombre en la tabla ProductCategory. El índice de búsqueda de texto completo requiere que ya haya un índice único definido en la columna.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Reference the ProductCategory table.
Dim tb As Table
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
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
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
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()

Crear un servicio de búsqueda de texto completo en Visual C#

En este ejemplo de código se crea un catálogo de búsqueda de texto completo para la tabla ProductCategory de la base de datos de ejemplo AdventureWorks. A continuación, crea un índice de búsqueda de texto completo en la columna Nombre en la tabla ProductCategory. El índice de búsqueda de texto completo requiere que sea un índice único ya definido en la columna.

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