Skip to content

Commit b738310

Browse files
authored
Merge pull request #22 from xsuchy09/feature/direct-pit-and-search-after-support
Direct support for pit and search_after in QueryBuilder
2 parents afe8cb7 + 71d919e commit b738310

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/QueryBuilder.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class QueryBuilder
2121

2222
private ?int $size = null;
2323

24+
private string|array|null $pit = null;
25+
26+
private ?array $searchAfter = null;
27+
2428
private ?QueryInterface $query = null;
2529

2630
private ?array $fields = null;
@@ -59,6 +63,24 @@ public function setSize(int $size): self
5963
return $this;
6064
}
6165

66+
public function setPit(string|array|null $pit): self
67+
{
68+
if (is_array($pit) && !isset($pit['id'])) {
69+
throw new \InvalidArgumentException('Parameter "pit" is not valid. Value with key "id" is not set.');
70+
}
71+
72+
$this->pit = $pit;
73+
74+
return $this;
75+
}
76+
77+
public function setSearchAfter(?array $searchAfter): self
78+
{
79+
$this->searchAfter = $searchAfter;
80+
81+
return $this;
82+
}
83+
6284
public function setQuery(QueryInterface $query): self
6385
{
6486
$this->query = $query;
@@ -113,6 +135,18 @@ public function build(): array
113135
$query['size'] = $this->size;
114136
}
115137

138+
if (null !== $this->pit) {
139+
if (is_string($this->pit)) {
140+
$query['pit'] = ['id' => $this->pit];
141+
} else {
142+
$query['pit'] = $this->pit;
143+
}
144+
}
145+
146+
if (null !== $this->searchAfter) {
147+
$query['search_after'] = $this->searchAfter;
148+
}
149+
116150
if (null !== $this->source) {
117151
$query['_source'] = $this->source;
118152
}
@@ -155,6 +189,16 @@ public function getSize(): ?int
155189
return $this->size;
156190
}
157191

192+
public function getPit(): string|array|null
193+
{
194+
return $this->pit;
195+
}
196+
197+
public function getSearchAfter(): ?array
198+
{
199+
return $this->searchAfter;
200+
}
201+
158202
public function getQuery(): ?QueryInterface
159203
{
160204
return $this->query;

tests/QueryBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public function testItSetTheFrom(): void
6464
$this->assertEquals(50, $query['from']);
6565
}
6666

67+
public function testItSetThePitAsString(): void
68+
{
69+
$queryBuilder = new QueryBuilder();
70+
71+
$queryBuilder->setPit('pit-as-string');
72+
73+
$query = $queryBuilder->build();
74+
75+
$this->assertEquals(['id' => 'pit-as-string'], $query['pit']);
76+
}
77+
78+
public function testItSetThePitAsArray(): void
79+
{
80+
$queryBuilder = new QueryBuilder();
81+
82+
$queryBuilder->setPit(['id' => 'pit-as-array', 'keep_alive' => '1m']);
83+
84+
$query = $queryBuilder->build();
85+
86+
$this->assertEquals(['id' => 'pit-as-array', 'keep_alive' => '1m'], $query['pit']);
87+
}
88+
6789
public function testItAllowToSort(): void
6890
{
6991
$queryBuilder = new QueryBuilder();

0 commit comments

Comments
 (0)