"Filtering" Search Results
Search results can be restricted to a subset of
records. For instance, if we have two tables:
The PartName and PartDescription fields have been indexed by Rubicon. How can the search results be limited to those parts which are manufactured in Canada? Before answering this, let's define filtering as it applies to this page. It means restricting the search results to a subset of a table. The techniques described here are much more efficient than the TTable.Filtered property (or the equivalent Filtered property supported by third party datasets). For more information about dataset filters and ranges, see the "Filters and Ranges" section of the documentation. In order for Rubicon to filter the results, each record in the text table must have a unique key word associated with it. In this case, the key words are going to be 'Canada', 'Germany', 'France', 'Mexico', 'USA', etc. We will assume that these words do not appear in the PartName or PartDescription fields. There are three ways to associate these words with the records:
Add PartCountry Field While this approach is not very elegant, it does illustrate the concept. A new string field is added to the Part table, PartMfgCountry, and the field is populated with appropriate MfgCountry value. PartMfgCountry is included for indexing and the Words table is rebuilt. To implement the filter, the search code becomes: with rbSearch1 do Calculated or Lookup Fields Instead of changing the table, a more efficient approach would be to add a calculated or lookup field named PartMfgCountry that returned the value of MfgCountry from the Manufacturer table. When using a TTable-like dataset, the simplest way to do this is to use the Fields Editor in the Delphi IDE to define the PartMfgCountry field and include this field for indexing. When using a TQuery-like dataset the PartMfgCountry field cannot be added via the Fields Editor because Rubicon is going to use the TQuery to execute different SQL statements, some of which will not be selecting the fields necessary to perform the lookup. In this case, using the OnProcessField event may be the appropriate alternative. For advanced users, a work around for this problem would be to create a TQuery.AfterOpen event that checks the SQL to see whether the necessary fields are being selected, and, if so, creates the lookup or calculated fields at run time. Using OnProcessField This approach uses the OnProcessField event to perform the function of a lookup or calculated field. First, add the PartMfgNo field to the list of fields being indexed by Rubicon. Then create the following OnProcessField event for the TextLink. procedure TForm1.CustomProcessField(Sender: TObject; As each record is processed, this event is called for each field. If the field name is not PartMfgNo, then the text in that field is simply processed. If the field name is PartMfgNo, then the field value is used to perform the lookup in the Manufacturer table to find and process the country value. When using OnProcessField event with TrbMake, if you are also using TrbUpdate, then use must use CustomeProcessField with TrbUpdate.TextLink. In this example it is not necessary to use the CustomProcessField event with TrbSearch.TextLink. Uniqueness All of the above approaches rely on the filter word being unique. But what if the word 'Canada' appears in the PartDescription field and what if this part is not manufactured in Canada? This would lead to invalid results. To avoid this, it is often necessary to ensure the uniqueness of the filter words by adding one or more characters to the word (e.g. 'Canada' becomes 'xxCanada'). When altering the filter words, be sure to respect the definition of a word as defined my MinWordLen, WordDelims, and TrbAccept (if used). Filters in Action The online newsgroup search application uses filters
to restrict search results to certain newsgroups. For each newsgroup
message, the name of the newsgroup is converted from When a user want to restrict the search results to newsgroups whose name contains the word 'database', they precede the word with a caret like this: ttable locate ^database The newsgroup search application simply replaces the caret with 'ng_' and performs the search. The 'ng_' prefix is used because that makes all the filter words unique to the database.
|
Copyright 2003 © Tamarack Associates |
||
www.TamarackA.com | Last updated 11/13/00 | www.FullTextSearch.com |