teamtnt / tntsearch

A fully featured full text search engine written in PHP
https://tnt.studio/solving-the-search-problem-with-laravel-and-tntsearch
MIT License
3.09k stars 292 forks source link

Fuzziness / Fuzzy-Search not working #300

Closed chris-82 closed 1 year ago

chris-82 commented 1 year ago

Hi there,

I just installed tntsearch to a test project on an Ubuntu Server with PHP 8.1 to do some testing. But whatever I do or configure, there is never a result on a fuzzy search.

I build my index with this example:

use TeamTNT\TNTSearch\TNTSearch;

    $tnt = new TNTSearch;

    $tnt->loadConfig([
        'driver'    => 'mysql',
        'host'      => ENV_DBHOST,
        'database'  => ENV_DBNAME,
        'username'  => ENV_DBUSER,
        'password'  => ENV_DBPASS,
        'storage'   => '/var/www/html/tmp/tntsearch/',
        'charset' => 'utf8',
        'stemmer'   => \TeamTNT\TNTSearch\Stemmer\GermanStemmer::class//optional
    ]);

    $indexer = $tnt->createIndex(ENV_SEARCHINDEX);
    $indexer->query("SELECT id, ident, tags, title, description FROM searchindex;");
    $indexer->setLanguage('german');
    $indexer->run();

My index file is beeing generated properly with several PHP warnings:

Deprecated: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/vendor/teamtnt/tntsearch/src/Support/Tokenizer.php on line 10

Now I search my index for "Regenerative" with this example script:

use TeamTNT\TNTSearch\TNTSearch;

    $tnt = new TNTSearch;

    $config =[
        'driver'    => 'mysql',
        'host'      => ENV_DBHOST,
        'database'  => ENV_DBNAME,
        'username'  => ENV_DBUSER,
        'password'  => ENV_DBPASS,
        'storage'   => '/var/www/html/tmp/tntsearch/',
    ];

    $tnt->loadConfig($config);
    $tnt->selectIndex(ENV_SEARCHINDEX);

    $tnt->fuzziness = true;
    $tnt->fuzzy_prefix_length = 2;
    $tnt->fuzzy_max_expansions = 100;
    $tnt->fuzzy_distance = 2;

    $search = "Regenerative";

    $results = $tnt->search($search, 10);

    echo "<pre>";
    print_r($results);

But when I search for "Regenrative" or "Regeneartive" I don't get any results at all. I tried different search terms and several fuzziness settings without success.

Is there anything that has to be configured when creating the index or can I check somehow, if the fuzzy-search is really active?!

Thanks for any advice.

RmbxCh commented 1 year ago

Hello! I had the same issue, fuzzy search wasn't working because of "$tnt->fuzziness = true;", "fuzziness" was considered as an undefined property by my IDE I tried "$tnt->fuzziness($data_to_search);" instead and now fuzzy search works. I don't know if it's the right way to deal with this issue, but it's working...

alaminfirdows commented 1 year ago

Try $tnt->fuzziness(true); instead of $tnt->fuzziness = true;. The attribute fuzziness doesn't exist on the TNTSearch class.

...
$tnt->loadConfig($config);
$tnt->selectIndex(ENV_SEARCHINDEX);
$tnt->fuzziness(true);

$search = "Regenerative";
$results = $tnt->search($search, 10);
...
chris-82 commented 1 year ago

Try $tnt->fuzziness(true); instead of $tnt->fuzziness = true;. The attribute fuzziness doesn't exist on the TNTSearch class.

...
$tnt->loadConfig($config);
$tnt->selectIndex(ENV_SEARCHINDEX);
$tnt->fuzziness(true);

$search = "Regenerative";
$results = $tnt->search($search, 10);
...

@alaminfirdows using fuzziness as method solved my problem! Thanks a lot :-)