Predicate Queries
There is a second use of predicate expressions besides filtering predicates, which was briefly explained in Designing Queries for TIBCO Patterns. Sometimes, you might want to use a predicate expression as a query type alongside simple, cognate, and date comparisons within a complex query structure. Predicates used in this way are called predicate queries.
In general, predicate queries are useful when one or more of the following conditions are true:
| • | You do not need for inexact matching on a given field. |
| • | You do not need for the logic operations available with predicate expressions. |
| • | You must perform comparisons on numeric fields based on numeric rather than text values. |
| • | You need to perform date range tests on a date field. |
Not all fields of tables require or benefit from inexact matching. Coded fields such as State (of the U.S.), Gender, Marital Status, or Product Type typically have content limited to a set of fixed values. When these values are relevant in a search, they are often good candidates for predicate queries. Using exact-matching predicates on such fields also saves memory and improves performance.
Comparison of numeric and date values often involves special logic different from fuzzy string matching, for instance, matching based on numeric or date range. The integer, float, and date field types of Patterns in-memory tables are provided primarily for predicate support – they are more efficient in predicate expressions and use less memory than the equivalent values as text strings.
In predicate queries, the predicate expression need not be constructed in such a way that it evaluates either true or false; it is required only that it evaluate to a value that can be converted to a double value in the usual score interval of 0.0 to 1.0 or the special reject score of -1.0. This includes Boolean values, which map to 1.0 or 0.0 for true and false respectively, and the integers 0 and 1. This “predicate score” can then be combined with other scores in the complex query. For example, you can use a predicate expression to compute a score based on the geographical distance between two locations, normalized onto the interval [0.0, 1.0].
The following expression is a predicate query where records more than 25 miles away are assigned a zero score:
toscore{ geod{ 40.0, 75.0, $"lat", $"long", "miles" }, 25.0, 0.0 }
The following is an example that shows how the conditional function can be used to assign a score to coded field values with eye color:
if { $"eyes" ~= "blue", 0.1, $"eyes" ~= "hazel", 0.5, $"eyes" ~= "brown", 1.0, 0.1 }
Another use for the conditional function is to create tiered scores:
if { $"age" >= 50, 0.25, $"age" >= 35, 0.5, $"age" >= 25, 1.0, $"age" >= 21, 0.75, 0.0 }
Here, a score is assigned based on an age bracket, with anyone under 21 getting a score of 0.0.