Do you use the current search functionality?

Post Reply
User avatar
cobber
Master Bludit
Posts: 78
Joined: Sun Feb 28, 2016 10:15 am
Location: Scotland
Has thanked: 21 times
Been thanked: 5 times

Hi guys.

Loving Bludit and am taking a bit of a change of direction and changing the look and goals for my website. What are your thoughts on the current search functionality within Bludit?

I'm asking because I have search working but it's a bit of a hit and miss. I get relevant results but I also receive addition results that shouldn't be there.

The tags search works brilliantly but the normal search . . . meh

Just wondered what everyone else uses if anything at all.

Many thanks
haywardgg
Ssr. Bludit
Posts: 19
Joined: Wed Apr 05, 2023 10:46 pm
Has thanked: 4 times
Been thanked: 1 time

I found that it was searching Case sensitive (so seaerching 'jackson' wouldn't return pages with 'Jackson' and vise versa). I changed the search function in the search plugin and fixed it. But I still get results that don't have the keywords, which is annoying haha!

Code: Select all

private function search($text)
{
    // Read the cache file
    $json = file_get_contents($this->cacheFile());
    $cache = json_decode($json, true);

    // Convert search text to lowercase
    $text = strtolower($text);

    // Convert cache data to lowercase
    $cache = array_map('array_change_key_case', $cache);

    // Include Fuzz algorithm
    require_once($this->phpPath().'vendors/fuzz.php');
    $fuzz = new Fuzz($cache, 10, 1, true);
    $results = $fuzz->search($text, $this->getValue('minChars'));

    return array_keys($results);
}
haywardgg
Ssr. Bludit
Posts: 19
Joined: Wed Apr 05, 2023 10:46 pm
Has thanked: 4 times
Been thanked: 1 time

cobber wrote: Tue Apr 25, 2023 7:45 pm Hi guys.

Loving Bludit and am taking a bit of a change of direction and changing the look and goals for my website. What are your thoughts on the current search functionality within Bludit?

I'm asking because I have search working but it's a bit of a hit and miss. I get relevant results but I also receive addition results that shouldn't be there.

The tags search works brilliantly but the normal search . . . meh

Just wondered what everyone else uses if anything at all.

Many thanks
In your search plugin folder, edit plugin.php and replace the search function with this:

Code: Select all

private function search($text)
{
    // Read the cache file
    $json = file_get_contents($this->cacheFile());
    $cache = json_decode($json, true);

    // Convert search text to lowercase
    $text = strtolower($text);

    // Convert cache data to lowercase
    $cache = array_map('array_change_key_case', $cache);

    // Include Fuzz algorithm
    require_once($this->phpPath().'vendors/fuzz.php');
    $fuzz = new Fuzz($cache, 10, 1, true);

    $results = [];
    foreach ($cache as $key => $value) {
        // Perform exact word match using regular expression
        if (preg_match('/\b' . preg_quote($text) . '\b/i', strtolower($value['title']))) {
            $results[$key] = $value;
        }
    }

    return array_keys($results);
}
What was happening for me, is that "jackson" would return any result with any page that contain the string "son" , "jack" etc.. My code fixed that :) for me anyway.
medicamin
Jr. Bludit
Posts: 6
Joined: Fri Sep 15, 2023 1:24 pm
Has thanked: 2 times
Been thanked: 2 times

haywardgg wrote: Tue Jun 06, 2023 1:35 pm
In your search plugin folder, edit plugin.php and replace the search function with this:
Thank you for this code. I found that if I replace 'title' with 'content', I get the results which has the search text in their content.

But there is a problem with your code. Persian or Arabic alphabet search breaks. searching in these languages return 0 results. Is there any solution for this?
haywardgg
Ssr. Bludit
Posts: 19
Joined: Wed Apr 05, 2023 10:46 pm
Has thanked: 4 times
Been thanked: 1 time

medicamin wrote: Sat Sep 23, 2023 8:25 pm
haywardgg wrote: Tue Jun 06, 2023 1:35 pm
In your search plugin folder, edit plugin.php and replace the search function with this:
Thank you for this code. I found that if I replace 'title' with 'content', I get the results which has the search text in their content.

But there is a problem with your code. Persian or Arabic alphabet search breaks. searching in these languages return 0 results. Is there any solution for this?
Try this:

Code: Select all

private function search($text)
{
    // Read the cache file
    $json = file_get_contents($this->cacheFile());
    $cache = json_decode($json, true);

    // Convert search text to lowercase with multibyte support
    $text = mb_strtolower($text, 'UTF-8');

    // Convert cache data to lowercase with multibyte support
    $cache = array_map(function ($item) {
        return array_map('mb_strtolower', $item, array_fill(0, count($item), 'UTF-8'));
    }, $cache);

    // Include Fuzz algorithm
    require_once($this->phpPath().'vendors/fuzz.php');
    $fuzz = new Fuzz($cache, 10, 1, true);

    $results = [];
    foreach ($cache as $key => $value) {
        // Perform exact word match using regular expression with Unicode word boundaries
        if (preg_match('/\b' . preg_quote($text, '/') . '\b/u', $value['title'])) {
            $results[$key] = $value;
        }
    }

    return array_keys($results);
}
medicamin
Jr. Bludit
Posts: 6
Joined: Fri Sep 15, 2023 1:24 pm
Has thanked: 2 times
Been thanked: 2 times

Tried, but now even searching with English characters returns no results.
haywardgg
Ssr. Bludit
Posts: 19
Joined: Wed Apr 05, 2023 10:46 pm
Has thanked: 4 times
Been thanked: 1 time

medicamin wrote: Sun Sep 24, 2023 8:39 am Tried, but now even searching with English characters returns no results.
Try the original code (the one that worked for you before) and just replace the strtolower block with:

Code: Select all

    // Convert search text to lowercase with multibyte support
    $text = mb_strtolower($text, 'UTF-8');
I can't test it at the moment, I'm with family. But will have another look this evening when I'm home!
medicamin
Jr. Bludit
Posts: 6
Joined: Fri Sep 15, 2023 1:24 pm
Has thanked: 2 times
Been thanked: 2 times

I just thought something with preg-match could prevent UTF-8 characters matches together. And yes, it was preg-match.
I used this line in results and now I get true results in utf8 characters:

Code: Select all

   if (preg_match('/(*UTF8)' . preg_quote($text ) . '/i', strtolower($value['content']))) {
I must say that \b should be removed to make this work. I don't know why!
Post Reply