Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Engines/MysqlEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function createIndex(string $indexName)
"CREATE TABLE IF NOT EXISTS {$this->indexName}_doclist (
term_id INTEGER,
doc_id VARCHAR(255),
hit_count INTEGER);"
hit_count FLOAT);"
);

$this->index->exec(
Expand Down Expand Up @@ -198,13 +198,15 @@ public function saveWordlist(Collection $stems)

public function saveDoclist(array $terms, int $docId)
{
$countTerms = count($terms);
$insertRows = [];
foreach ($terms as $term) {
$insertRows[] = '(' . $this->index->quote($term['id']) . ', ' . $this->index->quote($docId) . ', ' . $this->index->quote($term['hits']) . ')';
$insertRows[] = '('.$this->index->quote($term['id']).', '.$this->index->quote($docId).', '.$this->index->quote($term['hits'] / $countTerms).')';
}

$this->index->exec('INSERT INTO ' . $this->indexName . '_doclist (term_id, doc_id, hit_count) VALUES ' . implode(',',
$insertRows) . '');
$this->index->exec(
'INSERT INTO '.$this->indexName.'_doclist (term_id, doc_id, hit_count) VALUES '.implode(',', $insertRows)
);
}

public function saveHitList(array $stems, int $docId, array $termsList)
Expand Down
3 changes: 2 additions & 1 deletion src/Engines/RedisEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ public function saveWordlist(Collection $stems)

public function saveDoclist(array $terms, int $docId)
{
$countTerms = count($terms);
foreach ($terms as $term => $docsHits) {
$redisKey = "{$this->indexName}:doclist:{$term}:{$docId}";
$this->redis->hset($redisKey, 'num_hits', $docsHits['num_hits']);
$this->redis->hset($redisKey, 'num_hits', $docsHits['num_hits'] / $countTerms);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Engines/SqliteEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function createIndex(string $indexName)
$this->index->exec("CREATE TABLE IF NOT EXISTS doclist (
term_id INTEGER,
doc_id INTEGER,
hit_count INTEGER)");
hit_count FLOAT)");

$this->index->exec("CREATE TABLE IF NOT EXISTS fields (
id INTEGER PRIMARY KEY,
Expand Down Expand Up @@ -317,10 +317,11 @@ public function saveDoclist(array $terms, int $docId)
$insert = 'INSERT INTO doclist (term_id, doc_id, hit_count) VALUES (:id, :doc, :hits)';
$stmt = $this->index->prepare($insert);

$countTerms = count($terms);
foreach ($terms as $term) {
$stmt->bindValue(':id', $term['id']);
$stmt->bindValue(':doc', $docId);
$stmt->bindValue(':hits', $term['hits']);
$stmt->bindValue(':hits', $term['hits'] / $countTerms);
try {
$stmt->execute();
} catch (\Exception $e) {
Expand Down
9 changes: 2 additions & 7 deletions src/TNTSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public function search(string $phrase, int $numOfResults = 100)
$keywords = $keywords->map(function ($keyword) {
return $this->stemmer->stem($keyword);
});
$tfWeight = 1;
$dlWeight = 0.5;

$docScores = [];
$count = $this->totalDocumentsInCollection();
$noLimit = $this->engine->fuzzy_no_limit;
Expand All @@ -127,11 +126,7 @@ public function search(string $phrase, int $numOfResults = 100)
foreach ($this->getAllDocumentsForKeyword($term, $noLimit, $isLastKeyword) as $document) {
$docID = $document['doc_id'];
$tf = $document['hit_count'];
$num = ($tfWeight + 1) * $tf;
$denom = $tfWeight
* ((1 - $dlWeight) + $dlWeight)
+ $tf;
$score = $idf * ($num / $denom);
$score = $idf * $tf;
$docScores[$docID] = isset($docScores[$docID]) ?
$docScores[$docID] + $score : $score;
}
Expand Down