Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 067d0b1

Browse files
committed
Added builder offset method.
1 parent 04817ac commit 067d0b1

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,17 @@ Now you can [index](https://laravel.com/docs/5.5/scout#indexing) and [search](ht
195195
Basic search usage example:
196196

197197
```php
198-
App\MyModel::search('phone')
198+
// set query string
199+
App\MyModel::search('phone')
200+
// filter
199201
->where('color', 'red')
202+
// sort
200203
->orderBy('price', 'asc')
204+
// set offset
205+
->from(0)
206+
// set limit
201207
->take(10)
208+
// get results
202209
->get();
203210
```
204211

src/Builders/FilterBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class FilterBuilder extends Builder
1313

1414
public $with;
1515

16+
public $offset;
17+
1618
public function __construct($model, $callback = null)
1719
{
1820
$this->model = $model;
@@ -196,4 +198,11 @@ public function with($relations)
196198

197199
return $this;
198200
}
201+
202+
public function from($offset)
203+
{
204+
$this->offset = $offset;
205+
206+
return $this;
207+
}
199208
}

src/ElasticEngine.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ protected function buildSearchQueryPayload(Builder $builder, $queryPayload, arra
8080
->setIfNotEmpty('body.explain', $options['explain'] ?? null)
8181
->setIfNotEmpty('body.profile', $options['profile'] ?? null);
8282

83-
if ($size = isset($options['limit']) ? $options['limit'] : $builder->limit) {
84-
$payload->set('body.size', $size);
83+
if (isset($builder->offset)) {
84+
$payload->set('body.from', $builder->offset);
85+
}
8586

86-
if (isset($options['page'])) {
87-
$payload->set('body.from', ($options['page'] - 1) * $size);
88-
}
87+
if (isset($builder->limit)) {
88+
$payload->set('body.size', $builder->limit);
8989
}
9090

9191
return $payload->get();
@@ -163,10 +163,11 @@ public function search(Builder $builder)
163163

164164
public function paginate(Builder $builder, $perPage, $page)
165165
{
166-
return $this->performSearch($builder, [
167-
'limit' => $perPage,
168-
'page' => $page
169-
]);
166+
$builder
167+
->from(($page - 1) * $perPage)
168+
->take($perPage);
169+
170+
return $this->performSearch($builder);
170171
}
171172

172173
public function explain(Builder $builder)

tests/ElasticEngineTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,39 @@ public function test_if_the_search_method_with_specified_limit_builds_correct_pa
189189
$this->addToAssertionCount(1);
190190
}
191191

192+
public function test_if_the_search_method_with_specified_offset_builds_correct_payload()
193+
{
194+
$this
195+
->mockClient()
196+
->shouldReceive('search')
197+
->with([
198+
'index' => 'test_index',
199+
'type' => 'test_table',
200+
'body' => [
201+
'query' => [
202+
'bool' => [
203+
'must' => [
204+
'query_string' => [
205+
'query' => 'test query'
206+
]
207+
]
208+
]
209+
],
210+
'from' => 10
211+
]
212+
]);
213+
214+
$model = $this->mockModel();
215+
216+
$builder = (new SearchBuilder($model, 'test query'))->from(10);
217+
218+
$this
219+
->buildEngine()
220+
->search($builder);
221+
222+
$this->addToAssertionCount(1);
223+
}
224+
192225
public function test_if_the_search_method_with_specified_order_builds_correct_payload()
193226
{
194227
$this->mockClient()

0 commit comments

Comments
 (0)