Skip to content

Commit ab90810

Browse files
committed
CLOUDTECH-3
1 parent bd8f6ab commit ab90810

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

src/Aggregating/AggregationsQuery.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ensi\LaravelElasticQuery\Contracts\AggregationsBuilder;
88
use Ensi\LaravelElasticQuery\Contracts\SearchIndex;
99
use Ensi\LaravelElasticQuery\Filtering\BoolQueryBuilder;
10+
use GuzzleHttp\Ring\Future\FutureArray;
1011
use Illuminate\Support\Collection;
1112

1213
class AggregationsQuery implements AggregationsBuilder
@@ -29,18 +30,30 @@ public function composite(Closure $callback): static
2930
return $this;
3031
}
3132

32-
public function get(): Collection
33+
public function get(?callable $async = null): Collection|FutureArray
3334
{
3435
if ($this->aggregations->isEmpty()) {
3536
return new Collection();
3637
}
3738

38-
$response = $this->execute();
39+
if ($async) {
40+
/** @var FutureArray $promise */
41+
$promise = $this->execute(async: true);
42+
43+
$promise->then(function (array $response) use ($async) {
44+
$async($this->responseToResults($response));
45+
});
46+
47+
return $promise;
48+
} else {
49+
$response = $this->execute(async: false);
50+
return $this->responseToResults($response);
51+
}
3952

4053
return $this->aggregations->parseResults($response['aggregations'] ?? []);
4154
}
4255

43-
protected function execute(): array
56+
protected function execute(bool $async = false): array|FutureArray
4457
{
4558
$dsl = [
4659
'size' => 0,
@@ -49,6 +62,11 @@ protected function execute(): array
4962
'aggs' => $this->aggregations->toDSL(),
5063
];
5164

52-
return $this->index->search($dsl);
65+
return $async ? $this->index->searchAsync($dsl) : $this->index->search($dsl);
66+
}
67+
68+
protected function responseToResults($response): Collection
69+
{
70+
return $this->aggregations->parseResults($response['aggregations'] ?? []);
5371
}
5472
}

src/Concerns/InteractsWithIndex.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ensi\LaravelElasticQuery\Search\SearchQuery;
99
use Ensi\LaravelElasticQuery\Suggesting\SuggestQuery;
1010
use Exception;
11+
use GuzzleHttp\Ring\Future\FutureArray;
1112

1213
trait InteractsWithIndex
1314
{
@@ -33,6 +34,14 @@ public function search(array $dsl): array
3334
return $this->resolveClient()->search($this->indexName(), $dsl);
3435
}
3536

37+
/**
38+
* @see SearchIndex::search()
39+
*/
40+
public function searchAsync(array $dsl): FutureArray
41+
{
42+
return $this->resolveClient()->searchAsync($this->indexName(), $dsl);
43+
}
44+
3645
/**
3746
* @see SearchIndex::search()
3847
*/

src/ElasticClient.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Elasticsearch\ClientBuilder;
77
use Ensi\LaravelElasticQuery\Debug\QueryLog;
88
use Ensi\LaravelElasticQuery\Debug\QueryLogRecord;
9+
use GuzzleHttp\Ring\Future\FutureArray;
910
use Illuminate\Support\Collection;
1011

1112
class ElasticClient
@@ -26,6 +27,16 @@ public function search(string $indexName, array $dsl): array
2627
]);
2728
}
2829

30+
public function searchAsync(string $indexName, array $dsl): FutureArray
31+
{
32+
$this->queryLog?->log($indexName, $dsl);
33+
34+
return $this->client->search($this->paramsAsync([
35+
'index' => $indexName,
36+
'body' => $dsl,
37+
]));
38+
}
39+
2940
public function deleteByQuery(string $indexName, array $dsl): array
3041
{
3142
$this->queryLog?->log($indexName, $dsl);
@@ -133,4 +144,11 @@ public static function fromConfig(array $config): static
133144

134145
return new static($builder->build());
135146
}
147+
148+
protected function paramsAsync(array $params): array
149+
{
150+
return array_merge_recursive($params, [
151+
'client' => ['future' => 'lazy']
152+
]);
153+
}
136154
}

src/Search/SearchQuery.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Ensi\LaravelElasticQuery\Search\Collapsing\Collapse;
1818
use Ensi\LaravelElasticQuery\Search\Sorting\SortBuilder;
1919
use Ensi\LaravelElasticQuery\Search\Sorting\SortCollection;
20+
use GuzzleHttp\Ring\Future\FutureArray;
2021
use Illuminate\Support\Collection;
2122
use InvalidArgumentException;
2223
use Webmozart\Assert\Assert;
@@ -54,17 +55,31 @@ public function get(): Collection
5455
return $this->parseHits($response);
5556
}
5657

57-
public function paginate(int $size, int $offset = 0): Page
58+
public function paginate(int $size, int $offset = 0, ?callable $async = null): Page|FutureArray
5859
{
5960
Assert::greaterThan($size, 0);
6061
Assert::greaterThanEq($offset, 0);
6162

62-
$response = $this->execute(size: $size, from: $offset, totals: true);
63-
$hits = $this->parseHits($response);
63+
if ($async) {
64+
/** @var FutureArray $promise */
65+
$promise = $this->execute(size: $size, from: $offset, totals: true, async: true);
66+
67+
$promise->then(function (array $response) use ($size, $offset, $async) {
68+
$async($this->responseToPage($size, $offset, $response));
69+
});
70+
71+
return $promise;
72+
} else {
73+
$response = $this->execute(size: $size, from: $offset, totals: true, async: false);
74+
return $this->responseToPage($size, $offset, $response);
75+
}
76+
}
6477

78+
protected function responseToPage(int $size, int $offset, array $response): Page
79+
{
6580
return new Page(
6681
$size,
67-
$hits,
82+
$this->parseHits($response),
6883
aggs: $this->aggregations?->parseResults($response['aggregations'] ?? []),
6984
offset: $offset,
7085
total: data_get($response, 'hits.total.value', 0)
@@ -122,8 +137,9 @@ protected function execute(
122137
?int $from = null,
123138
bool $totals = false,
124139
bool $source = true,
125-
?Cursor $cursor = null
126-
): array {
140+
?Cursor $cursor = null,
141+
bool $async = false,
142+
): array|FutureArray {
127143
$dsl = [
128144
'size' => $size,
129145
'from' => $from,
@@ -150,7 +166,9 @@ protected function execute(
150166
$dsl['search_after'] = $cursor->toDSL();
151167
}
152168

153-
return $this->index->search(array_filter($dsl));
169+
$dsl = array_filter($dsl);
170+
171+
return $async ? $this->index->searchAsync($dsl) : $this->index->search($dsl);
154172
}
155173

156174
protected function sourceToDSL(bool $source): array | bool

0 commit comments

Comments
 (0)