From 96770c7a133f54184d8cedec29c10c23938a6e18 Mon Sep 17 00:00:00 2001 From: edalzell Date: Sun, 25 Oct 2026 16:04:21 -0700 Subject: [PATCH] Look up the Comb index once per query builder --- src/Search/Comb/Query.php | 4 +++- tests/Search/Comb/QueryTest.php | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/Search/Comb/QueryTest.php diff --git a/src/Search/Comb/Query.php b/src/Search/Comb/Query.php index dcad27df30f..419816ccf6e 100644 --- a/src/Search/Comb/Query.php +++ b/src/Search/Comb/Query.php @@ -6,8 +6,10 @@ class Query extends QueryBuilder { + protected $lookups = []; + public function getSearchResults($query) { - return $this->index->lookup($this->query); + return $this->lookups[$this->query] ??= $this->index->lookup($this->query); } } diff --git a/tests/Search/Comb/QueryTest.php b/tests/Search/Comb/QueryTest.php new file mode 100644 index 00000000000..62f0e30a24a --- /dev/null +++ b/tests/Search/Comb/QueryTest.php @@ -0,0 +1,41 @@ +shouldReceive('lookup')->with('foo')->once()->andReturn(collect([ + ['reference' => 'a', 'search_score' => 2], + ['reference' => 'b', 'search_score' => 1], + ])); + + $paginator = (new Query($index))->query('foo')->withoutData()->paginate(1); + + $this->assertEquals(2, $paginator->total()); + $this->assertEquals(['a'], $paginator->getCollection()->map->reference->all()); + } + + #[Test] + public function changing_the_query_looks_up_the_index_again() + { + $index = Mockery::mock(Index::class); + $index->shouldReceive('lookup')->with('foo')->once()->andReturn(collect([['reference' => 'a']])); + $index->shouldReceive('lookup')->with('bar')->once()->andReturn(collect([['reference' => 'b']])); + + $builder = (new Query($index))->withoutData(); + + $this->assertEquals(['a'], $builder->query('foo')->get()->map->reference->all()); + $this->assertEquals(['b'], $builder->query('bar')->get()->map->reference->all()); + $this->assertEquals(['a'], $builder->query('foo')->get()->map->reference->all()); + } +}