Search Filter Generator Examples

This section provides additional code examples for generating search filters using the SearchFilterGenerator class:

  • Creating complex search filters.

  • Boosting the score of filter matches.

Note: The & is the default connector; therefore, for composite filters, MatchAll is implied and does not have to be explicitly specified.

Example 1 — Using an Explicit Or

The following example generates a composite search filter with Field1 equal to string X or Field2 containing string Y:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();
&fg.MatchAny();
&fg.EqualsString("Field1", &str_X);
&fg.ContainsWord("Field2", &str_Y);
&fg.EndMatchAny();
&fg.SetQueryFilter(&qry);

See MatchAny for more information.

Example 2 — Using an Implicit And

The following example generates a composite search filter with Field1 equal to string X and Field2 containing string Y:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();
/* The & is the default connector; therefore, MatchAll is implied and does not */
/* have to be explicitly specified. */
&fg.EqualsString("Field1", &str_X);
&fg.ContainsWord("Field2", &str_Y);
&fg.SetQueryFilter(&qry);

See MatchAll for more information.

Example 3 — Specifying a Complex, Composite Filter

To generate the following composite search filter ((Field1 = string X & Field2 > number N1) & ((Field3 >= number N2 & Field3 <= number N3) | (Field4 = string A | Field4 = string B | Field4 = string C)))

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();
If &fg.MatchAllReturnTrue() Then
   &fg.EqualsString("Field1", &str_X);
   &fg.GreaterThanNumber("Field2", &N1);
   &fg.EndMatchAll();
End-If;

If &fg.MatchAnyReturnTrue() Then
   
   If &fg.MatchAllReturnTrue() Then
      &fg.GreaterThanEqualsNumber("Field3", &N2);
      &fg.LessThanEqualsNumber("Field3", &N3);
      &fg.EndMatchAll();
   End-If;
   
   If &fg.MatchAnyReturnTrue() Then
      &fg.EqualsString("Field4", &str_A);
      &fg.EqualsString("Field4", &str_B);
      &fg.EqualsString("Field4", &str_C);
      &fg.EndMatchAny();
   End-If;
   
   &fg.EndMatchAny();
End-If;

&fg.SetQueryFilter(&qry);

Example 4 — Specifying a Numeric Filter Between Two Values

The following example generates a composite search filter with BU equal to 001 and price between 10 and 200:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

&fg.EqualsString("BU", "001");
If &fg.MatchAllReturnTrue() Then
   &fg.GreaterThanEqualsNumber("PRICE", 10);
   &fg.LessThanEqualsNumber("PRICE", 200);
   &fg.EndMatchAll();
End-If;
&fg.SetQueryFilter(&qry);

See GreaterThanEqualsNumber and LessThanEqualsNumber for more information.

Example 5 — Specifying a String Filter Within a Set of Values

The following example generates a composite search filter with BU equal to 001 and status in (Open, Pending, Approved):

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

&fg.EqualsString("BU", "001");
If &fg.MatchAnyReturnTrue() Then
   &fg.EqualsString("STATUS", "Open");
   &fg.EqualsString("STATUS", "Pending");
   &fg.EqualsString("STATUS", "Approved");
   &fg.EndMatchAny();
End-If;

&fg.SetQueryFilter(&qry);

See EqualsString for more information.

Example 6 — Specifying a String Filter Not in a Set of Values

The following example generates a composite search filter with BU equal to 001 and status not in (Open, Pending, Approved):

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

&fg.EqualsString("BU", "001");
If &fg.MatchAllReturnTrue() Then
   &fg.NotEqualsString("STATUS", "Open");
   &fg.NotEqualsString("STATUS", "Pending");
   &fg.NotEqualsString("STATUS", "Approved");
   &fg.EndMatchAll();
End-If;

&fg.SetQueryFilter(&qry);

See NotEqualsString for more information.

Note: When two or more score boosts have been applied to individual search filters, then specify the boost mode. Otherwise, the default boost mode is MIN, which is equivalent to executing the setBoostModeMinimum method.

Example 1 — Specifying a Single Score Boost

In the following example, the score returned by the search engine is boosted by 2 points if there is a match:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "support");
   &fg.BoostFilterScore(0.02);
   &fg.EndMatchAll();
End-If;

Example 2 — Using the Default Score Boost Mode (with MatchAll)

In the following example, the score returned by the search engine is boosted by 2 points when all filters match because the default boost mode of minimum is applied when no boost mode is explicitly defined:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "support");
   &fg.BoostFilterScore(0.02);
   &fg.ContainsWord("QE_STATUS", "open");
   &fg.BoostFilterScore(0.03);
   &fg.EndMatchAll();
End-If;

Example 2 — Using the Default Score Boost Mode (with MatchAny)

Similarly, in the following example, the score returned by the search engine is also boosted by 2 points when both filter matches because the default boost mode of minimum is applied when no boost mode is explicitly defined; however if only one filter matches, then the score is boosted by that match alone:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "support");
   &fg.BoostFilterScore(0.02);
   &fg.ContainsWord("QE_STATUS", "open");
   &fg.BoostFilterScore(0.03);
   &fg.EndMatchAny();
End-If;

Example 3 — Specifying Sum as the Score Boost Mode

In the following example, if there is a match for both filters, then the score would be boosted by 5 points, which is the sum of the score boosts; otherwise, if just one filter is matched, then the score is boosted by that match alone:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "support");
   &fg.BoostFilterScore(0.02);
   &fg.ContainsWord("QE_STATUS", "open");
   &fg.BoostFilterScore(0.03);
   &fg.EndMatchAny();
   &fg.setBoostModeSum();
   Rem &fg.setBoostMode("ADD"); /* Equivalent mode */
End-If;

See setBoostMode and setBoostModeSum for more information.

Example 4 — Adding a Filter That Does Not Include a Score Boost

The score returned by the search engine is boosted by 5 points, which is the sum of the score boosts; otherwise, if just one filter is matched, then the score is boosted by that match alone. While this example includes a third filter (city = Sydney), it does not contribute to the score boosting.

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "systems");
   &fg.BoostFilterScore(0.02);
   &fg.ContainsWord("QE_STATUS", "draft");
   &fg.BoostFilterScore(0.03);
   &fg.ContainsWord("CITY", "Sydney");
   &fg.EndMatchAny();
   &fg.setBoostModeSum();
End-If;

Example 5 — Using the BoostCompositeFilterScore Method

While the BoostCompositeFilterScore method provides a boost of 2 points for the inner block of filters (match any), the entire score boost would be 6 points if all filters match:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("DEPTNAME", "Support");
   &fg.BoostFilterScore(0.02);
   Local date &date = Date("2009/05/07");
   &fg.AfterDateValue("OPEN_DT", &date);
   &fg.BoostFilterScore(0.02);
   &fg.ContainsWord("QE_YEAR_H", "2009");
   If &fg.MatchAnyReturnTrue() Then
      &fg.ContainsWord("QE_YEAR_H", "07");
      &fg.ContainsWord("QE_STATUS", "Draft");
      &fg.BoostCompositeFilterScore(0.02);
      &fg.EndMatchAny();
   End-If;
   &fg.EndMatchAll();
   &fg.setBoostModeSum();
End-If;

See BoostCompositeFilterScore for more information.

Example 7 — Using the ContainsPhrase Method

In the following example, the score returned by the search engine is boosted by 2 points when the phrase is matched exactly:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsPhrase("MESSAGE_TEXT", "some long days");
   &fg.BoostFilterScore(0.02);
   &fg.EndMatchAny();
End-If;

See ContainsPhrase for more information.

Example 8 — Performing a Search Using Wildcard Characters

Use the * wildcard to search and match on one or more characters; use the ? wildcard to search and match on a single character.

In the following example, the score returned by the search engine is boosted by 1 point when all filters match because the default boost mode of minimum is applied when no boost mode is explicitly defined:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("MESSAGE_TEXT", "l*g");
   &fg.BoostFilterScore(0.01);
   &fg.ContainsWord("MESSAGE_TEXT", "b??ght");
   &fg.BoostFilterScore(0.02);
   &fg.EndMatchAll();
End-If;

Example 9 — Performing Stemming in a Search

Use the $ character to indicate that stemming is to be performed.

In the following example, the score returned by the search engine is boosted by 3 points if there is a match:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("MESSAGE_TEXT", "$wanted");
   &fg.BoostFilterScore(0.03);
   &fg.EndMatchAll();
End-If;

Example 10 — Performing a Phonetic Search

Use the ! character to indicate that a phonetic search is to be performed.

In the following example, the score returned by the search engine is boosted by 3 points if there is a match:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAllReturnTrue() Then
   &fg.ContainsWord("MESSAGE_TEXT", "!smithee");
   &fg.BoostFilterScore(0.03);
   &fg.EndMatchAll();
End-If;

Example 11 — Performing a Fuzzy Search

Use the ~ character to indicate that a fuzzy search is to be performed.

In the following example, the score returned by the search engine is boosted by 2 points if there is a match:

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsWord("MESSAGE_TEXT", "speeks~");
   &fg.BoostFilterScore(0.02);
   &fg.EndMatchAny();
End-If;

Example 12 — Specifying Custom Scoring with a Keyword-Like Search

Typically, a keyword-like search (similar to a keyword search performed on the Global Search field in the browser) would be constructed programmatically by using the QueryText property of the SearchQuery class. To specify custom scoring for a keyword-like search, you would use either the ContainsWord or ContainsPhrase methods without specifying the search field name value (that is, it is an empty string indicating that the search is against all search fields) as show in the following example.

import PT_SEARCH:SearchFilterGenerator;

Local PT_SEARCH:SearchFilterGenerator &fg = create PT_SEARCH:SearchFilterGenerator();

If &fg.MatchAnyReturnTrue() Then
   &fg.ContainsWord("", "long");
   &fg.BoostFilterScore(0.02);
   &fg.EndMatchAny();
End-If;

See QueryText for more information for the typical approach to performing a keyword-like search.