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()); + } +}