Skip to content

Commit 54dd201

Browse files
committed
_source include and exclude
1 parent b5d1547 commit 54dd201

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/Search/SearchQuery.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SearchQuery implements SortableQuery
2525
protected ?int $size = null;
2626
protected ?int $from = null;
2727
protected array $fields = [];
28+
protected array $include = [];
29+
protected array $exclude = [];
2830

2931
public function __construct(protected SearchIndex $index)
3032
{
@@ -117,7 +119,7 @@ protected function execute(
117119
'from' => $from,
118120
'query' => $this->boolQuery->toDSL(),
119121
'track_total_hits' => $totals,
120-
'_source' => $source && !$this->fields,
122+
'_source' => $this->sourceToDSL($source),
121123
'fields' => $source && $this->fields ? $this->fields : null,
122124
];
123125

@@ -133,6 +135,16 @@ protected function execute(
133135
return $this->index->search(array_filter($dsl));
134136
}
135137

138+
protected function sourceToDSL(bool $source): array | bool
139+
{
140+
return $source && !$this->fields ?
141+
[
142+
'include' => $this->include,
143+
'exclude' => $this->exclude,
144+
] :
145+
false;
146+
}
147+
136148
protected function parseHits(array $response): Collection
137149
{
138150
return collect(data_get($response, 'hits.hits') ?? []);
@@ -165,6 +177,24 @@ public function take(int $count): static
165177
return $this;
166178
}
167179

180+
public function select(array $include): static
181+
{
182+
array_map(Assert::stringNotEmpty(...), $include);
183+
184+
$this->include = $include;
185+
186+
return $this;
187+
}
188+
189+
public function exclude(array $exclude): static
190+
{
191+
array_map(Assert::stringNotEmpty(...), $exclude);
192+
193+
$this->exclude = $exclude;
194+
195+
return $this;
196+
}
197+
168198
public function skip(int $count): static
169199
{
170200
Assert::greaterThanEq($count, 0);

0 commit comments

Comments
 (0)