webCOMAND

search::search()

Search a COMAND repository by keyword with optional facets and custom search rules.

Prototype

collection search( repository $repo, string $keywords, array $options = [] )

Parameters

  • repo - Repository connection to use for the search.  It should be connected with the appropriate connection mode and user authorization to avoid revealing unauthorized content.
  • keywords - String containing space-separated keywords.
  • options - See Constructor options.

Return

Returns a collection of objects that match the search in order of best to worst match.

Simple Example

The following example will search the entire repository based on a simple keyword string.

// Perform a simple keyword search on the entire repository.
$repo = comand::repo();
$results = \io_comand_search\search::search($repo, "Content Type");
foreach($results as $result) {
    echo("$result\n");
}

Advanced Example

The following example will search all Web Page objects, and objects of a Content Type that extends Web Page.

// Perform a faceted search on Web Pages with custom search rules.
$repo = comand::repo();
$default_rules = \io_comand_search\search::default_rules();
$results = \io_comand_search\search::search($repo, "treatments", [
    '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; }
            ]
        ]
    ])
]);
foreach($results as $result) {
    echo("$result\n");
}