Set the search predicate from a string.


Namespace: NetricsServerInterface
Assembly: NetricsServerInterface (in NetricsServerInterface.dll)

Syntax

Visual Basic (Declaration)
Public Sub SetSearchPredicate( _ 
   ByVal expr As String _ 
)
C#
public void SetSearchPredicate(
   string expr
)
C++
public:
 void SetSearchPredicate(
   String expr
) sealed 
J#
public void SetSearchPredicate(
   string expr
)
JScript
public  function SetSearchPredicate(
   expr : String
)

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,0x8FFFInteger constant values. They follow the standard "C" and C# conventions for decimal, Octal and Hex integers.
123.45, 0.17e-10Floating 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.
#2Table 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 1", :"block 2" ]
[ :]
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.

Predicate expressions are defined in a typical manner. E.g.

 Copy Code
                     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:
 Copy Code
                     ((INT $"age" + INT $"years experience") / 2) - 1 >= 21
                     

Operators are left associative, that is:
 Copy Code
                     2 + 3 + 12
                     

is equivalent to:
 Copy Code
                     (2 + 3) + 12
                     


Operators have precedence similar to those in "C" C# and C++. So:
 Copy Code
                     12 + 2 * 3
                     

is the same as:
 Copy Code
                     12 + (2 * 3)
                     

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.
  • + has slightly higher precedence than - (minus), thus
     Copy Code
    a - b + c
    is the same as
     Copy Code
    a - (b + c)
    not
     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:
operatorsdescription
**raise to power
*multiple
/divide
+addition for numbers, concatenation for strings
-subtraction
=, ==, ~=, >, ~>, >=, ~>=, <, ~<, <=, ~<= SUBSET, SUPERSET, INComparison operators, "=" and "==" are synonyms. Comparison operators prefixed with ~ perform letter case insensitive comparisons.
ANDLogical and joining operator, similar to the C# && operator
ORLogical or joining operator, similar to the C# || operator

The unary operators are:
operatordescription
+arithmetic positive, this does nothing
-arithmetic negation
NOTlogical inverse
UPPERconvert character string/byte block to upper case
LOWERconvert character string/byte block to lower case
INTconvert value to integer
DBL DOUBLE FLOATconvert value to floating point. These are all synonyms for the same operation.
DATEconvert value to a date.
DATEEU EUDATEconvert value to a date. String and byte block format dates are assumed to be in European format. These are synonyms for the same operation
BLOCKconvert value to a byte block.
SPLIT TOKENIZEsplit a string or block into a block array with each element of the array being a word from the string.
ABSreturn 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:

 Copy Code
                     NetricsSearchCfg tblcfg = new NetricsSearchCfg("tbl");
tblcfg.SetSearchPredicate("$\"Company\" == \"Pepsi\" or $\"Company\" == \"Coke\"");

Example

This sample shows how to perform a search of a TIBCO Patterns Engine table using a predicate to filter the result set to only include those records from the state of CA.

 Copy Code
                     // 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 TIBCO Patterns Engine 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");
                            }
                        }
                    }
                     

See Also