Skip to content

Commit 4838b69

Browse files
authored
Merge pull request #60 from ensi-platform/v7-cloudtech-3
V7 async search
2 parents bd8f6ab + c034dfd commit 4838b69

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

src/Aggregating/AggregationsQuery.php

Lines changed: 23 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,31 @@ 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+
51+
return $this->responseToResults($response);
52+
}
3953

4054
return $this->aggregations->parseResults($response['aggregations'] ?? []);
4155
}
4256

43-
protected function execute(): array
57+
protected function execute(bool $async = false): array|FutureArray
4458
{
4559
$dsl = [
4660
'size' => 0,
@@ -49,6 +63,11 @@ protected function execute(): array
4963
'aggs' => $this->aggregations->toDSL(),
5064
];
5165

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

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: 26 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,32 @@ 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);
6474

75+
return $this->responseToPage($size, $offset, $response);
76+
}
77+
}
78+
79+
protected function responseToPage(int $size, int $offset, array $response): Page
80+
{
6581
return new Page(
6682
$size,
67-
$hits,
83+
$this->parseHits($response),
6884
aggs: $this->aggregations?->parseResults($response['aggregations'] ?? []),
6985
offset: $offset,
7086
total: data_get($response, 'hits.total.value', 0)
@@ -122,8 +138,9 @@ protected function execute(
122138
?int $from = null,
123139
bool $totals = false,
124140
bool $source = true,
125-
?Cursor $cursor = null
126-
): array {
141+
?Cursor $cursor = null,
142+
bool $async = false,
143+
): array|FutureArray {
127144
$dsl = [
128145
'size' => $size,
129146
'from' => $from,
@@ -150,7 +167,9 @@ protected function execute(
150167
$dsl['search_after'] = $cursor->toDSL();
151168
}
152169

153-
return $this->index->search(array_filter($dsl));
170+
$dsl = array_filter($dsl);
171+
172+
return $async ? $this->index->searchAsync($dsl) : $this->index->search($dsl);
154173
}
155174

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

0 commit comments

Comments
 (0)