Skip to content

Commit

Permalink
fix for #60
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Mar 9, 2017
1 parent 7bdc02c commit 57a2463
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Indexer/TNTIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,14 @@ public function delete($documentId)
['key' => ':documentId', 'value' => $documentId]
]);

$this->prepareAndExecuteStatement("DELETE FROM wordlist WHERE num_hits = 0");
$res = $this->prepareAndExecuteStatement("DELETE FROM wordlist WHERE num_hits = 0");

$total = $this->totalDocumentsInCollection() - 1;
$this->updateInfoTable('total_documents', $total);
$affected = $res->rowCount();

if ($affected) {
$total = $this->totalDocumentsInCollection() - 1;
$this->updateInfoTable('total_documents', $total);
}
}

public function updateInfoTable($key, $value)
Expand Down
29 changes: 29 additions & 0 deletions tests/TNTSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ public function testSearchBoolean()
$this->assertEquals([11], $res['ids']);
}

/**
* https://github.com/teamtnt/tntsearch/issues/60
*/
public function testTotalDocumentCountOnIndexUpdate()
{
$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);
$this->assertEquals(12, $tnt->totalDocumentsInCollection());

$index = $tnt->getIndex();

//first we test if the total number of documents will decrease
$index->delete(12);
$this->assertEquals(11, $tnt->totalDocumentsInCollection());

//now we try with a document that does not exist, the total number should increase for 1
$index->update(1234, ['id' => '1234', 'title' => 'updated title', 'article' => 'updated article']);

$this->assertEquals(12, $tnt->totalDocumentsInCollection());
}

public function testIndexUpdate()
{
$tnt = new TNTSearch;
Expand Down

0 comments on commit 57a2463

Please sign in to comment.