diff --git a/doc/search.md b/doc/search.md index 4757dc8..6936e6c 100644 --- a/doc/search.md +++ b/doc/search.md @@ -3,6 +3,8 @@ This bundle provides a [Search](../src/Search/Search.php) utils that allows you to build complex queries using a fluent interface. +# Building a search + ## Querying an index ```php @@ -18,6 +20,26 @@ $search->in('foo'); $search = Search::within('foo'); ``` +**Note**: Keep in mind that using `Search::within('foo')` is just a shortcut for the first syntax. + +## Specifying the query + +```php +in('foo'); +$search->query('bar'); + +// OR + +$search = Search::on('foo', 'bar'); +``` + +**Note**: Keep in mind that using `Search::on('foo', 'bar')` is just a shortcut for the first syntax. + ## Conditions Conditions (or filters in MeiliSearch) can be hard to handle, @@ -205,3 +227,5 @@ $search->in('foo')->paginate('id', '>', $result->getLastIdentifier(), 20); $search = Search::within('foo')->paginate('id', '>', $result->getLastIdentifier(), 20); ``` + +## Usage diff --git a/src/Search/Search.php b/src/Search/Search.php index c499bab..ebd16ba 100644 --- a/src/Search/Search.php +++ b/src/Search/Search.php @@ -28,6 +28,11 @@ final class Search */ private $index; + /** + * @var string + */ + private $search; + /** * @var int */ @@ -84,6 +89,16 @@ public function in(string $index): self return $this; } + public static function on(string $index, string $query): self + { + $self = new self(); + + $self->in($index); + $self->query($query); + + return $self; + } + public function max(int $limit): self { $this->limit = $limit; @@ -220,10 +235,26 @@ public function paginate(string $field, string $operator, $value, int $limit): s return $this; } + public function getIndex(): string + { + return $this->index; + } + + public function getQuery(): string + { + return $this->query; + } + + public function getLimit(): int + { + return $this->limit; + } + public function getRaw(): array { return [ 'index' => $this->index, + 'search' => $this->search, 'query' => $this->query, 'offset' => $this->offset, 'limit' => $this->limit, diff --git a/src/Search/SearchResult.php b/src/Search/SearchResult.php index e7b7406..57d3d6e 100644 --- a/src/Search/SearchResult.php +++ b/src/Search/SearchResult.php @@ -173,6 +173,14 @@ public function getFacetsDistribution(): array return $this->facetsDistribution; } + /** + * @return mixed + */ + public function getLastIdentifier() + { + return $this->lastIdentifier; + } + /** * {@inheritdoc} */ diff --git a/src/Search/SearchResultInterface.php b/src/Search/SearchResultInterface.php index 09f9ea8..31d5a1e 100644 --- a/src/Search/SearchResultInterface.php +++ b/src/Search/SearchResultInterface.php @@ -49,6 +49,15 @@ public function getProcessingTimeMs(): int; public function getQuery(): string; + public function getExhaustiveFacetsCount(): ?bool; + + public function getFacetsDistribution(): array; + + /** + * @return mixed Can be either a string, integer or null. + */ + public function getLastIdentifier(); + /** * @return array */ diff --git a/tests/Search/SearchResultTest.php b/tests/Search/SearchResultTest.php index 8fa7df4..a5914db 100644 --- a/tests/Search/SearchResultTest.php +++ b/tests/Search/SearchResultTest.php @@ -6,6 +6,7 @@ use MeiliSearchBundle\Search\SearchResult; use PHPUnit\Framework\TestCase; +use Traversable; use function array_key_exists; /** @@ -26,19 +27,30 @@ public function testHitsCanBeFiltered(): void ], ], 0, 0, 1, false, 100, 'foo'); - $result->filter(function (array $hit, int $key): bool { + $result->filter(function (array $hit, int $_): bool { return array_key_exists('title', $hit) && $hit['title'] === 'foo'; }); + static::assertInstanceOf(Traversable::class, $result->getIterator()); + static::assertSame([ + 'id' => 2, + 'title' => 'foobar', + ], $result->getHit(1)); static::assertArrayHasKey('hits', $result->toArray()); static::assertArrayHasKey('offset', $result->toArray()); static::assertArrayHasKey('limit', $result->toArray()); static::assertArrayHasKey('nbHits', $result->toArray()); + static::assertSame(1, $result->getNbHits()); static::assertArrayHasKey('exhaustiveNbHits', $result->toArray()); + static::assertFalse($result->getExhaustiveNbHits()); static::assertArrayHasKey('processingTimeMs', $result->toArray()); + static::assertSame(100, $result->getProcessingTimeMs()); static::assertArrayHasKey('query', $result->toArray()); static::assertArrayHasKey('exhaustiveFacetsCount', $result->toArray()); + static::assertNull($result->getExhaustiveFacetsCount()); static::assertArrayHasKey('facetsDistribution', $result->toArray()); + static::assertEmpty($result->getFacetsDistribution()); static::assertSame(1, $result->count()); + static::assertSame(2, $result->getLastIdentifier()); } } diff --git a/tests/Search/SearchTest.php b/tests/Search/SearchTest.php index f9e565b..cbb9f41 100644 --- a/tests/Search/SearchTest.php +++ b/tests/Search/SearchTest.php @@ -19,9 +19,34 @@ public function testSearchCanBeBuiltOnIndex(): void $search->in('foo'); static::assertSame('foo', $search->getRaw()['index']); + static::assertSame('foo', $search->getIndex()); $search = Search::within('foo'); static::assertSame('foo', $search->getRaw()['index']); + static::assertSame('foo', $search->getIndex()); + } + + public function testSearchCanBeBuiltOnSpecificQuery(): void + { + $search = new Search(); + + $search->in('foo')->query('bar'); + static::assertSame('foo', $search->getRaw()['index']); + static::assertSame('bar', $search->getRaw()['query']); + static::assertSame('foo', $search->getIndex()); + static::assertSame('bar', $search->getQuery()); + + $search = Search::within('foo')->query('bar'); + static::assertSame('foo', $search->getRaw()['index']); + static::assertSame('bar', $search->getRaw()['query']); + static::assertSame('foo', $search->getIndex()); + static::assertSame('bar', $search->getQuery()); + + $search = Search::on('foo', 'bar'); + static::assertSame('foo', $search->getRaw()['index']); + static::assertSame('bar', $search->getRaw()['query']); + static::assertSame('foo', $search->getIndex()); + static::assertSame('bar', $search->getQuery()); } public function testSearchCanTargetSpecificIndexAndLimitResults(): void @@ -31,10 +56,12 @@ public function testSearchCanTargetSpecificIndexAndLimitResults(): void static::assertSame('foo', $search->getRaw()['index']); static::assertSame(10, $search->getRaw()['limit']); + static::assertSame(10, $search->getLimit()); $search = Search::within('foo')->max(10); static::assertSame('foo', $search->getRaw()['index']); static::assertSame(10, $search->getRaw()['limit']); + static::assertSame(10, $search->getLimit()); } public function testSearchCanTargetSpecificIndexAndOffset(): void @@ -50,17 +77,6 @@ public function testSearchCanTargetSpecificIndexAndOffset(): void static::assertSame(10, $search->getRaw()['offset']); } - public function testSearchCanSpecifyAQuery(): void - { - $search = new Search(); - $search->in('foo')->query('foo'); - - static::assertSame('foo', $search->getRaw()['query']); - - $search = Search::within('foo')->query('foo'); - static::assertSame('foo', $search->getRaw()['query']); - } - public function testSearchCannotBeBuiltWithInvalidWhereCondition(): void { $search = new Search();