Scripting for searching
A search engine is a necessity on any large Web site or an intranet. Most search engines, though, are not quite ready for prime-time right out of the box. In this article, I am going to show you two methods for creating search scripts for Index Server, Microsoft's search engine for Internet Information Server (IIS).
- Internet Information Server (IIS) 3.0 or 4.0
- Index Server 1.x or 2.0
- Window NT Workstation or Server 4.0
- Your favorite text editor
There are some caveats with these tools. First, if you install IIS 4.0 and Index Server 2.0, you will have more flexibility, because you can use both scripting methods that are presented in this article. However, you will need to have NT Server, because Index Server 2.0 will only work with IIS 4.0, which will only run on NT Server. Second, if you have or can get IIS 3.0 and Index Server 1.x, you can run them on NT Workstation, but you will only be able to use the old scripting method.
Old scriptsThe first scripting method I am going to show you is what I call the old method. I call it that because Index Server 2.0 introduces a new, more flexible method. To use the old method, you have to create three files: 1) a Web page (.HTM or .HTML), which is the starting point; 2) a query template file (.HTX), which is used to display search results; and 3) a query parameters file (.IDQ), which is used to configure the query. The Web page can be put in any Web directory, but the query parameters file and the template file have to be put in a Web directory with execute permission. A common place is "/scripts".
The Web page can be as simple or as fancy as you want, but at minimum it needs to contain a form, a field for the search text and a button to submit the form. The action of the form should be set to the URL of the query parameters file, e.g.,
. My sample Web page is SimpleSearch.htm.
action = "/scripts/SimpleSearch.idq"
The query template file is used to display the search results. It contains HTML and special tags for variable substitution and basic control statements, e.g., <
> and <
%if CiMatchedRecordCount eq 0%
>. Note that the <
> and <
> tags mark the block that will be processed once for each result record returned by the search. My sample query template file is SimpleSearch.htx. The source version is here, and output from a sample search is here.
The query parameters file is much like an INI file and is used to specify the information that Index Server needs to execute the query. These parameters include: the columns that should be returned, the scope of the query, the template file to be used, the sort order and so on. My sample query parameters file is SimpleSearch.idq.
Values for any of the query parameters can be passed in from any Web page. This is done simply by creating a field in the Web page that calls the query parameters file; then, in the query parameters file, setting the parameter equal to
is the name you chose for the field. For example, in SimpleSearch.htm, I created a field called "target", like this:
<INPUT TYPE="text" NAME="target" SIZE="45" MAXLENGTH="100">
Then, because "target" gets passed into SimpleSearch.idq, I used it to set the value of the CiRestriction parameter, like this:
CiRestriction=(! #vpath *\_vti*) and (%target%)
Additionally, all of the query parameters and all of the fields passed in from the form are passed into the query template file when it is processed by Index Server. You can see an example of this by tracing "target" from SimpleSearch.htm to
in SimpleSearch.idq to <
> in SimpleSearch.htx.
For more information on the query template file, the query parameters file or the Index Server query language, consult the Index Server documentation.
New objectsThe second scripting method I'm going to show you is what I call the new method. I call it that because it was introduced with IIS 4.0 and Index Server 2.0. With the new method, you can use an Active Server Pages file (.ASP) to execute the search and display the results, instead of having to use three separate files (.HTM, .IDQ and .HTX). Also, you can use the new Index Server Objects to interact with Index Server in the same manner you would interact with a database server via Active Data Objects (ADO). My sample ASP file is SimpleSearch.asp. The source version is here, the display version is here and the output from a search is here.
SimpleSearch.asp is like a combination of the three files used in the old method, with VBScript added to the mix, which provides much more flexibility. With VBScript, I was able to add VCR-style controls to the bottom of the page, instead of the simple "next" and "previous" buttons I used with the old method. (Note that JScript can also be used, but I stuck with VBScript because it's what Microsoft uses in its examples.)
When you look at SimpleSearch.asp, you will see that most of the work is done with
creates a Query object, sets the query parameters by setting properties of the Query object, executes the search by telling the Query object to create an ADO Recordset, then calls
, which displays the search results and the navigation controls.
For more information on Index Server Objects or Active Data Objects, consult the Windows NT Option Pack documentation.
ImprovementsBecause the sample files I provided with this article were written as examples rather than production code, there is room for improvement. In SimpleSearch.idq, all of the query parameters could be set with fields from the calling Web page, so that one IDQ file could be used by multiple search pages. Doing this would be very straightforward. In SimpleSearch.asp, the ASP Session object could be used to cache the query results, so the query does not have to be executed again if the user is just paging through the results. Doing this, however, would not be particularly straightforward. I leave both of these improvements as an exercise for the reader.
Thornton Rose is currently a software developer in Atlanta, GA. He can be reached via e-mail at firstname.lastname@example.org. Hoch nuH qel ghunwI' po'.