Improve Search


1. Search Highlighter

1.1. Full words


Search action: Highlight only full words. (default)

<?php

$highlight_this = function ($text, $words)
{
    $text            = Ut::html($text);
    $words            = trim($words);
    $words_array    = explode(' ', $words);
    $boundaries        = '[\\p{Z}\\p{P}\\p{C}]';

    if ($words_array)
    {
        $pat_pre        = "(^|{$boundaries})";
        $pat_post        = "({$boundaries}|$)";

        foreach ($words_array as $word)
        {
            // escape bad regex characters
            $word    = preg_quote($word, '/');

            $pattern    = "/$pat_pre(" . $word . ")$pat_post/ui";
            $text        = preg_replace($pattern, "\\1<mark class='highlight'>\\2</mark>\\3", $text);
        }
    }

    return $text;
};

1.2. Strings

Search action: Highlight strings. (hl_simple=1)
Ideographic languages such as Chinese and Japanese do not have word delimiters.

<?php

$highlight_simple = function ($text, $words)
{
    $hl_words        = [];
    $text            = Ut::html($text);
    $words            = trim($words);
    $words_array    = explode(' ', $words);

    if ($words_array)
    {
        foreach ($words_array as $word)
        {
            // escape bad regex characters
            $hl_words[] = preg_quote($word, '/');
        }

        $pattern    = implode('|', $hl_words);
        $text    = preg_replace('/(' . $pattern . ')/ui', '<mark class="highlight">$1</mark>', $text);
    }

    return $text;
};

2. ngram Full-Text Parser

InnoDB full-text index for Chinese search, not supported by MariaDB.


Add add WITH PARSER ngram by alter the page table

ALTER TABLE page ADD FULLTEXT INDEX ft_index (title, body) WITH PARSER ngram;