Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Don't set "from" if it's zero
Browse files Browse the repository at this point in the history
Otherwise there is no way to disable it. E.g. the Count API doesn't support "from" and "size".
  • Loading branch information
Sam Stenvall committed May 22, 2018
1 parent e2e724b commit 3090b81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ public function buildBody()
$from = ($this->getPage() - 1) * $this->getSize();
}

$body['from'] = $from;
if ($from > 0) {
$body['from'] = $from;
}

return $body;
}
Expand Down
18 changes: 12 additions & 6 deletions tests/Search/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function testToArray()
$this->assertEquals([
'query' => ['match_all' => new \stdClass()],
'size' => 100,
'from' => 0,
], $this->search->buildBody());

$this->search = $this->service->createSearch();
Expand Down Expand Up @@ -142,7 +141,6 @@ public function testToArray()
],
],
'size' => 100,
'from' => 0,
], $this->search->buildBody());

$this->search = $this->service->createSearch();
Expand All @@ -152,7 +150,6 @@ public function testToArray()
'query' => ['match_all' => new \stdClass()],
'sort' => ['_score'],
'size' => 100,
'from' => 0,
], $this->search->buildBody());

$this->search = $this->service->createSearch();
Expand All @@ -170,8 +167,19 @@ public function testToArray()
],
],
'size' => 100,
'from' => 0,
], $this->search->buildBody());

// Make sure "from" is present when it's a positive number
$this->search->setFrom(10);

$this->assertArraySubset([
'from' => 10,
], $this->search->buildBody());

// Make sure "size" is not present when it's set to zero
$this->search->setSize(0);

$this->assertArrayNotHasKey('size', $this->search->buildBody());
}

public function testToArrayWithSource()
Expand All @@ -184,7 +192,6 @@ public function testToArrayWithSource()
$this->assertEquals([
'query' => ['match_all' => new \stdClass()],
'size' => 100,
'from' => 0,
'_source' => [
'id',
'title',
Expand Down Expand Up @@ -227,7 +234,6 @@ public function testAddAggregations()
]
],
'size' => 100,
'from' => 0,
], $this->search->buildBody());
}
}

0 comments on commit 3090b81

Please sign in to comment.