return function () {
    $self = url('/@object/@action/' . urlencode('search/products'));
    $demo = _class('docs')->_sample('search');
    $products = _class('docs')->_sample('products');
    $all = $products->load();
    $colours = $products->colours($all);
    $p = $demo->params($_GET, array_keys($colours));

    $intro = '<p class="docs-demo-intro">Full-text style search with the search plugin: <code>_class(\'wrapper_search\')->search($params, $error)</code>
        passes the same parameters (text, weighted fields, filters, sort, page, highlight, facets) to the configured driver, so a site can move
        from the database drivers to Manticore, Elasticsearch or OpenSearch without changing the call. Here the <code>db_like</code> driver
        (one <code>LIKE</code> per word; every word must be in a field, a match in the name weighs 3, in the description 1) searches the
        ' . count($all) . ' sample products, loaded into an SQLite database in memory for this request only: the site database is not used and
        nothing is sent to a search service. The text is limited to ' . $demo->MAX_QUERY_LEN . ' letters, digits and <code>, . - \' % _</code>;
        the driver escapes it, so <code>%</code> and <code>_</code> match themselves (try <code>100%</code>). The other values come from fixed lists.
        Classes: <code>yf_wrapper_search</code>, <code>yf_search_driver_db_like</code>, <code>yf_db_driver_pdo_sqlite</code>.</p>';

    $options = function ($name, array $values, $selected, $label) {
        $html = '<select name="' . $name . '" class="form-control form-select input-sm form-select-sm" aria-label="' . $label . '">';
        foreach ($values as $value => $text) {
            $html .= '<option value="' . _prepare_html((string) $value, 0) . '"' . ((string) $value === (string) $selected ? ' selected' : '') . '>' . _prepare_html((string) $text, 0) . '</option>';
        }
        return $html . '</select>';
    };
    $prices = [];
    foreach ($demo->MAX_PRICES as $price) {
        $prices[$price] = $price ? 'up to $' . $price : 'Any price';
    }
    $form = '<form method="get" action="' . $self . '" class="docs-demo-search-form" role="search">'
        . '<input type="search" name="q" class="form-control input-sm form-control-sm docs-demo-search-q" maxlength="' . $demo->MAX_QUERY_LEN . '" placeholder="e.g. black skull, cotton, 100%" aria-label="Search text" value="' . _prepare_html($p['q'], 0) . '">'
        . $options('colour', ['' => 'Any colour'] + $colours, $p['colour'], 'Colour')
        . $options('max_price', $prices, $p['max_price'], 'Price')
        . $options('sort', array_map(fn ($s) => $s[0], $demo->SORTS), $p['sort'], 'Sort')
        . '<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i> Search</button>'
        . '<a class="btn btn-default btn-outline-secondary btn-sm" href="' . $self . '">Reset</a></form>';

    if ( ! extension_loaded('pdo_sqlite')) {
        return $intro . '<div class="alert alert-info">This demo searches an in-memory SQLite copy of the sample products and needs the PHP
            <code>pdo_sqlite</code> extension, which this server does not have.</div>';
    }
    $driver = $demo->driver($demo->memory_db($demo->rows($all)));
    $error = '';
    $result = $driver->search($demo->search_params($p), $error);
    $pages = $result === false ? 1 : max(1, (int) ceil($result['total'] / $demo->PER_PAGE));
    if ($result !== false && $p['page'] > $pages) {
        $p['page'] = $pages;
        $result = $driver->search($demo->search_params($p), $error);
    }
    if ($result === false) {
        // $error can hold the raw database error: it is not for visitors
        return $intro . $form . '<div class="alert alert-danger">The search failed.</div>';
    }

    $facets = '<a class="btn btn-xs btn-sm ' . ($p['colour'] === '' ? 'btn-primary active' : 'btn-default btn-outline-primary') . '" href="' . $self . '?' . _prepare_html($demo->query($p, ['colour' => '', 'page' => 1]), 0) . '">any</a> ';
    foreach ($result['facets']['colour'] ?? [] as $facet) {
        $value = (string) $facet['value'];
        $facets .= '<a class="btn btn-xs btn-sm ' . ($value === $p['colour'] ? 'btn-primary active' : 'btn-default btn-outline-primary') . '" href="' . $self . '?' . _prepare_html($demo->query($p, ['colour' => $value, 'page' => 1]), 0) . '">'
            . _prepare_html($value, 0) . ' <span class="docs-demo-search-count">' . (int) $facet['count'] . '</span></a> ';
    }

    $items = '';
    foreach ($result['items'] as $item) {
        $doc = $item['doc'];
        // highlight values are HTML from the driver: the text escaped, only the <mark> pre / post added
        $name = $item['highlight']['name'] ?? _prepare_html((string) $doc['name'], 0);
        $desc = $item['highlight']['description'] ?? _prepare_html((string) $doc['description'], 0);
        $items .= '<li class="list-group-item docs-demo-search-item"><div class="docs-demo-search-head"><b>' . $name . '</b>'
            . '<span class="docs-demo-search-price">$' . number_format((float) $doc['price'], 2) . '</span></div>'
            . '<div class="text-muted">' . $desc . '</div>'
            . '<small class="text-muted">colour ' . _prepare_html((string) $doc['colour'], 0) . ' · sold ' . (int) $doc['sold']
            . ($p['q'] !== '' ? ' · score ' . _prepare_html((string) $item['score'], 0) : '') . '</small></li>';
    }
    $found = $result['total'] ? '<ul class="list-group docs-demo-search-results">' . $items . '</ul>'
        : '<div class="alert alert-info">No product matches. Every word has to be found in the name or the description.</div>';

    $suggest = '';
    if ($p['q'] !== '') {
        $names = $driver->suggest($p['q'], ['index' => $demo->INDEX, 'field' => 'name', 'limit' => 5]);
        if ($names) {
            $links = [];
            foreach ($names as $n) {
                $links[] = '<a href="' . $self . '?' . _prepare_html($demo->query(['q' => $n] + $p, ['page' => 1]), 0) . '">' . _prepare_html($n, 0) . '</a>';
            }
            $suggest = '<p class="docs-demo-search-suggest">Names starting with “' . _prepare_html($p['q'], 0) . '” (<code>suggest()</code>): ' . implode(', ', $links) . '</p>';
        }
    }

    $pager = '';
    if ($result['total'] > $demo->PER_PAGE) {
        [, $pager] = common()->divide_pages('', $self . '?' . _prepare_html($demo->query($p, ['page' => 1]), 0), 'slide', $demo->PER_PAGE, $result['total'], '', 0, [
            'requested_page' => $p['page'],
            'add_get_vars' => 0,
        ]);
    }

    css('.docs-demo-search-form { display:flex; flex-wrap:wrap; align-items:center; gap:8px; margin-bottom:12px; }
        .docs-demo-search-form .form-control { display:inline-block; width:auto; } .docs-demo-search-form .docs-demo-search-q { flex:1 1 220px; min-width:180px; }
        .cssfw-bs5 .docs-demo-search-form select.form-select { padding-right:2rem; }
        .docs-demo-search-facets .btn { margin:0 4px 6px 0; } .docs-demo-search-count { opacity:.7; }
        .docs-demo-search-head { display:flex; justify-content:space-between; gap:12px; } .docs-demo-search-price { white-space:nowrap; font-weight:bold; }
        .docs-demo-search-results mark { padding:0 1px; background:#fff3a3; color:inherit; }
        pre.docs-demo-search-code code { white-space:pre-wrap; }');
    return $intro . $form
        . '<p class="docs-demo-search-facets"><span class="text-muted">Colour facet:</span> ' . $facets . '</p>'
        . '<p class="text-muted">' . (int) $result['total'] . ' of ' . count($all) . ' products match, page ' . $p['page'] . ' of ' . $pages
        . ' (' . _prepare_html((string) $result['took_ms'], 0) . ' ms)</p>'
        . $suggest . $found . $pager
        . '<pre class="docs-demo-search-code"><code class="php">' . _prepare_html($demo->code($p), 0) . '</code></pre>';
};

Full-text style search with the search plugin: _class('wrapper_search')->search($params, $error) passes the same parameters (text, weighted fields, filters, sort, page, highlight, facets) to the configured driver, so a site can move from the database drivers to Manticore, Elasticsearch or OpenSearch without changing the call. Here the db_like driver (one LIKE per word; every word must be in a field, a match in the name weighs 3, in the description 1) searches the 20 sample products, loaded into an SQLite database in memory for this request only: the site database is not used and nothing is sent to a search service. The text is limited to 40 letters, digits and , . - ' % _; the driver escapes it, so % and _ match themselves (try 100%). The other values come from fixed lists. Classes: yf_wrapper_search, yf_search_driver_db_like, yf_db_driver_pdo_sqlite.

Colour facet: any black 9 white 5 purple 2 black and green 1 green 1 grey 1 navy 1

20 of 20 products match, page 4 of 4 (0.49 ms)

  • Liberty T-shirt, black$35.00
    A T-shirt made in the USA, 100% cotton.
    colour black · sold 0
  • Pray Skull T-shirt, black$35.00
    A T-shirt made in the USA, 100% cotton.
    colour black · sold 0
  • Blood Head T-shirt, black$40.00
    A T-shirt made in the USA, 100% cotton.
    colour black · sold 0
  • Outlaw T-shirt, black$40.00
    A T-shirt made in the USA, 100% cotton.
    colour black · sold 0
  • Random T-shirt, black$40.00
    A T-shirt made in the USA, 100% cotton.
    colour black · sold 0
$result = _class('wrapper_search')->search([
    'index' => 'products',
    'q' => '',
    'fields' => ['name' => 3, 'description' => 1],
    'filters' => [],
    'sort' => ['_score' => 'desc'],
    'page' => 4,
    'per_page' => 5,
    'highlight' => ['fields' => ['name', 'description'], 'pre' => '<mark>', 'post' => '</mark>'],
    'facets' => ['colour'],
], $error);