Build Your Own Search Engine
Lesson 6: Building a Search Engine
This lesson focuses on building a search engine with DataFlex. What is a search engine? A search engine basically combines a number of result sets and presents them as a single, searchable result set. This is advantageous because it allows the developer to set the criteria for searches that comb through all of an applications data whether it is user types, customers, and invoices, or album names, tracks, and artist like the example used in this course.
The steps to creating a search engine:
- Determine the tables to search, this can be determined ahead of time or added incrementally as needed
- Build a database view for searching, use SQL Server Management Studio to build and test
- Create a cWebView object with List in your application
- Provide Drilldowns to cWebViews, a drill down should be assigned for each type of information that is available
In this lesson it is assumed that step one, determining the tables to search, has already been completed. Moving on to the second step, a view needs to be created that is going to concatenate multiple result sets. For each area that is going to be searched, a result set needs to be created and they need to be concatenated.
Important: The first result set that is created determines the names of the columns. Each of the result sets must have the same number of columns. Each of the defined columns must have the same data type in every result set. The following example will continue using the Chinook music library database that was used in Lesson 1. Several steps have been prepared in preparation for this lesson. Start by building a view that is going to concatenate several result sets. In this example, the search engine view created will have three columns: the source, the id of the row that is being shown and the description of the row that is being shown.
- In “SQL Server Management Studio” create a view that displays the source, ID, and description. For this example, the source will be [Track], the ID is [TrackID], and the description is [Track].[Name].
- Again, this first result set determines the column names.
- Use UNION to concatenate all desired result sets.
- Note that in the example:
- A conversion to nvarchar (255) was made for the ID, so that this view can produce an integer’s ID, text IDs and others. 255 is the maximum length.
- Fields can also be concatenated. In this example, FirstName was concatenated with LastName.
- The view is brought into DataFlex by using the method DATABASE >>>CONNECT TO DATABASE TABLES found in the top toolbar.
- Right click on the desired table from the Table Explorer on the left (vwDataFlexFind in this example) and select VIEW TABLE to view the results.
- As was shown in Lesson 1, a select has been created for this example called “TestSelect.wo.” The only difference with TestSelect.wo is that when viewed there is only one column with the first row showing the description, and the second row showing the source.
- Regarding the constraint in this this example…
- The value is entered into a form, and then it is trimmed.
- Important: an extra line has been added, SQLEscapedStr, to remove any possible SQL characters that the user may mistakenly enter into the search field. This sanitizes the input, so that the characters entered cannot be mistaken as part of an SQL statement.
- Following that, it shows that if a value is greater than “”, the SplitString function is called.
- This causes a search with multiple keywords to be split into an array or strings. For example, a search of “Queen Hits” will be split into “queen” and “hits.”
- To connect a zoom to an action such as an ‘on click’…
- In the OnRowClick procedure, determine what is the DataSource of the current row that has been clicked on and what is the zoom. The zoom also needs to be activated.
- A NavigateForward command needs to be added to activate the zoom. The zoom also needs to know what record is to be shown. To get the record, the activated zoom is going to call back with OnGetNavigateForwardData. The ID of the table it needs to show needs to be defined as well as the record identifier/ID that needs to be shown.
- In this example, the zoom being connecting is for tracks. The data source is track, and the zoom is oTrackZoom.
- Register the zoom at the top of the OnRowClick section, Register_Object oTrackZoom
- Declare the data file at the top of the code, Declare_Datafile vwTrack
- To test, run it by clicking the COMPILE icon on the top toolbar, select the SEARCH tile, and then write “Queen Bohemian” into the search bar.
What was just shown is that a complete search engine can be created in DataFlex by simply building a view. The view combined multiple data sources together, and then brought it together in a select window in the DataFlex Framework.
Points to remember:
- SQL can really speed up search functions
- Sanitize SQL input to protect against SQL injection
- Use views like tables; really powerful for finding data