ibi Patterns - Search 6.0.0 .NET API |
NetricsSearchCfg.SetSearchPredicate Method (String) |
See AlsoExample |
![]() ![]() |
Set the search predicate from a string.
Namespace: NetricsServerInterface
Assembly: NetricsServerInterface (in NetricsServerInterface.dll)
Syntax
Remarks
In addition to building the search predicate piece by piece the entire predicate can be defined as a string using a SQL like syntax. The basic items in the syntax are:
?TRUE?, ?FALSE? | Boolean constant values are true and false enclosed in question marks. Letter case insensitive |
123,0777,0x8FFF | Integer constant values. They follow the standard "C" and C# conventions for decimal, Octal and Hex integers. |
123.45, 0.17e-10 | Floating point values. They follow the standard "C" and C# conventions for fixed and scientific notation values. |
"string" | string constants are enclosed in double quotes. The basic XML/HMTL encoding scheme is used to represent the double quote character (") itself and other special characters. The numeric conventions: &#ddd;, &#xhh; are recognized and the entity names: quot, amp, lt, gt and apos are recognized. Note that you must NOT insert an encoded NULL character into the string as this is a NULL terminated string value. |
:"byte block" | A byte block is specified by preceding a quoted string value with a colon (:) character. The length of the block is computed automatically. All non-valid string characters must be encoded using the XML/HTML like encodings as defined for strings. |
#2 | Table fields specified by numeric position. An integer value preceded with the pound (#) character is used to represent the table field at the indicated column position. Like array indexes in "C" and C# these field numbers are zero based. |
$"first name" | Table fields specified by field name. A quoted string value (see string description above) preceded by the dollar ($) sign. The string value is the field name. |
[ :] | Block arrays. A comma separated list of blocks enclosed in square brackets ([]). To specify a Block array with zero entries use an open bracket followed by colon close bracket (:]) no space between the colon and close bracket. |
Argument Lists. A comma separated list of expressions enclosed in curly braces ({}) is a special notation used to create argument lists for predicate functions. |
Predicate expressions are defined in a typical manner. E.g.
![]() | |
---|---|
INT $"age" > 21 |
specifies all records where the "age" field is greater than 21 (if the field "age" was of type int or float the INT conversion operator would not be necessary). Parenthesis can be used to group operations into more complex expressions:
![]() | |
---|---|
((INT $"age" + INT $"years experience") / 2) - 1 >= 21 |
Operators are left associative, that is:
![]() | |
---|---|
2 + 3 + 12 |
is equivalent to:
![]() | |
---|---|
(2 + 3) + 12 |
Operators have precedence similar to those in "C" C# and C++. So:
![]() | |
---|---|
12 + 2 * 3 |
is the same as:
![]() | |
---|---|
12 + (2 * 3) |
Special unary operators called predicate functions take an argument list as their single argument:
![]() | |
---|---|
geod { DBL $"lat", DBL $"long", 45.0, 75.0, "miles" } < 25.0 |
Some key differences between the precedence of predicate operators and the "C" language family of operators are:
- All unary operators have higher precedence (bind tighter) than binary operators, this includes all predicate function operators.
- + has slightly higher precedence than - (minus), thus is the same as
Copy Code
a - b + c
notCopy Code
a - (b + c)
Copy Code
(a - b) + c
- similarly multiply (*) has slightly higher precedence than divide (/)
- All comparison operators have the same precedence.
The binary operators listed from highest precedence to lowest are:
operators | description |
** | raise to power |
* | multiple |
/ | divide |
+ | addition for numbers, concatenation for strings |
- | subtraction |
=, ==, ~=, >, ~>, >=, ~>=, <, ~<, <=, ~<= SUBSET, SUPERSET, IN, I_IN | Comparison operators, "=" and "==" are synonyms. Comparison operators prefixed with ~ ("I_" in the case of the IN operator) perform letter case insensitive comparisons. |
AND | Logical and joining operator, similar to the C# && operator |
OR | Logical or joining operator, similar to the C# || operator |
The unary operators are:
operator | description |
+ | arithmetic positive, this does nothing |
- | arithmetic negation |
NOT | logical inverse |
UPPER | convert character string/byte block to upper case |
LOWER | convert character string/byte block to lower case |
INT | convert value to integer |
DBL DOUBLE FLOAT | convert value to floating point. These are all synonyms for the same operation. |
DATE | convert value to a date. |
DATEEU EUDATE | convert value to a date. String and byte block format dates are assumed to be in European format. These are synonyms for the same operation |
BLOCK | convert value to a byte block. |
SPLIT TOKENIZE | split a string or block into a block array with each element of the array being a word from the string. |
ABS | return the absolute value of a number. |
The names of all operators are letter case insensitive. For more information on these operations see NetricsPredicate.
The final example from NetricsPredicate as a string predicate would be:
![]() | |
---|---|
NetricsSearchCfg tblcfg = new NetricsSearchCfg("tbl"); |
Example
This sample shows how to perform a search of an ibi™ Patterns - Search table using a predicate to filter the result set to only include those records from the state of CA.
![]() | |
---|---|
// This is an example of a multi-query where different query strings can be used in a search across // different fields within a table. This requires first configuring a Simple query for each query // string along with the fields to be searched and then either "And" or "Or" them together. In this // we will query the "names" table using a query string against the "last" field and a another query // string against the "first" field. Each of these queries are referred to as a querylet. We will // apply a higher weight to the "last" field querylet and apply a the "nicknames" thesuaurs to the // "first" field querylet treating the related terms within the thesaurus as equivalent. We are also // using a predicate for this query which is set to limit the results to only those records that have // the "state" field equal to "CA". using System; using NetricsServerInterface; class MyClass { private static NetricsServerInterface.NetricsServerInterface si = null; public static void Main() { try { String host = "localhost"; int port = 5051; si = new NetricsServerInterface.NetricsServerInterface(host, port); // Table is the ibi™ Patterns - Search table to be search/matched against query String table = "names"; // Going to use 2 NetricsQuery for the search configuration NetricsQuery[] nqs = new NetricsQuery[2]; // Create NetricsSearchOpts object using the defaults NetricsSearchOpts opts = new NetricsSearchOpts(); // Set up the query to use the nicknames thesaurus. i.e. "Den" is equivalent to "Dennis" opts.useThesaurus("nicknames"); // Create the NetricsSearchCfg pointing to the table to be searched/matched against NetricsSearchCfg tblCfg = new NetricsSearchCfg(table); // Query will contain the query string for the first querylet which is "last String query = "brown"; // FieldNames contain the fields within the table to be searhed/matched against. // If fieldNames is set to null then all fields will be searched/matched against String[] fieldNames = { "last" }; // Create a Simple query for the "last" names search nqs[0] = NetricsQuery.Simple(query, fieldNames, null); // Set up the second Simple query query = "den"; fieldNames[0] = "first"; nqs[1] = NetricsQuery.Simple(query, fieldNames, null); // QueryletWeights is an array that must be the same length as the number of querylets // and is the weight applied to each querylet in the search which impacts the similarity // scrore. In this case there will be two querylets that will be ANDed together to create // a single search configuration double[] queryletWeights = { 1.0, 1.0 }; // Now AND the two Simple querys together to create the final NetricsQuery for the search NetricsQuery nqAnded = NetricsQuery.And(queryletWeights, nqs); // Set the NetricsSearchCfg; tblCfg.setNetricsQuery(nqAnded); // Limit the result set to include only records whose "state" field equals "CA" tblCfg.SetSearchPredicate("$\"state\" == \"CA\""); // Perform the search/match using the NetricsSearchCfg and NetricsSearchOpts NetricsSearchResponse resp = si.search(tblCfg, opts); String s = ""; // Get the results of the search/match NetricsSearchResult[] res = resp.getSearchResults(); // Create viables for result processing String[] xfieldNames; String[] xfields; // Iterate through the result records for (int i = 0; i < res.Length; i++) { // Get the fieldNames in the result record xfieldNames = res[i].getFieldNames(); // Get the data for each fieldName in result record xfields = res[i].getFields(); // Get the score for this result record double score = res[i].getMatchScore(); // Build the printline for the result record s = s + "Rank=" + i + ", Score=" + score.ToString(); for (int x = 0; x < xfields.Length; x++) { s = s + ", " + xfieldNames[x] + "=" + xfields[x]; } s = s + "\n"; } Console.WriteLine(s); } catch (NetricsException e) { Console.Write(e.getErrorDescription() + "\n"); Console.Write(e.ToString() + "\n"); } } } |