Skip to content

Commit

Permalink
improved fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Jul 18, 2018
1 parent 8447a36 commit 40c4395
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/TNTSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ public function totalMatchingDocuments($keyword, $isLastWord = false)
*/
public function getWordlistByKeyword($keyword, $isLastWord = false)
{
if ($this->fuzziness) {
return $this->fuzzySearch($keyword);
}

$searchWordlist = "SELECT * FROM wordlist WHERE term like :keyword LIMIT 1";
$stmtWord = $this->index->prepare($searchWordlist);

Expand All @@ -335,9 +339,6 @@ public function getWordlistByKeyword($keyword, $isLastWord = false)
$stmtWord->execute();
$res = $stmtWord->fetchAll(PDO::FETCH_ASSOC);

if ($this->fuzziness && !isset($res[0])) {
return $this->fuzzySearch($keyword);
}
return $res;
}

Expand All @@ -357,10 +358,22 @@ public function fuzzySearch($keyword)

$resultSet = [];
foreach ($matches as $match) {
if (levenshtein($match['term'], $keyword) <= $this->fuzzy_distance) {
$resultSet[] = $match;
$distance = levenshtein($match['term'], $keyword);
if ($distance <= $this->fuzzy_distance) {
$match['distance'] = $distance;
$resultSet[] = $match;
}
}

// Sort the data by distance, and than by num_hits
$distance = [];
$hits = [];
foreach ($resultSet as $key => $row) {
$distance[$key] = $row['distance'];
$hits[$key] = $row['num_hits'];
}
array_multisort($distance, SORT_ASC, $hits, SORT_DESC, $resultSet);

return $resultSet;
}

Expand Down

0 comments on commit 40c4395

Please sign in to comment.