webCOMAND

search::__construct()

Create a new search object that can be used to perform multiple searches.

Prototype

search search(repository $repo, $options = [])

Parameters

  • repo - The repository connect to use to perform the search.
  • options - The following options may be specified in an associative array.
    • select - Optional array of cQL SELECT expressions to include in results.  Similar to standard repository queries, this can be used to preload field values to improve performance.  If not specified, the field values can still be accessed, but they will be loaded on-demand when requested (aka lazy-loaded).
    • from - Optional array of content types to include in the search.  If not specified, all content types will be searched.  Similar to a repository query, the content type Identifier must be given with an optional trailing '+' to also include all content types that extend the specified content type.  For example, ['Contact', 'WebPage+']
    • in - Optional cPath string to limit results to objects in a specific folder or field.  This corresponds to the cQL IN clause.
    • with - Optional cQL WITH clause to specify which dimension of each object is returned.
    • where - Optional cQL WHERE clause to limit results based on specific criteria.  This can be used for faceted searches.
    • group_by - Not typically useful, but can be used to group results if needed.  Aggregate expressions can be specified for the select option.
    • having - Optional cQL HAVING clause to filter results grouped with the group_by option.
    • order_by - Optional array of cQL ORDER BY expressions.  These will come before the default score-based ordering.
    • limit - Optional cQL LIMIT expression to limit the number and offset of results, which is useful for paging.  Can be provided as a string (comma-separated for offset, count) or array (offset, count).
    • keyword_regex - A string or array of strings that contain a pattern to find on each keyword, in the form of a regular expression.  It will be passed to preg_replace() as the pattern parameter, where the replacement parameter is keyword_replace (see next) and the subject parameter is the keyword.  The default is '/[^a-z0-9!@#$%^&*()\-_+=\[\]{}|\\:;]/'.
    • keyword_replace - A string or array of strings that contain a replacement on each keyword_regex match.  It will be passed to preg_replace() as the replacement parameter, where the pattern is the keyword_regex (see previous) and the subject parameter is the keyword.  The default is '' (empty string).
    • log - Custom Event Logger to log events, including debug events (see debug option).
    • score - If set to TRUE, a Score field will be included with each result.  The score is determined by the rules.  This option can also be set to a string to specify a field name to use instead of Score.
    • unordered - Set to TRUE to skip the automatic ORDER BY clause.  Useful for getting a count of all results.
    • rows - Set to TRUE to internally call get_rows() instead of get() when ambiguous (ie. static search() is called).
    • rules - Optional custom search rules to override the default.  The default rules can also be retrieved with the default_rules() method and then modified.
      • <group name> - Unique name for this group of fields and corresponding rules.
        • fields - array of dot-notation field names to compare to all rules in this group
        • keyphrase or keyword - Defines one or more rules to be applied to the entire search keyphrase or individual keywords.  Each rule may define optional functions that receive an associative array of information (see below) about the keyphrase or keyword and return a cQL expression that will be applied to the search query as described below.
          • <rule name> - Unique name for this rule.  This name will be included in the results if debug notices.
            • where - Optional function to return a cQL WHERE expression that will be used to filter results based on the keyphrase or a keyword.
            • condition - Optional function to return a cQL WHERE expression that will be used to determine if a score should be applied for the keyphrase or a keyword.
            • score - Optional function to return a cQL SELECT expression that will be used to determine the score if no condition is specified, or the condition evaluates to TRUE.  While not required, it is a best practice to produce scores that range between 0 (lowest) and 1 (highest).  The weight of a rule can then be used to more easily increase or decrease the relative influence of that score.
            • weight - Optional function that will return a numeric weight to multiply the resulting score by.  It can be positive decimal number (adjust relative influence) or negative decimal number (invert relative influence).
    • debug - Set to TRUE to log debug events, otherwise FALSE (default).

Keyphrase Function Parameter

An associative array with the following values is passed to the keyphrase functions.

  • field - Content Type Field Identifier of a field in this rule group's field array.  The Keyphrase and Keyword functions will be called on each field in the array.
  • keyphrase - The entire keyphrase.
  • quoted_keyphrase - The entire keyphrase in quotes.
  • keyphrase_length - Length of the entire keyphrase.
  • keyphrase_like_filter - The entire keyphase wrapped with like wildcards (ie. '%<keyphrase>%'.
  • keyphrase_like_filter_start - The entire keyphase with a trailing like wildcard (ie. '<keyphrase>%'.

Keyword Function Parameter

An associative array with the following values is passed to the keyphrase functions.

  • field - Content Type Field Identifier of a field in this rule group's field array.  The Keyphrase and Keyword functions will be called on each field in the array.
  • keyword - The entire keyphrase.
  • quoted_keyword - This one keyword in quotes.
  • keyword_length - The length of this one keyword.
  • keyword_like_filter - This one keyword wrapped with like wildcards (ie. '%<keyword>%'.
  • keyword_like_filter_start - This one keyword with a trailing like wildcard (ie. '<keyphrase>%'.

Example

// Perform multiple searches on Web Pages with custom search rules.
$repo = comand::repo();
$default_rules = \io_comand_search\search::default_rules();
$search = new \io_comand_search\search($repo, [
    'select' => ['Title', '.Summary()'],
    'from' => ['WebPage+'],
    'where' => ["Status='active'", "Tags.Title='Health'"],
    'limit' => 12,
    'rules' => array_merge_recursive($default_rules, [
        'fields' => ['Title', '.Description()'],
        'keyword' => [
            'matches_title' => [
                'where' => function($vars) {
                    return $vars['field'] . ' ILIKE ' . $vars['keyword_like_filter'];
                },
                'condition' => function($vars) {
                    return $vars['field'] . ' ILIKE ' . $vars['keyword_like_filter'];
                },
                'score' => function($vars) {
                    return 1;
                },
                'weight' => function($vars) { return 100; }
            ]
        ]
    ])
]);
$results = $search->get('first search');
foreach($results as $result) {
    echo("$result\n");
}

$results = $search->get('second search');
foreach($results as $result) {
    echo("$result\n");
}