Skip to content

Commit

Permalink
使用 Value 对象存储具体数据
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Jul 9, 2024
1 parent 61cd49b commit fd2babb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
35 changes: 25 additions & 10 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function toBody(): array
}

public function bulk(
array $docs,
array|Collection $values,
#[ArrayShape([
'refresh' => 'bool',
'retry_on_conflict' => 'int',
Expand All @@ -152,15 +152,23 @@ public function bulk(
$settings ??= $this->document->getConfig()->getUpdateSettings();

$body = [];
foreach ($docs as $doc) {
foreach ($values as $value) {
if ($value instanceof Value) {
$id = $value->id;
$source = $value->source;
} else {
$id = $value[$this->document->getKey()];
$source = $value;
}

$body[] = [
'index' => [
'_index' => $this->document->getIndex(),
'_id' => $doc[$this->document->getKey()],
'_id' => $id,
],
];

$body[] = $doc;
$body[] = $source;
}

return $this->document->getWriteClient()->bulk([
Expand All @@ -170,15 +178,22 @@ public function bulk(
}

public function update(
array $doc,
array|Value $value,
mixed $id = null,
#[ArrayShape([
'refresh' => 'bool',
'retry_on_conflict' => 'int',
])]
?array $settings = null
): bool {
$id ??= $doc[$this->document->getKey()] ?? $this->getKeyValue();
if ($value instanceof Value) {
$id = $value->id;
$source = $value->source;
} else {
$id ??= $value[$this->document->getKey()] ?? $this->getKeyValue();
$source = $value;
}

if ($id === null) {
throw new RuntimeException('The document does not contain any ID');
}
Expand All @@ -189,7 +204,7 @@ public function update(
'index' => $this->document->getIndex(),
'id' => $id,
'body' => [
'doc' => $doc,
'doc' => $source,
'doc_as_upsert' => true,
],
...$settings,
Expand All @@ -202,13 +217,13 @@ public function get(): Collection

$result = [];
foreach ($response['hits']['hits'] as $hit) {
$result[] = $hit['_source'];
$result[] = new Value($hit['_id'], $hit['_source']);
}
return new Collection($result);
}

/**
* @return array{int, Collection<int, array>}
* @return array{int, Collection<int, Value>}
*/
public function paginate(): array
{
Expand All @@ -219,7 +234,7 @@ public function paginate(): array
$total = $response['hits']['total']['value'];
$result = [];
foreach ($response['hits']['hits'] as $hit) {
$result[] = $hit['_source'];
$result[] = new Value($hit['_id'], $hit['_source']);
}

return [$total, new Collection($result)];
Expand Down
36 changes: 36 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Fan\ElasticBoolQuery;

use Hyperf\Contract\Arrayable;
use JsonSerializable;

class Value implements JsonSerializable, Arrayable
{
public function __construct(public mixed $id, public array $source)
{
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}

public function toArray(): array
{
return [
'id' => $this->id,
'source' => $this->source,
];
}
}
28 changes: 14 additions & 14 deletions tests/Cases/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public function testGet()
{
$res = Foo::query()->where('id', 1)->get();

$this->assertSame(['id' => 1, 'name' => 'foo', 'summary' => 'foo'], $res->first());
$this->assertSame(['id' => 1, 'name' => 'foo', 'summary' => 'foo'], $res->first()->source);
}

public function testOrWhere()
{
$res = Foo::query()->orWhere('id', 1)->orWhere('id', 2)->orderBy('id', 'asc')->get();

$this->assertSame(1, $res->first()['id']);
$this->assertSame(2, $res->last()['id']);
$this->assertSame(1, $res->first()->source['id']);
$this->assertSame(2, $res->last()->source['id']);

$this->assertSame(2, $res->count());
}
Expand All @@ -100,13 +100,13 @@ public function testUpdateAndGet()
{
$res = Foo::query()->where('id', 1)->get();

$summary = $res->first()['summary'];
$summary = $res->first()->source['summary'];

Foo::query()->where('id', 1)->update(['summary' => 'foofoo']);

$res = Foo::query()->where('id', 1)->get();

$this->assertSame('foofoo', $res->first()['summary']);
$this->assertSame('foofoo', $res->first()->source['summary']);

Foo::query()->where('id', 1)->update(['summary' => $summary]);
}
Expand All @@ -131,18 +131,18 @@ public function testOrderBy()
{
$res = Foo::query()->orderBy('id', 'asc')->get();

$this->assertSame(1, $res->first()['id']);
$this->assertSame(1, $res->first()->source['id']);

$res = Foo::query()->where('id', '<=', 5)->orderBy('id', 'desc')->get();

$this->assertSame(5, $res->first()['id']);
$this->assertSame(5, $res->first()->source['id']);
}

public function testNotEqual()
{
$res = Foo::query()->where('id', '!=', 1)->orderBy('id', 'asc')->get();

$this->assertSame(2, $res->first()['id']);
$this->assertSame(2, $res->first()->source['id']);
}

public function testClosure()
Expand All @@ -155,8 +155,8 @@ public function testClosure()
->get();

$this->assertSame(2, $res->count());
$this->assertSame(2, $res->first()['id']);
$this->assertSame(5, $res->last()['id']);
$this->assertSame(2, $res->first()->source['id']);
$this->assertSame(5, $res->last()->source['id']);
}

public function testOrWhereClosure()
Expand All @@ -171,8 +171,8 @@ public function testOrWhereClosure()
->get();

$this->assertSame(2, $res->count());
$this->assertSame(2, $res->first()['id']);
$this->assertSame(5, $res->last()['id']);
$this->assertSame(2, $res->first()->source['id']);
$this->assertSame(5, $res->last()->source['id']);
}

public function testBulk()
Expand All @@ -184,10 +184,10 @@ public function testBulk()

$this->assertTrue($res);

$first = Foo::query()->where('id', 6)->get()->first();
$first = Foo::query()->where('id', 6)->get()->first()->source;
$this->assertSame($id, $first['summary']);

$first = Foo::query()->where('id', 7)->get()->first();
$first = Foo::query()->where('id', 7)->get()->first()->source;
$this->assertSame($id2, $first['summary']);
}
}

0 comments on commit fd2babb

Please sign in to comment.