You are here: Overviews > Limiting searches with SearchFilters > Using search filters to combine a full-text search with a geographic search
dtSearch Text Retrieval Engine Programmer's Reference
Using search filters to combine a full-text search with a geographic search

Describes how to use search filters to implement an application that executes a full-text search against a subset of a database, limiting the results to items within a specified geographic range.

Requirement: Search a collection of documents, each coded with a ZIP code, to find documents satisfying a full-text search criterion within a defined geographic area. 


A first attempt at this project usually combines the full-text search with a field search listing the ZIP codes that satisfy the geographic criterion:

(apple and pear) and ZipCode contains( 00001 or 00002 or 00003...)

Using xfilter expressions to improve performance produces a variation on this:

(apple and pear) and xfilter(word "ZipCode::00001") or xfilter(word "ZipCode::00002")...

Performance will be acceptable for this type of searching if only a few zip codes are involved, but if the number of codes reaches hundreds or thousands, searching will get slower or searching will fail when the maximum search request size (32k) is reached. 

To implement geographic searching using SearchFilters, the application will need a quick way to build a SearchFilter from a list of zip codes. To do this, the application should collect the mapping from zip codes to DocIds when the indexes are created. A simple way to do this would be to keep a file for each ZIP code with the SearchFilter for that ZIP code, and update the files each time the index is created or updated. (I.e., when a document from ZIP code 12345 is indexed, read the SearchFilter named 12345, set the DocId for the newly-added document, and save the filter.) 

Once these SearchFilter files are created, to search the application would proceed as follows: 

1. Identify the ZIP codes matching the geographic criteria 

2. Read the SearchFilter files for each matching ZIP code into a single SearchFilter (SearchFilter.ReadMultiple can be used for this.) 

3. Create a SearchJob, call SearchJob.AddIndexToSearch() with the path to the index, and call SearchJob.SetFilter to limit the search to the items in the filter 

4. Set up the other properties of the SearchJob with the search request, any search features required, etc. 

5. Execute the search

Copyright (c) 1995-2023 dtSearch Corp. All rights reserved.