Fuzzy database search functions

When you set up a database lookup, you can search for different numbers of words. To do this, you need to create one field for one word.

For example, if you want to search for John Smith in Canal Street, create four fields using either Dim Fields() As String or ReDim Fields(4).

Now that you have set up your fields, you can then assign each word to a field:

Fields(0) = ”John”
Fields(1) = ”Smith”
Fields(2) = ”Canal”
Fields(3) = ”Street”

Or

Fields = Split(“John Smith Canal Street”, " ")

FieldIDs can be used to define which column is searched in the database. If the FieldID is set to 0 then all columns in the database are searched.

Note Indexes are applied only to the subset of columns that were included when the database was imported. If you selected only 5 of 8 possible columns, you can index only one to five fields.

The following example shows the code needed to search in three columns:

Dim Fields() As String
Dim FieldIDs() As Long
ReDim Fields(2)
ReDim FieldIDs(2)
Dim DB As CscDatabase
Dim DBResItems As CscDatabaseResItems
Dim arr() As String
   
Fields(0) = pXDoc.Fields(0).Text
Fields(1) = pXDoc.Fields(1).Text
Fields(2) = pXDoc.Fields(2).Text

FieldIDs(0) = 1
FieldIDs(1) = 0
FieldIDs(2) = 0

Set DB = Project.Databases.ItemByName("German")
Set DBResItems = DB.Search(Fields, FieldIDs, CscEvalMatchQuery, 5)

If DBResItems.Count > 0 Then
    arr = DB.GetRecordData64(DBResItems(0).RecID64)
    If MsgBox("Confidence: " + CStr(DBResItems(0).Score) + " Zip: " + arr(0) + " & City: " + arr(1) + _ 
        "Street: " + arr(2) + vbCrLf + "Insert values?", vbYesNo, "Database lookup") = vbYes Then
            pXDoc.Fields(0).Text = arr(0)
            pXDoc.Fields(1).Text = arr(1)
            pXDoc.Fields(2).Text = arr(2)
    End If
End If
Note When you import a fuzzy database into your project, you can define a few preprocessing settings such as Additional Delimiter Characters, Ignore Characters, and Substitution pairs. When you use this database in a Database Locator, the preprocessing functions are applied to the document before searching its content in the database.

When you use this database directly in script, the preprocessing is not applied to the document beforehand. As a result, you can use the following examples in place of the preprocessing.

Code for additional delimiters:

' Additional Delimiter chars are used to split the text into smaller fields. You need to split your search string
' by using the same characters:
Dim i As Long
Dim s as String
   
s = QueryString
' Replace all delimiters by blank   
For i = 0 To Len(DB.AdditionalDelimiterChars) - 1
    s = Replace(s, Mid(DB.AdditionalDelimiterChars, i + 1, 1), " ")
Next

Fields = Split(s, " ")

Code for ignore characters:

' Characters that are ignored during DB import need to be removed from the query
Dim i As Long
Dim s as String
 
s = QueryString
' Replace all ignore characters by blank   
For i = 0 To Len(DB.RemoveChars) - 1
    s = Replace(s, Mid(DB.RemoveChars, i + 1, 1), " ")

Code for substitution pairs:

' Substitution pairs define which texts to be replaced by what. Apply that to your query string
Dim i As Long
Dim s as String
   
s = QueryString

For i = 0 To DB.SubstitutionPairCount - 1
    Dim value As String
    Dim substitute As String
    DB.GetSubstitutionPair(i, value, substitute)
    s = Replace(s, value, substitute)
Next