Skip to content

Commit

Permalink
Merge pull request #233 from JosephLees/feature/exact-match-limit
Browse files Browse the repository at this point in the history
Flag to return all FuzzySearch results on exact match
  • Loading branch information
nticaric authored Feb 16, 2023
2 parents ca00916 + fbde344 commit 161d336
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/TNTSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TNTSearch
public $fuzzy_prefix_length = 2;
public $fuzzy_max_expansions = 50;
public $fuzzy_distance = 2;
public $fuzzy_no_limit = false;
protected $dbh = null;

/**
Expand Down Expand Up @@ -105,12 +106,13 @@ public function search($phrase, $numOfResults = 100)
$dlWeight = 0.5;
$docScores = [];
$count = $this->totalDocumentsInCollection();
$noLimit = $this->fuzzy_no_limit;

foreach ($keywords as $index => $term) {
$isLastKeyword = ($keywords->count() - 1) == $index;
$df = $this->totalMatchingDocuments($term, $isLastKeyword);
$idf = log($count / max(1, $df));
foreach ($this->getAllDocumentsForKeyword($term, false, $isLastKeyword) as $document) {
foreach ($this->getAllDocumentsForKeyword($term, $noLimit, $isLastKeyword) as $document) {
$docID = $document['doc_id'];
$tf = $document['hit_count'];
$num = ($tfWeight + 1) * $tf;
Expand Down Expand Up @@ -244,7 +246,7 @@ public function searchBoolean($phrase, $numOfResults = 100)
*/
public function getAllDocumentsForKeyword($keyword, $noLimit = false, $isLastKeyword = false)
{
$word = $this->getWordlistByKeyword($keyword, $isLastKeyword);
$word = $this->getWordlistByKeyword($keyword, $isLastKeyword, $noLimit);
if (!isset($word[0])) {
return new Collection([]);
}
Expand Down Expand Up @@ -300,7 +302,7 @@ public function totalMatchingDocuments($keyword, $isLastWord = false)
*
* @return array
*/
public function getWordlistByKeyword($keyword, $isLastWord = false)
public function getWordlistByKeyword($keyword, $isLastWord = false, $noLimit = false)
{
$searchWordlist = "SELECT * FROM wordlist WHERE term like :keyword LIMIT 1";
$stmtWord = $this->index->prepare($searchWordlist);
Expand All @@ -315,7 +317,7 @@ public function getWordlistByKeyword($keyword, $isLastWord = false)
$stmtWord->execute();
$res = $stmtWord->fetchAll(PDO::FETCH_ASSOC);

if ($this->fuzziness && !isset($res[0])) {
if ($this->fuzziness && (!isset($res[0]) || $noLimit)) {
return $this->fuzzySearch($keyword);
}
return $res;
Expand Down
23 changes: 23 additions & 0 deletions tests/TNTSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,29 @@ public function testFuzzySearchMultipleWordsFound()
$this->assertContains(15, $res['ids']);
}

public function testFuzzySearchOnExactMatchWithNoLimit()
{
$tnt = new TNTSearch();
$tnt->loadConfig($this->config);
$indexer = $tnt->createIndex($this->indexName);
$indexer->disableOutput = true;
$indexer->query('SELECT id, title, article FROM articles;');
$indexer->run();

$tnt->selectIndex($this->indexName);
$index = $tnt->getIndex();

$index->insert(['id' => '14', 'title' => '199x', 'article' => 'Nineties with the x...']);
$index->insert(['id' => '15', 'title' => '199y', 'article' => 'Nineties with the y...']);
$tnt->fuzziness = true;
$res = $tnt->search('199x');
$this->assertEquals([14], $res['ids']);

$tnt->fuzzy_no_limit = true;
$res = $tnt->search('199x');
$this->assertEquals([14,15], $res['ids']);
}

public function testIndexDoesNotExistException()
{
$this->expectException(IndexNotFoundException::class);
Expand Down

0 comments on commit 161d336

Please sign in to comment.