setting up a text search engine

 

there is no single way of setting this up but the following sample should enable you to get on the right track.

let's assume you have a book database and want to enable the user to search for books by author and/or title. we further want to present e.g. the columns author, first_name, titel, description and price from the table books on the result page.

if a search field is empty we want to retrieve all records for this category and furthermore we want to retrieve all records containing a search term, not just exact matches (e.g. "brod" should return "Max Brod" as well as "Harold Brodkey").

finally the records shall be ordered by author, then by first_name and then by titel.

step 1) setting up the search form

this could be a simple HTML page. insert a form and place two text fields and a submit button in it. call the textfields e.g. s_author and s_title (it could be any name).

as form action enter the URL of your result page and chose GET for method.

(theoretically you could use POST as well, but when you want to use "Go to Previous Record" and "Go to Next Record" links on the result page the form variables will be passed on as URL parameters by default so you might just as well use GET straight away. this has the added advantage that the search results can be bookmarked).

step 2) creating the result page

since there is more than one filter value the recordset cannot be set up in Ultradev's simple mode. open advanced mode, select the connection you want and then enter the following SQL statement:

SELECT author, first_name, title, price
FROM books
WHERE author Like '%r_author%' AND title Like '%r_title%'
ORDER BY author ASC, first_name ASC, title ASC

you could simply type this in, use the insertion tools at the bottom of the dialog box or start off with a simple mode query and refine it in advanced mode.

important: make sure that you only use single quotes as double quotes will interfere with othe double quotes in the ASP code!

r_author and r_title are variables which are going to be taken over from the search form.

the % characters act as wildcard. placing them in front and at the end of the variable will assure that e.g. the term "brod" will also retrieve "Brodkey".

the next step is to retrieve the variables from the search form and to define a default value in case the form field was left empty.

to do this click on the + in the variable field and enter r_author, then set the default value to % (as before: this acts as wildcard and will retrieve every field that has any value in this column) and define the runtime value as Request.Querystring("s_author") or just Request("s_author") ( the first one retrieves URL parameters only, with the latter one s_author could be either sent using GET or POST)

repeat this for r_title.

the final result should look like this:

after that you can close the recordset window and start creating the layout of the result page.

NOTE: if there is a chance that a search could result in zero records you should always add a "Repeat Region" behavior to the display of the results, even if you want to have only one repeat or you apply a "Hide Region (if empty)" behavior.

If you don't do this the script might try to display a value that does not exist and produce an error message "Either EOF is true or BOF...". the behaviors check if a value is available and display simply nothing if this is not the case.

 

 

 


 

1