Skip to content

Commit

Permalink
#37 Now checks the sort order of weighted search results; moves testi…
Browse files Browse the repository at this point in the history
…ng for missing or equal boost factors to a separate test
  • Loading branch information
extracts committed Nov 23, 2023
1 parent 541bc43 commit c78afc7
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions test/Solr/Solarium/AdapterSearchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
namespace OpusTest\Search\Solr\Solarium;

use Exception;
use Opus\Common\Config;
use Opus\Common\Document;
use Opus\Common\Person;
use Opus\Search\Query;
Expand Down Expand Up @@ -271,67 +270,86 @@ public function testWeightedSearch()

$query = new Query();
$query->addSorting('score', false);
$query->setWeightedSearch(true); // use the Solr eDisMax query parser

$filter = $search->createFilter();
$filter->createSimpleEqualityFilter('*')->addValue('test document');
$query->setFilter($filter);

// 1. without any boost factors assigned to fields, expect roughly equal scores
$query->setWeightedFields([]);
// 1. with different boost factors assigned to fields, expect clearly different scores & appropriate sort order
$this->adjustConfiguration([
'search' => [
'weightedSearch' => true, // use the Solr eDisMax query parser
'simple' => [
'abstract' => 0.5,
'title' => 10,
],
],
]);

$result = $search->customSearch($query);
$matches = $result->getReturnedMatches();

$this->assertEquals(2, count($matches));

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0);
$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) > 1.0);

// 2. with equal boost factors assigned to fields, expect roughly equal scores
$query->setWeightedFields(['title' => 1.0, 'abstract' => 1.0]);
$this->assertEquals('Another Test Document', $matches[0]->getDocument()->getTitleMain(0)->getValue());

// 2. with swapped boost factors, expect a swapped sort order
$query->setWeightedFields(['abstract' => 10.0, 'title' => 0.5]);

$result = $search->customSearch($query);
$matches = $result->getReturnedMatches();

$this->assertEquals(2, count($matches));

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0);
$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) > 1.0);

// 3. with different boost factors assigned to fields, expect clearly different scores
$this->adjustConfiguration([
'search' => [
'simple' => [
'title' => 10,
'abstract' => 0.5,
],
],
]);
$this->assertEquals('Some Document', $matches[0]->getDocument()->getTitleMain(0)->getValue());
}

public function testWeightedSearchWithEqualWeights()
{
$docA = Document::new();
$docA->addTitleMain()->setLanguage("eng")->setValue("Some Document");
$docA->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of test document A.\nSome more text.");
$docA->store();

$config = Config::get();
$weightedFields = $config->search->simple->toArray();
$docB = Document::new();
$docB->addTitleMain()->setLanguage("eng")->setValue("Another Test Document");
$docB->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of document B.\nSome blah blah text.");
$docB->store();

$query->setWeightedFields($weightedFields);
$index = Service::selectIndexingService(null, 'solr');
$index->addDocumentsToIndex([$docA, $docB]);

$search = Service::selectSearchingService(null, 'solr');

$query = new Query();
$query->setWeightedSearch(true);

$filter = $search->createFilter();
$filter->createSimpleEqualityFilter('*')->addValue('test document');
$query->setFilter($filter);

// 1. without any boost factors assigned to fields, expect roughly equal scores
$query->setWeightedFields([]);

$result = $search->customSearch($query);
$matches = $result->getReturnedMatches();

$this->assertEquals(2, count($matches));

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) > 1.0);

$this->assertEquals('Another Test Document', $matches[0]->getDocument()->getTitleMain(0)->getValue());

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0);

// 4. with swapped boost factors, also expect a swapped sort order
$query->setWeightedFields(['title' => 0.5, 'abstract' => 10.0]);
// 2. with equal boost factors assigned to fields, expect roughly equal scores
$query->setWeightedFields(['abstract' => 1.0, 'title' => 1.0]);

$result = $search->customSearch($query);
$matches = $result->getReturnedMatches();

$this->assertEquals(2, count($matches));

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) > 1.0);

$this->assertEquals('Some Document', $matches[0]->getDocument()->getTitleMain(0)->getValue());
$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0);
}
}

0 comments on commit c78afc7

Please sign in to comment.