Skip to content

Commit

Permalink
Continue on error (#5)
Browse files Browse the repository at this point in the history
* Continue on error

* Continue on error

* Continue on error

* Continue on error
  • Loading branch information
zingimmick authored Aug 12, 2021
1 parent 2f216c5 commit 4e59d34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 81 deletions.
42 changes: 11 additions & 31 deletions src/Engines/OpenSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Zing\LaravelScout\OpenSearch\Engines;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Laravel\Scout\Builder;
Expand Down Expand Up @@ -89,15 +88,7 @@ public function update($models): void
foreach ($objects as $object) {
$this->document->add($object);
}
$result = $this->document->commit(...explode('.', $models->first()->searchableAs()));
$searchResult = json_decode($result->result, true);
if ($searchResult['status'] !== 'OK') {
$message = collect(Arr::get($searchResult, 'errors', []))->map(function ($error) {
return "code: {$error['code']}, message: {$error['message']}";
})->implode('; ');

throw new \Exception($message ?: 'errors not return');
}
$this->document->commit(...explode('.', $models->first()->searchableAs()));
}
}

Expand All @@ -115,15 +106,7 @@ public function delete($models): void
foreach ($objects as $object) {
$this->document->remove($object);
}
$result = $this->document->commit(...explode('.', $models->first()->searchableAs()));
$searchResult = json_decode($result->result, true);
if ($searchResult['status'] !== 'OK') {
$message = collect(Arr::get($searchResult, 'errors', []))->map(function ($error) {
return "code: {$error['code']}, message: {$error['message']}";
})->implode('; ');

throw new \Exception($message ?: 'errors not return');
}
$this->document->commit(...explode('.', $models->first()->searchableAs()));
}

public function search(Builder $builder)
Expand Down Expand Up @@ -181,22 +164,16 @@ protected function performSearch(Builder $builder, array $options = [])
}
$result = $this->search->execute($searchParamsBuilder->build());
$searchResult = json_decode($result->result, true);
if ($searchResult === null) {
throw new \Exception('Nothing return');
}
if ($searchResult['status'] !== 'OK') {
$message = collect(Arr::get($searchResult, 'errors', []))->map(function ($error) {
return "code: {$error['code']}, message: {$error['message']}";
})->implode('; ');

throw new \Exception($message ?: 'errors not return');
}

return $searchResult['result'];
return $searchResult['result'] ?? null;
}

public function mapIds($results)
{
if ($results === null) {
return collect();
}

return collect($results['items'])->pluck('fields.id')->values();
}

Expand Down Expand Up @@ -235,6 +212,9 @@ public function map(Builder $builder, $results, $model)
*/
public function lazyMap(Builder $builder, $results, $model)
{
if ($results === null) {
return LazyCollection::make($model->newCollection());
}
if (
(is_array($results['items']) || $results['items'] instanceof \Countable ? count(
$results['items']
Expand All @@ -257,7 +237,7 @@ public function lazyMap(Builder $builder, $results, $model)

public function getTotalCount($results)
{
return $results['total'];
return $results['total'] ?? 0;
}

public function flush($model): void
Expand Down
52 changes: 2 additions & 50 deletions tests/OpenSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,6 @@ public function testUpdateWithEmpty(): void
$this->engine->update(Collection::make([$model]));
}

public function testUpdateFailed(): void
{
$this->expectException(\Throwable::class);
$this->client->shouldReceive('post')
->withArgs(['/apps/app/table/actions/bulk', new Mockery\Matcher\AnyArgs()])
->times(1)
->andReturn(new OpenSearchResult([
'result' => '{
"errors": [
{
"code": 2001,
"message": "待查应用不存在.待查应用不存在。",
"params": {
"friendly_message": "待查应用不存在。"
}
}
],
"request_id": "150116732819940316116461",
"status": "FAIL"
}',
]));
$this->engine->update(Collection::make([new SearchableModel()]));
}

public function testDelete(): void
{
$this->client->shouldReceive('post')
Expand All @@ -123,30 +99,6 @@ public function testDelete(): void
$this->engine->delete(Collection::make([new SearchableModel()]));
}

public function testDeleteFailed(): void
{
$this->expectException(\Throwable::class);
$this->client->shouldReceive('post')
->withArgs(['/apps/app/table/actions/bulk', new Mockery\Matcher\AnyArgs()])
->times(1)
->andReturn(new OpenSearchResult([
'result' => '{
"errors": [
{
"code": 2001,
"message": "待查应用不存在.待查应用不存在。",
"params": {
"friendly_message": "待查应用不存在。"
}
}
],
"request_id": "150116732819940316116461",
"status": "FAIL"
}',
]));
$this->engine->delete(Collection::make([new SearchableModel()]));
}

public function testSearch(): void
{
$this->client->shouldReceive('get')
Expand Down Expand Up @@ -278,7 +230,6 @@ public function testPaginate(): void

public function testSearchFailed(): void
{
$this->expectException(\Throwable::class);
$this->client->shouldReceive('get')
->times(1)
->andReturn(new OpenSearchResult([
Expand All @@ -300,7 +251,7 @@ public function testSearchFailed(): void
$builder->where('foo', 1);
$builder->orderBy('id', 'desc');

$this->engine->search($builder);
self::assertEmpty($this->engine->search($builder));
}

public function testCallback(): void
Expand Down Expand Up @@ -478,6 +429,7 @@ public function testSeachable(): void
->andReturn(new OpenSearchResult([
'result' => $result,
]));

self::assertCount(1, SearchableModel::search('test')->get());
}

Expand Down

0 comments on commit 4e59d34

Please sign in to comment.