Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Aspect Search Result Limit #82

Merged
merged 4 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ $searchResults = (new Search())
->search('john');
```

### Limiting aspect results

It is possible to limit the amount of results returned by each aspect by calling `limitAspectResults` prior to performing the search.

```php
$searchResults = (new Search())
->registerAspect(BlogPostAspect::class)
->limitAspectResults(50)
->search('How To');
```

### Rendering search results

Here's an example on rendering search results:
Expand Down
4 changes: 4 additions & 0 deletions src/ModelSearchAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public function getResults(string $term): Collection

$this->addSearchConditions($query, $term);

if($this->limit) {
$query->limit($this->limit);
}

return $query->get();
}

Expand Down
9 changes: 9 additions & 0 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function getSearchAspects(): array
return $this->aspects;
}

public function limitAspectResults(int $limit) : self
{
collect($this->getSearchAspects())->each(function(SearchAspect $aspect) use ($limit) {
$aspect->limit($limit);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$aspect->limit($limit);
$aspect->setLimit($limit);

});

return $this;
}

public function search(string $query, ?User $user = null): SearchResultCollection
{
return $this->perform($query, $user);
Expand Down
8 changes: 8 additions & 0 deletions src/SearchAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

abstract class SearchAspect
{
/** @var int */
protected $limit;

abstract public function getResults(string $term): Collection;

public function getType(): string
Expand All @@ -23,4 +26,9 @@ public function getType(): string

return Str::plural($type);
}

public function limit($limit) : void

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function limit($limit) : void
public function setLimit($limit) : void

I think it would be a good idea to use setLimit to keep consistency, see:

public function setType(string $type): self

{
$this->limit = $limit;
}
}
39 changes: 39 additions & 0 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,43 @@ public function it_can_register_a_model_search_aspect_with_a_attributes_from_a_c

$this->assertCount(2, $attributes);
}

/** @test */
public function it_can_limit_aspect_results()
{
$search = new Search();

TestModel::createWithName('Android 16');
TestModel::createWithName('Android 17');
TestModel::createWithName('Android 18');
TestModel::createWithName('Android 19');
TestModel::createWithName('Android 20');
TestModel::createWithName('Android 21');

$search->registerModel(TestModel::class, function (ModelSearchAspect $modelSearchAspect) {
$modelSearchAspect
->addSearchableAttribute('name');
});
$results = $search->limitAspectResults(2)->perform('android');
$this->assertCount(2, $results);
}

/** @test */
public function it_can_limit_multiple_aspect_results()
{
$search = new Search();

TestModel::createWithName('alex doe');
TestModel::createWithName('alex doe the second');
TestModel::createWithName('alex doe the third');
TestModel::createWithName('alex doe the fourth');
TestModel::createWithName('jenna');

// This will return 2 as it's results are hard coded
$search->registerAspect(CustomNameSearchAspect::class);
// Our limiter should apply to the second aspect registered here and will make it return only 2
$search->registerModel(TestModel::class, 'name');
$results = $search->limitAspectResults(2)->perform('doe');
$this->assertCount(4, $results);
}
}