Learning is one of the most important mental function of humans, animals and artificial cognitive systems. It relies on the acquisition of different types of knowledge supported by perceived information. It leads to the development of new capacities, skills, values, understanding, and preferences. Its goal is the increasing of individual and group experience. Learning functions can be performed by different brain learning processes, which depend on the mental capacities of learning subject, the type of knowledge which has to be acquitted, as well as on socio-cognitive and environmental circumstances[1].
Learning ranges from simple forms of learning such as habituation and classical conditioning seen in many animal species, to more complex activities such as play, seen only in relatively intelligent animals[2][3] and humans. Therefore, in general, a learning can be conscious and not conscious.
For example, for small children, not conscious learning processes are as natural as breathing. In fact, there is evidence for behavioral learning prenatally, in which habituation has been observed as early as 32 weeks into gestation, indicating that the central nervous system is sufficiently developed and primed for learning and memory to occur very early on in development.[4]
From the social perspective, learning is the goal of teaching and education.
Conscious learning is a capacity requested by students, therefore is usually goal-oriented and requires a motivation.
Learning has also been mathematically modeled using a differential equation related to an arbitrarily defined knowledge indicator with respect to time, and dependent on a number of interacting factors (constants and variables) such as initial knowledge, motivation, intelligence, knowledge anchorage or resistance, etc.[5][6] Thus, learning does not occur if there is no change in the amount of knowledge even for a long time, and learning is negative if the amount of knowledge is decreasing in time. Inspection of the solution to the differential equation also shows the sigmoid and logarithmic decay learning curves, as well as the knowledge carrying capacity for a given learner.
"Thought," in a general sense, is commonly conceived as something arising from the stimulation of neurons in the brain. Current understanding of neurons and the central nervous system implies that the process of learning corresponds to changes in the relationship between certain neurons in the brain. Research is ongoing in this area.[citation needed]
It is generally recognized that memory is more easily retained when multiple parts of the brain are stimulated, such as through combinations of hearing, seeing, smelling, motor skills, touch sense, and logical thinking.[citation needed]
Repeating thoughts and actions is an essential part of learning. Thinking about a specific memory will make it easy to recall. This is the reason why reviews are such an integral part of education. On first performing a task, it is difficult as according to current theory synaptic modification is necessary for the task to be acquired. After several repetitions it is believed that structural changes occur in relevant synapses, thus rendering the task easier. When the task becomes so easy that you can perform it at any time, these structural changes have likely ceased.
Types of learning
Simple non-associative learning
Habituation
Main article: Habituation
In psychology, habituation is an example of non-associative learning in which there is a progressive diminution of behavioral response probability with repetition of a stimulus. It is another form of integration. An animal first responds to a stimulus, but if it is neither rewarding nor harmful the animal reduces subsequent responses. One example of this can be seen in small song birds - if a stuffed owl (or similar predator) is put into the cage, the birds initially react to it as though it were a real predator. Soon the birds react less, showing habituation. If another stuffed owl is introduced (or the same one removed and re-introduced), the birds react to it again as though it were a predator, demonstrating that it is only a very specific stimulus that is habituated to (namely, one particular unmoving owl in one place). Habituation has been shown in essentially every species of animal, including the large protozoan Stentor coeruleus.[7]
Sensitization
Main article: Sensitization
Sensitization is an example of non-associative learning in which the progressive amplification of a response follows repeated administrations of a stimulus (Bell et al., 1995). An everyday example of this mechanism is the repeated tonic stimulation of peripheral nerves that will occur if a person rubs his arm continuously. After a while, this stimulation will create a warm sensation that will eventually turn painful. The pain is the result of the progressively amplified synaptic response of the peripheral nerves warning the person that the stimulation is harmful. Sensitization is thought to underlie both adaptive as well as maladaptive learning processes in the organism.
Associative learning
Operant conditioning
Main article: Operant conditioning
Operant conditioning is the use of consequences to modify the occurrence and form of behavior. Operant conditioning is distinguished from Pavlovian conditioning in tyat operant conditioning deals with the modification of voluntary behavior. Discrimination learning is a major form of operant conditioning. One form of it is called Errorless learning.
Classical conditioning
Main article: Classical conditioning
The typical paradigm for classical conditioning involves repeatedly pairing an unconditioned stimulus (which unfailingly evokes a particular response) with another previously neutral stimulus (which does not normally evoke the response). Following conditioning, the response occurs both to the unconditioned stimulus and to the other, unrelated stimulus (now referred to as the "conditioned stimulus"). The response to the conditioned stimulus is termed a conditioned response.
Imprinting
Main article: Imprinting (psychology)
Imprinting is the term used in psychology and ethology to describe any kind of phase-sensitive learning (learning occurring at a particular age or a particular life stage) that is rapid and apparently independent of the consequences of behavior. It was first used to describe situations in which an animal or person learns the characteristics of some stimulus, which is therefore said to be "imprinted" onto the subject.
Observational learning
Main article: Observational learning
The most basic learning process is imitation; one's personal repetition of an observed process, such as a smile. Thus an imitation will take one's time (attention to the details), space (a location for learning), skills (or practice), and other resources (for example, a protected area). Through copying, most infants learn how to hunt (i.e., direct one's attention), feed and perform most basic tasks necessary for survival.
Play
Main article: Play (activity)
Play generally describes behavior which has no particular end in itself, but improves performance in similar situations in the future. This is seen in a wide variety of vertebrates besides humans, but is mostly limited to mammals and birds. Cats are known to play with a ball of string when young, which gives them experience with catching prey. Besides inanimate objects, animals may play with other members of their own species or other animals, such as orcas playing with seals they have caught. Play involves a significant cost to animals, such as increased vulnerability to predators and the risk or injury and possibly infection. It also consumes energy, so there must be significant benefits associated with play for it to have evolved. Play is generally seen in younger animals, suggesting a link with learning. However, it may also have other benefits not associated directly with learning, for example improving physical fitness.
Multimedia learning
The learning where learner uses multimedia learning environments (Mayer, 2001). This type of learning relies on dual-coding theory (Paivio, 1971).
e-Learning and m-Learning
Electronic learning or e-learning is a general term used to refer to Internet-based networked computer-enhanced learning. A specific and always more diffused e-learning is mobile learning (m-Learning), it uses different mobile telecommunication equipments, such as cellular phones.
Machine learning
Main article: Machine learning
Although learning is often thought of as a property associated with living things, computers are also able to modify their own behaviors as a result of experiences. Known as machine learning, this is a broad subfield of artificial intelligence concerned with the design and development of algorithms and techniques that allow computers to "learn". At a general level, there are two types of learning: inductive, and deductive. Inductive machine learning methods extract rules and patterns out of massive data sets.
The major focus of machine learning research is to extract information from data automatically, by computational and statistical methods. Hence, machine learning is closely related to data mining and statistics but also theoretical computer science.
Machine learning has a wide spectrum of applications including natural language processing, syntactic pattern recognition, search engines, medical diagnosis, bioinformatics and cheminformatics, detecting credit card fraud, stock market analysis, classifying DNA sequences, speech and handwriting recognition, object recognition in computer vision, game playing and robot locomotion.
Approaches to learning
Rote learning
Main article: Rote learning
Rote learning is a technique which avoids understanding the inner complexities and inferences of the subject that is being learned and instead focuses on memorizing the material so that it can be recalled by the learner exactly the way it was read or heard. The major practice involved in rote learning techniques is learning by repetition, based on the idea that one will be able to quickly recall the meaning of the material the more it is repeated. Rote learning is used in diverse areas, from mathematics to music to religion. Although it has been criticized by some schools of thought, rote learning is a necessity in many situations.
Informal learning
Main article: Informal learning
Informal learning occurs through the experience of day-to-day situations (for example, one would learn to look ahead while walking because of the danger inherent in not paying attention to where one is going). It is learning from life, during a meal at table with parents, Play, exploring.
Formal learning
Main article: Education
A depiction of the world's oldest university, the University of Bologna, Italy
A depiction of the world's oldest university, the University of Bologna, Italy
Formal learning is learning that takes place within a teacher-student relationship, such as in a school system.
Non-formal learning is organized learning outside the formal learning system. For example: learning by coming together with people with similar interests and exchanging viewpoints, in clubs or in (international) youth organizations, workshops.
Non-formal learning and combined approaches
The educational system may use a combination of formal, informal, and non-formal learning methods. The UN and EU recognize these different forms of learning (cf. links below). In some schools students can get points that count in the formal-learning systems if they get work done in informal-learning circuits. They may be given time to assist international youth workshops and training courses, on the condition they prepare, contribute, share and can proof this offered valuable new insights, helped to acquire new skills, a place to get experience in organizing, teaching, etc.
In order to learn a skill, such as solving a Rubik's cube quickly, several factors come into play at once:
* Directions help one learn the patterns of solving a Rubik's cube
* Practicing the moves repeatedly and for extended time helps with "muscle memory" and therefore speed
* Thinking critically about moves helps find shortcuts, which in turn helps to speed up future attempts.
* The Rubik's cube's six colors help anchor solving it within the head.
* Occasionally revisiting the cube helps prevent negative learning or loss of skill
Saturday, June 21, 2008
Saturday, May 31, 2008
Controlling SQL Text Indexing Services with SQL Managed Objects and VB.NET
Some applications create whole databases and tables dynamically. Entity Diagramming tools such as ERwin generate databases. Application builders or customizers—like ACT—modify databases, and applications that permit customized data storage sometimes generate new tables or even whole databases.
The challenge with dynamic generation is that your application is already running, and it is unlikely that you will be there to perform administrative tasks, such as enabling full text indexing.
Full text indexing uses an indexing service and permits you to index text fields in SQL Server. The benefit is that you can use functions like FREETEXT in SQL Server queries to query one or more fields for fragments of text. The net effect is that full text indexing makes your database capable of performing Google-like searches. That is, with full text indexing you can let the user enter any text data and SQL Server can search multiple fields for that fragment with expressing a WHERE predicate for every combination. Instead of WHERE CustomerName LIKE 'somedata' OR ADDRESS CONTAINS 'somedata', and so forth, you write one predicate using the FREETEXT function and SQL Server searches all of the indexed columns for that text.
Here is where full text searching is really useful. Many, many applications support search and find capabilities. Many of these applications typically have a label indicating the kind of data that will be used to build the filter and an input control of some sort. Based on the controls containing values a dynamic search is assembled with as many WHERE predicates as there are kinds of input data. All of these labels, controls, and dynamic assemblage of queries are time consuming.
The no-brainer is that Google is a metaphor that everyone seems to get; hence its popularity. So, why not have one input field every time where the user can input any data and let the indexing service and full text indexing find the matches for you? Full text indexing makes this possible. My article from August 2005, FREETEXT Searches with SQL Server and ADO.NET, demonstrates how to configure indexing services manually and perform full text searches. (This article builds on that concept in a different way.)
The aforementioned article walks you through FREETEXT searches and manually configuring indexing services. This article shows you how to enable full text indexing and FREETEXT searches dynamically.
With SQL Server 2005, the SQL Management Objects (SMO) capabilities were added to SQL Server and the .NET framework. The result is that many of the things that one could do manually using SQL Server tools can now be accomplished programmatically using SMO and VB.NET. In this article, you will learn how to support full text searches on SQL Server Express 2005—it's already supported in non-Express versions—enable full text indexing with SMO, and write FREETEXT queries against indexed columns, all with .NET code.
Preparing the Demo
When you install Visual Studio 2005 or up, SQL Server Express 2005 is installed. Some of you may have access only to SQL Express and some more of you will be developing on your workstation with SQL Server Express. Unfortunately, the out-of-the-box install doesn't support full text indexing for SQL Server Express. Therefore, if you are using SQL Server Express and you want to try the full text indexing demo, read the next paragraph. Otherwise, if you are using SQL 2005 developer edition or above, you can skip the next paragraph.
Downloading SQL Server Express 2005 with Advanced Services SP2
For SQL Express users, full text indexing is not supported. If you want to try the demo and be able to perform FREETEXT queries with SQL Server Express, download the SQL Server Express 2005 with Advanced services SP2 (Service Pack 2) update at http://msdn.microsoft.com/en-us/express/bb410792.aspx. This service pack contains support for full text indexing.
Tip
SP2 also contains SQL Server Management Studio Express. If you already have the enterprise client tools installed (SSMS enterprise) on your workstation, uncheck SSMS Express in the SP2 install. If you don't have SSMS, install the SSMS express client tools during the patch installation.
Reviewing Prerequisites for the Demo
Obviously, you will need access to SQL Server. SQL Express 2005 works if you install Service Pack 2 with Advanced Services. The example program was written in Visual studio 2008, but SMO was released with .NET 2.0 and Visual Studio 2005, so those versions should work too. (Make sure to write me at pkimmel@softconcepts.com if the demo doesn't work in Visual Studio 2005.)
Configuring Full Text Indexing Dynamically and Writing FREETEXT Queries
The sample program assumes an existing database is present. The code configures full text indexing on an nvarchar column, populates the index, and performs a query using the FREETEXT function.
In the sample program, a database that contains company and stock ticker symbols along with historical quotes is used. You can use the Northwind database, or any database, to complete the example. Simply substitute details such as the connection string, database name, table names, and indexes in the code to reflect actual elements in the database you are using.
Adding References
SQL Management Objects (SMO) uses elements of the framework defined in the Microsoft.SqlServer namespace. To try the demo, create a VB.NET application—a console application will do—and add the following additional references:
* Microsoft.SqlServer.ConnectionInfo.dll
* Microsoft.SqlServer.Smo.dll
* Microsoft.SqlServer.SmoEnum.dll
* Microsoft.SqlServer.SqlEnum.dll
Having added the SMO assemblies, you will need these Imports statements to support the code as demonstrated in Listing 1.
* Imports Microsoft.SqlServer.Management.Smo
* Imports Microsoft.SqlServer.Management.Common
* Imports System.Data
* Imports System.Data.SqlClient
If you miss an Imports staement, the compiler and Intellisense will help you fill in the blanks.
Configuring Full Text Searches with SQL Management Objects
The sample code is all in a single Main function in a Console application. This part describes the SMO code in the first half of the listing (see Listing 1) and the next section contains vanilla SQL code with a FREETEXT query. All of the code is shown in Listing 1.
In the Main function in Listing 1, the code from Dim server to the line before the connectionString variable is the SMO code. Everything else is the SQL code.
In Main, a Server object is created. In the example, I am using SQLExpress, as shown in the listing. Using the Server object, a database is requested via the Databases collection, passing in the name of the database. (If you are using Northwind, replace "StockHistory" with "Northwind". From the Database object, request the Table desired by invoking accessing the Database.Tables collection. The code defines a catalog name and checks to see whether the catalog already exists. If the catalog doesn't exist, the catalog is created by creating a new FullTextCatalog object passing in the DB object and the catalog name. This catalog is set as the default and the FullTextCatalog.Create method is called.
Next, the Table object is used to determine whether a FullTextIndex exists. If not, a new FullTextIndex is created passing in the Table object. A FullTextIndexColumn object is created passing in the FullTextIndex object and the column-name to index. The column is added to the FullTextIndex.IndexedColumns property. The implication is that you can index multiple columns. (In practice, index every column that you may want to search on.) The SMO code is wrapped up enabling change tracking, providing a unique key—use the primary key column, which is required—and associate the catalog name with the index. Invoke FullTextIndex.Create and StartPopulation.
Populating the index with an incremental population will permit you to start querying the full text indexes immediately, although on large databases some results may not be found until the index is substantially complete. If you elect to perform a full population, the database may be unavailable until indexing is complete.
Querying the Database Using FREETEXT
As written, the code supports immediate querying using the indexes. The code in Listing 1, starting with the connectionString variable, contains the vanilla ADO.NET code. The description of the SQL code is provided after the listing.
Listing 1: The complete list creates the full text index with SMO and uses the index to perform a FREETEXT query.
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim server As Server = New Server(".\SQLExpress")
Dim db As Database = server.Databases("StockHistory")
Dim table As Table = db.Tables("Company")
Dim catalogName As String = "Company_Catalog"
If (db.FullTextCatalogs(catalogName) Is Nothing) Then
Dim catalog As FullTextCatalog = _
New FullTextCatalog(db, catalogName)
catalog.IsDefault = True
catalog.Create()
End If
If (table.FullTextIndex Is Nothing) Then
Dim index As FullTextIndex = New FullTextIndex(table)
Dim column1 As FullTextIndexColumn = _
New FullTextIndexColumn(index, "CompanyName")
index.IndexedColumns.Add(column1)
index.ChangeTracking = ChangeTracking.Automatic
index.UniqueIndexName = "PK_Company_1"
index.CatalogName = catalogName
index.Create()
index.StartPopulation(IndexPopulationAction.Incremental)
End If
Dim connectionString As String = _
"Data Source=.\SQLExpress;Initial Catalog=StockHistory;" + _
"Integrated Security=True;Pooling=False"
Using connection As SqlConnection = _
New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = New SqlCommand( _
"SELECT * FROM COMPANY WHERE FREETEXT(*, 'MICROSOFT')", _
connection)
Dim reader As SqlDataReader = command.ExecuteReader
While (reader.Read())
Console.WriteLine("{0}, {1}, {2}", reader("CompanyID"), _
reader("CompanyName"), reader("CompanySymbol"))
End While
Console.ReadLine()
End Using
End Sub
End Module
In the second half of the example, a connectionString is declared. The connectionString is used to initialize a SqlConnection object in a Using statement. The Using statement ensures the SqlConnection is closed and disposed of at the end of the using block. The connection is opened. A SqlCommand object is created. The SqlCommand object contains the SQL text and the connection object. Notice that the WHERE clause contains the FREETEXT predicate.
The FREETEXT predicate supports specifying the columns to search. The asterisk (*) means searching all indexed columns. The text—in the listing, 'MICROSOFT'—indicates the text to look for. Finally, a SqlDataReader is used to read and display the resultset.
As the example, you could have solved the same problem with the following query:
WHERE CompanyName LIKE 'MICROSOFT%'
Clearly, this works. Where LIKE begins to fail is when the text can appear anywhere, when there are multiple columns to check search, and those columns are dependent on specific columns to search as expressed by the user. For example, if you have ten possible fields to search, building the WHERE clause becomes very clumsy; by using FREETEXT and full text Indexing, the WHERE clause changes very little regardless of the fields to search.
Summary
SQL Management Objects (SMO) is the ability to manage SQL programmatically. This capability was added to SQL Server 2005 and the .NET Framework 2.0. In the example, you see how Full text Indexing can be enabled, indexes configured, and the index populated and used—all in the same chunk of code.
There is a lot more to SMO then shown here, but this article should get you started. If nothing else, perhaps you can save time by simply adding a single point of entry search to your applications and letting the Indexing Service and SQL Server figure out how to construct the actual search against the indexes.
The challenge with dynamic generation is that your application is already running, and it is unlikely that you will be there to perform administrative tasks, such as enabling full text indexing.
Full text indexing uses an indexing service and permits you to index text fields in SQL Server. The benefit is that you can use functions like FREETEXT in SQL Server queries to query one or more fields for fragments of text. The net effect is that full text indexing makes your database capable of performing Google-like searches. That is, with full text indexing you can let the user enter any text data and SQL Server can search multiple fields for that fragment with expressing a WHERE predicate for every combination. Instead of WHERE CustomerName LIKE 'somedata' OR ADDRESS CONTAINS 'somedata', and so forth, you write one predicate using the FREETEXT function and SQL Server searches all of the indexed columns for that text.
Here is where full text searching is really useful. Many, many applications support search and find capabilities. Many of these applications typically have a label indicating the kind of data that will be used to build the filter and an input control of some sort. Based on the controls containing values a dynamic search is assembled with as many WHERE predicates as there are kinds of input data. All of these labels, controls, and dynamic assemblage of queries are time consuming.
The no-brainer is that Google is a metaphor that everyone seems to get; hence its popularity. So, why not have one input field every time where the user can input any data and let the indexing service and full text indexing find the matches for you? Full text indexing makes this possible. My article from August 2005, FREETEXT Searches with SQL Server and ADO.NET, demonstrates how to configure indexing services manually and perform full text searches. (This article builds on that concept in a different way.)
The aforementioned article walks you through FREETEXT searches and manually configuring indexing services. This article shows you how to enable full text indexing and FREETEXT searches dynamically.
With SQL Server 2005, the SQL Management Objects (SMO) capabilities were added to SQL Server and the .NET framework. The result is that many of the things that one could do manually using SQL Server tools can now be accomplished programmatically using SMO and VB.NET. In this article, you will learn how to support full text searches on SQL Server Express 2005—it's already supported in non-Express versions—enable full text indexing with SMO, and write FREETEXT queries against indexed columns, all with .NET code.
Preparing the Demo
When you install Visual Studio 2005 or up, SQL Server Express 2005 is installed. Some of you may have access only to SQL Express and some more of you will be developing on your workstation with SQL Server Express. Unfortunately, the out-of-the-box install doesn't support full text indexing for SQL Server Express. Therefore, if you are using SQL Server Express and you want to try the full text indexing demo, read the next paragraph. Otherwise, if you are using SQL 2005 developer edition or above, you can skip the next paragraph.
Downloading SQL Server Express 2005 with Advanced Services SP2
For SQL Express users, full text indexing is not supported. If you want to try the demo and be able to perform FREETEXT queries with SQL Server Express, download the SQL Server Express 2005 with Advanced services SP2 (Service Pack 2) update at http://msdn.microsoft.com/en-us/express/bb410792.aspx. This service pack contains support for full text indexing.
Tip
SP2 also contains SQL Server Management Studio Express. If you already have the enterprise client tools installed (SSMS enterprise) on your workstation, uncheck SSMS Express in the SP2 install. If you don't have SSMS, install the SSMS express client tools during the patch installation.
Reviewing Prerequisites for the Demo
Obviously, you will need access to SQL Server. SQL Express 2005 works if you install Service Pack 2 with Advanced Services. The example program was written in Visual studio 2008, but SMO was released with .NET 2.0 and Visual Studio 2005, so those versions should work too. (Make sure to write me at pkimmel@softconcepts.com if the demo doesn't work in Visual Studio 2005.)
Configuring Full Text Indexing Dynamically and Writing FREETEXT Queries
The sample program assumes an existing database is present. The code configures full text indexing on an nvarchar column, populates the index, and performs a query using the FREETEXT function.
In the sample program, a database that contains company and stock ticker symbols along with historical quotes is used. You can use the Northwind database, or any database, to complete the example. Simply substitute details such as the connection string, database name, table names, and indexes in the code to reflect actual elements in the database you are using.
Adding References
SQL Management Objects (SMO) uses elements of the framework defined in the Microsoft.SqlServer namespace. To try the demo, create a VB.NET application—a console application will do—and add the following additional references:
* Microsoft.SqlServer.ConnectionInfo.dll
* Microsoft.SqlServer.Smo.dll
* Microsoft.SqlServer.SmoEnum.dll
* Microsoft.SqlServer.SqlEnum.dll
Having added the SMO assemblies, you will need these Imports statements to support the code as demonstrated in Listing 1.
* Imports Microsoft.SqlServer.Management.Smo
* Imports Microsoft.SqlServer.Management.Common
* Imports System.Data
* Imports System.Data.SqlClient
If you miss an Imports staement, the compiler and Intellisense will help you fill in the blanks.
Configuring Full Text Searches with SQL Management Objects
The sample code is all in a single Main function in a Console application. This part describes the SMO code in the first half of the listing (see Listing 1) and the next section contains vanilla SQL code with a FREETEXT query. All of the code is shown in Listing 1.
In the Main function in Listing 1, the code from Dim server to the line before the connectionString variable is the SMO code. Everything else is the SQL code.
In Main, a Server object is created. In the example, I am using SQLExpress, as shown in the listing. Using the Server object, a database is requested via the Databases collection, passing in the name of the database. (If you are using Northwind, replace "StockHistory" with "Northwind". From the Database object, request the Table desired by invoking accessing the Database.Tables collection. The code defines a catalog name and checks to see whether the catalog already exists. If the catalog doesn't exist, the catalog is created by creating a new FullTextCatalog object passing in the DB object and the catalog name. This catalog is set as the default and the FullTextCatalog.Create method is called.
Next, the Table object is used to determine whether a FullTextIndex exists. If not, a new FullTextIndex is created passing in the Table object. A FullTextIndexColumn object is created passing in the FullTextIndex object and the column-name to index. The column is added to the FullTextIndex.IndexedColumns property. The implication is that you can index multiple columns. (In practice, index every column that you may want to search on.) The SMO code is wrapped up enabling change tracking, providing a unique key—use the primary key column, which is required—and associate the catalog name with the index. Invoke FullTextIndex.Create and StartPopulation.
Populating the index with an incremental population will permit you to start querying the full text indexes immediately, although on large databases some results may not be found until the index is substantially complete. If you elect to perform a full population, the database may be unavailable until indexing is complete.
Querying the Database Using FREETEXT
As written, the code supports immediate querying using the indexes. The code in Listing 1, starting with the connectionString variable, contains the vanilla ADO.NET code. The description of the SQL code is provided after the listing.
Listing 1: The complete list creates the full text index with SMO and uses the index to perform a FREETEXT query.
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim server As Server = New Server(".\SQLExpress")
Dim db As Database = server.Databases("StockHistory")
Dim table As Table = db.Tables("Company")
Dim catalogName As String = "Company_Catalog"
If (db.FullTextCatalogs(catalogName) Is Nothing) Then
Dim catalog As FullTextCatalog = _
New FullTextCatalog(db, catalogName)
catalog.IsDefault = True
catalog.Create()
End If
If (table.FullTextIndex Is Nothing) Then
Dim index As FullTextIndex = New FullTextIndex(table)
Dim column1 As FullTextIndexColumn = _
New FullTextIndexColumn(index, "CompanyName")
index.IndexedColumns.Add(column1)
index.ChangeTracking = ChangeTracking.Automatic
index.UniqueIndexName = "PK_Company_1"
index.CatalogName = catalogName
index.Create()
index.StartPopulation(IndexPopulationAction.Incremental)
End If
Dim connectionString As String = _
"Data Source=.\SQLExpress;Initial Catalog=StockHistory;" + _
"Integrated Security=True;Pooling=False"
Using connection As SqlConnection = _
New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = New SqlCommand( _
"SELECT * FROM COMPANY WHERE FREETEXT(*, 'MICROSOFT')", _
connection)
Dim reader As SqlDataReader = command.ExecuteReader
While (reader.Read())
Console.WriteLine("{0}, {1}, {2}", reader("CompanyID"), _
reader("CompanyName"), reader("CompanySymbol"))
End While
Console.ReadLine()
End Using
End Sub
End Module
In the second half of the example, a connectionString is declared. The connectionString is used to initialize a SqlConnection object in a Using statement. The Using statement ensures the SqlConnection is closed and disposed of at the end of the using block. The connection is opened. A SqlCommand object is created. The SqlCommand object contains the SQL text and the connection object. Notice that the WHERE clause contains the FREETEXT predicate.
The FREETEXT predicate supports specifying the columns to search. The asterisk (*) means searching all indexed columns. The text—in the listing, 'MICROSOFT'—indicates the text to look for. Finally, a SqlDataReader is used to read and display the resultset.
As the example, you could have solved the same problem with the following query:
WHERE CompanyName LIKE 'MICROSOFT%'
Clearly, this works. Where LIKE begins to fail is when the text can appear anywhere, when there are multiple columns to check search, and those columns are dependent on specific columns to search as expressed by the user. For example, if you have ten possible fields to search, building the WHERE clause becomes very clumsy; by using FREETEXT and full text Indexing, the WHERE clause changes very little regardless of the fields to search.
Summary
SQL Management Objects (SMO) is the ability to manage SQL programmatically. This capability was added to SQL Server 2005 and the .NET Framework 2.0. In the example, you see how Full text Indexing can be enabled, indexes configured, and the index populated and used—all in the same chunk of code.
There is a lot more to SMO then shown here, but this article should get you started. If nothing else, perhaps you can save time by simply adding a single point of entry search to your applications and letting the Indexing Service and SQL Server figure out how to construct the actual search against the indexes.
Subscribe to:
Comments (Atom)
