Skip to content

Commit 66cadab

Browse files
authored
Merge pull request #10 from olekjs/dev-release-v1.8.0
Add ability to select specific fields
2 parents 65fd385 + bbc178f commit 66cadab

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Builder/Builder.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Builder implements BuilderInterface
4747

4848
private array $sort;
4949

50+
private array $select;
51+
5052
private array $body = [];
5153

5254
public function __construct(private readonly ClientInterface $client)
@@ -248,6 +250,17 @@ public function rawSort(array $sort): self
248250
return $this;
249251
}
250252

253+
public function select(mixed $fields): self
254+
{
255+
$fields = is_array($fields) ? $fields : func_get_args();
256+
257+
foreach ($fields as $field) {
258+
$this->select[] = $field;
259+
}
260+
261+
return $this;
262+
}
263+
251264
/**
252265
* @throws IndexNotFoundResponseException
253266
* @throws FindResponseException
@@ -394,12 +407,21 @@ public function getSort(): array
394407
return $this->sort;
395408
}
396409

397-
private function performSearchBody(): void
410+
public function getSelect(): array
411+
{
412+
return $this->select;
413+
}
414+
415+
public function performSearchBody(): void
398416
{
399417
if (isset($this->query)) {
400418
$this->body['query'] = $this->query;
401419
}
402420

421+
if (isset($this->select)) {
422+
$this->body['_source'] = $this->select;
423+
}
424+
403425
if (empty($this->body)) {
404426
$this->body['query'] = [
405427
'match_all' => (object)[]

src/Contracts/BuilderInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function rawQuery(array $query): self;
6868

6969
public function rawSort(array $sort): self;
7070

71+
public function select(mixed $fields): self;
72+
7173
/**
7274
* @throws IndexNotFoundResponseException
7375
* @throws FindResponseException
@@ -135,4 +137,8 @@ public function getBody(): array;
135137
public function getQuery(): array;
136138

137139
public function getSort(): array;
140+
141+
public function getSelect(): array;
142+
143+
public function performSearchBody(): void;
138144
}

tests/Unit/BuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,20 @@ public function testDeleteMethod(): void
543543

544544
$this->assertInstanceOf(IndexResponseDto::class, $result);
545545
}
546+
547+
public function testSelectMethod(): void
548+
{
549+
$expected = ['test', 'test1', 'test2', 'test3', 'test4'];
550+
551+
$builder = Builder::query()
552+
->select('test')
553+
->select(['test1', 'test2'])
554+
->select('test3', 'test4');
555+
556+
$this->assertSame($expected, $builder->getSelect());
557+
558+
$builder->performSearchBody();
559+
560+
$this->assertSame(['_source' => $expected], $builder->getBody());
561+
}
546562
}

0 commit comments

Comments
 (0)