Skip to content

Commit

Permalink
refactor(search): progress on Search
Browse files Browse the repository at this point in the history
The documentation must be improved
  • Loading branch information
Guikingone committed Nov 1, 2020
1 parent 1bfbd07 commit dff561d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 12 deletions.
24 changes: 24 additions & 0 deletions doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<?php

use MeiliSearchBundle\Search\Search;

$search = new Search();
$search->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,
Expand Down Expand Up @@ -205,3 +227,5 @@ $search->in('foo')->paginate('id', '>', $result->getLastIdentifier(), 20);

$search = Search::within('foo')->paginate('id', '>', $result->getLastIdentifier(), 20);
```

## Usage
31 changes: 31 additions & 0 deletions src/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ final class Search
*/
private $index;

/**
* @var string
*/
private $search;

/**
* @var int
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public function getFacetsDistribution(): array
return $this->facetsDistribution;
}

/**
* @return mixed
*/
public function getLastIdentifier()
{
return $this->lastIdentifier;
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Search/SearchResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>
*/
Expand Down
14 changes: 13 additions & 1 deletion tests/Search/SearchResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MeiliSearchBundle\Search\SearchResult;
use PHPUnit\Framework\TestCase;
use Traversable;
use function array_key_exists;

/**
Expand All @@ -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());
}
}
38 changes: 27 additions & 11 deletions tests/Search/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit dff561d

Please sign in to comment.