Skip to content

Commit e028bc5

Browse files
authored
Merge pull request #70 from ensi-platform/v8-cloudtech-5
CLOUDTECH-5
2 parents 9664608 + 2539c86 commit e028bc5

File tree

13 files changed

+298
-151
lines changed

13 files changed

+298
-151
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,69 @@ Hosts should be comma seperated string of hosts with protocol prefix and port su
305305
ELASTICSEARCH_SSL_VERIFICATION=true,
306306
```
307307

308+
## Async Usage
309+
310+
All methods can return a `Promise`.
311+
To enable this, you will need to add `http_async_client` to your config and then execute `ElasticSearch::getClient()->setAsync(true).`
312+
To disable: `ElasticQuery::getClient()->setAsync(false)`.
313+
314+
For example:
315+
316+
laravel-elastic-query.php:
317+
318+
```php
319+
return [
320+
'connection' => [
321+
322+
// ..
323+
324+
'http_async_client' => [HttpClientOptionsBuilder::class, 'getAsyncClient'],
325+
],
326+
];
327+
```
328+
329+
HttpClientOptionsBuilder:
330+
331+
```php
332+
use Http\Adapter\Guzzle7\Client as GuzzleAdapter;
333+
use Http\Client\HttpAsyncClient;
334+
335+
class HttpClientOptionsBuilder
336+
{
337+
public static function getAsyncClient(): HttpAsyncClient
338+
{
339+
return GuzzleAdapter::createWithConfig([]);
340+
}
341+
}
342+
```
343+
344+
Action:
345+
346+
```php
347+
348+
use Ensi\LaravelElasticQuery\ElasticQuery;
349+
350+
ElasticQuery::getClient()->setAsync(true);
351+
352+
// With async
353+
$promises = [
354+
'key1' => FirstIndex::query()->get(),
355+
'key2' => FirstIndex::suggest()->paginate(/* ... */),
356+
];
357+
358+
$results = [];
359+
foreach ($promises as $key => $promise) {
360+
$results[$key] = $promise->wait();
361+
}
362+
363+
$firstResponse = $results['key1'];
364+
365+
ElasticQuery::getClient()->setAsync(false);
366+
367+
// Without async
368+
$firstResponse = FirstIndex::query()->get()
369+
```
370+
308371
## Elasticsearch 7 and 8 support.
309372

310373
Due to the incompatibility of clients for Elasticsearch 7 and 8, separate releases will be created for these versions.

config/laravel-elastic-query.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414

1515
'http_client' => null, // class implementing the \Psr\Http\Client\ClientInterface
1616
'http_client_options' => null, // for call_user_func_array
17+
18+
'http_client_logger' => null, // for call_user_func_array, return class implementing the \Psr\Log\LoggerInterface
19+
20+
'http_async_client' => null, // for call_user_func_array, return class implementing the \Http\Client\HttpAsyncClient
1721
],
1822
];

src/Aggregating/AggregationsQuery.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Ensi\LaravelElasticQuery\Contracts\AggregationsBuilder;
88
use Ensi\LaravelElasticQuery\Contracts\SearchIndex;
99
use Ensi\LaravelElasticQuery\Filtering\BoolQueryBuilder;
10+
use Ensi\LaravelElasticQuery\Response;
11+
use Http\Promise\Promise;
1012
use Illuminate\Support\Collection;
1113

1214
class AggregationsQuery implements AggregationsBuilder
@@ -29,18 +31,21 @@ public function composite(Closure $callback): static
2931
return $this;
3032
}
3133

32-
public function get(): Collection
34+
public function get(): Collection|Promise
3335
{
3436
if ($this->aggregations->isEmpty()) {
3537
return new Collection();
3638
}
3739

38-
$response = $this->execute();
39-
40-
return $this->aggregations->parseResults($response['aggregations'] ?? []);
40+
return Response::fn(
41+
$this->execute(),
42+
function (array $response) {
43+
return $this->aggregations->parseResults($response['aggregations'] ?? []);
44+
}
45+
);
4146
}
4247

43-
protected function execute(): array
48+
protected function execute(): array|Promise
4449
{
4550
$dsl = [
4651
'size' => 0,

src/Concerns/InteractsWithIndex.php

Lines changed: 14 additions & 13 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 Http\Promise\Promise;
1112

1213
trait InteractsWithIndex
1314
{
@@ -28,50 +29,50 @@ protected function settings(): array
2829
/**
2930
* @see SearchIndex::search()
3031
*/
31-
public function search(array $dsl, ?string $searchType = null): array
32+
public function search(array $dsl, ?string $searchType = null): array|Promise
3233
{
3334
return $this->resolveClient()->search($this->indexName(), $dsl, $searchType);
3435
}
3536

3637
/**
3738
* @see SearchIndex::search()
3839
*/
39-
public function deleteByQuery(array $dsl): array
40+
public function deleteByQuery(array $dsl): array|Promise
4041
{
4142
return $this->resolveClient()->deleteByQuery($this->indexName(), $dsl);
4243
}
4344

44-
public function isCreated(): bool
45+
public function isCreated(): bool|Promise
4546
{
4647
return $this->resolveClient()->indicesExists($this->indexName());
4748
}
4849

49-
public function create(): void
50+
public function create(): ?Promise
5051
{
51-
$this->resolveClient()->indicesCreate($this->indexName(), $this->settings());
52+
return $this->resolveClient()->indicesCreate($this->indexName(), $this->settings());
5253
}
5354

54-
public function bulk(array $body): array
55+
public function bulk(array $body): array|Promise
5556
{
5657
return $this->resolveClient()->bulk($this->indexName(), $body);
5758
}
5859

59-
public function get(int|string $id): array
60+
public function get(int|string $id): array|Promise
6061
{
6162
return $this->resolveClient()->get($this->indexName(), $id);
6263
}
6364

64-
public function documentDelete(int|string $id): array
65+
public function documentDelete(int|string $id): array|Promise
6566
{
6667
return $this->resolveClient()->documentDelete($this->indexName(), $id);
6768
}
6869

69-
public function catIndices(string $indexName, ?array $getFields = null): array
70+
public function catIndices(string $indexName, ?array $getFields = null): array|Promise
7071
{
7172
return $this->resolveClient()->catIndices($indexName, $getFields);
7273
}
7374

74-
public function indicesInfo(array $columns = ['i'], array $sort = [], ?string $health = null): array
75+
public function indicesInfo(array $columns = ['i'], array $sort = [], ?string $health = null): array|Promise
7576
{
7677
return $this->resolveClient()->indicesInfo(
7778
indices: [$this->indexName()],
@@ -81,17 +82,17 @@ public function indicesInfo(array $columns = ['i'], array $sort = [], ?string $h
8182
);
8283
}
8384

84-
public function indicesDelete(string $index): array
85+
public function indicesDelete(string $index): array|Promise
8586
{
8687
return $this->resolveClient()->indicesDelete($index);
8788
}
8889

89-
public function indicesRefresh(): array
90+
public function indicesRefresh(): array|Promise
9091
{
9192
return $this->resolveClient()->indicesRefresh($this->indexName());
9293
}
9394

94-
public function indicesReloadSearchAnalyzers(): array
95+
public function indicesReloadSearchAnalyzers(): array|Promise
9596
{
9697
return $this->resolveClient()->indicesReloadSearchAnalyzers($this->indexName());
9798
}

src/Contracts/SearchIndex.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ensi\LaravelElasticQuery\Contracts;
44

5+
use Http\Promise\Promise;
6+
57
interface SearchIndex
68
{
79
/**
@@ -16,15 +18,15 @@ public function tiebreaker(): string;
1618
*
1719
* @param array $dsl
1820
* @param string|null $searchType
19-
* @return array
21+
* @return array|Promise
2022
*/
21-
public function search(array $dsl, ?string $searchType = null): array;
23+
public function search(array $dsl, ?string $searchType = null): array|Promise;
2224

2325
/**
2426
* Perform delete by query.
2527
*
2628
* @param array $dsl
27-
* @return array
29+
* @return array|Promise
2830
*/
29-
public function deleteByQuery(array $dsl): array;
31+
public function deleteByQuery(array $dsl): array|Promise;
3032
}

0 commit comments

Comments
 (0)