ajuste na pesquisa de informacao, para passar a dar prioridade

This commit is contained in:
2026-11-13 12:07:51 +02:00
parent 71f2fcbb84
commit eab09dd61e
4 changed files with 52 additions and 7 deletions
+21
View File
@@ -3,6 +3,7 @@
namespace Tests\Unit;
use App\Services\FederatedSearch;
use App\Services\SearchTerms;
use PHPUnit\Framework\TestCase;
final class FederatedSearchTest extends TestCase
@@ -44,4 +45,24 @@ final class FederatedSearchTest extends TestCase
$result = (new FederatedSearch)->page($factories, 1, 5, fn ($item) => $item['score']);
$this->assertSame(['Sectores', 'Informação'], array_column($result['data'], 'title'));
}
public function test_direct_matches_precede_associations_across_sources_and_pages(): void
{
$search = new SearchTerms;
$term = 'corredor da beira';
$tokens = $search->tokens($term);
$sources = [
'Oportunidades' => fn () => ['count' => 2, 'fetch' => fn ($offset, $limit) => array_slice([
['title' => 'Açúcar no Corredor da Beira'], ['title' => 'Produção de açúcar'],
], $offset, $limit)],
'Carteira' => fn () => ['count' => 1, 'fetch' => fn ($offset, $limit) => array_slice([
['title' => 'Corredor da Beira'],
], $offset, $limit)],
];
$score = fn ($item) => $search->score($item['title'], $term, $tokens);
$service = new FederatedSearch;
$this->assertSame(['Corredor da Beira', 'Açúcar no Corredor da Beira'], array_column($service->page($sources, 1, 2, $score)['data'], 'title'));
$this->assertSame(['Produção de açúcar'], array_column($service->page($sources, 2, 2, $score)['data'], 'title'));
}
}
+25
View File
@@ -56,4 +56,29 @@ final class SearchTermsTest extends TestCase
$this->assertGreaterThan($search->score('Como investir', 'investir', ['investir']), $search->score('Investir', 'investir', ['investir']));
$this->assertSame(['de'], $search->tokens('de'));
}
public function test_direct_titles_precede_titles_and_descriptions_that_reference_the_query(): void
{
$search = new SearchTerms;
$phrase = 'corredor da beira';
$tokens = $search->tokens($phrase);
$titles = ['Açúcar no Corredor da Beira', 'Corredor da Beira e investimentos', 'Corredor da Beira', 'Produção de açúcar'];
usort($titles, fn ($a, $b) => $search->score($b, $phrase, $tokens) <=> $search->score($a, $phrase, $tokens));
$this->assertSame(['Corredor da Beira', 'Corredor da Beira e investimentos', 'Açúcar no Corredor da Beira', 'Produção de açúcar'], $titles);
$this->assertSame($search->score('Corredor da Beira', $phrase, $tokens), $search->score('CORREDOR DA BEIRA', $phrase, $tokens));
}
public function test_database_relevance_uses_the_same_levels_and_escaped_prefix(): void
{
$connection = new PostgresConnection(fn () => throw new \LogicException('A database must not be opened.'));
$query = $connection->table('records');
$search = new SearchTerms;
$search->orderByRelevance($query, 'title', '100% açúcar', $search->tokens('100% açúcar'), 'id');
$this->assertStringContainsString('THEN 1000', $query->toSql());
$this->assertStringContainsString('THEN 600', $query->toSql());
$this->assertStringContainsString('THEN 400', $query->toSql());
$this->assertSame(['100% acucar', '100!% acucar%', '%100!% acucar%'], array_slice($query->getBindings(), 0, 3));
}
}