-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4f315f
commit d4cc557
Showing
14 changed files
with
345 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeDuck\Elasticsearch; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class Document | ||
{ | ||
private Identifier $identifier; | ||
private array $source; | ||
private float $score; | ||
|
||
public function __construct(Identifier $identifier, array $source, float $score = 0.0) | ||
{ | ||
$this->identifier = $identifier; | ||
$this->source = $source; | ||
$this->score = $score; | ||
} | ||
|
||
public static function fromArray(array $result): self | ||
{ | ||
return new self( | ||
Identifier::fromArray($result), | ||
isset($result['_source']) && is_array($result['_source']) ? $result['_source'] : [], | ||
isset($result['_score']) && is_float($result['_score']) ? $result['_score'] : 0.0 | ||
); | ||
} | ||
|
||
public function getSource(): array | ||
{ | ||
return $this->source; | ||
} | ||
|
||
public function getScore(): float | ||
{ | ||
return $this->score; | ||
} | ||
|
||
public function getIdentifier(): Identifier | ||
{ | ||
return $this->identifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeDuck\Elasticsearch; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class Identifier | ||
{ | ||
private string $index; | ||
private string $id; | ||
private string $type; | ||
|
||
public function __construct(string $index, string $id, string $type = '_doc') | ||
{ | ||
$this->index = $index; | ||
$this->id = $id; | ||
$this->type = $type; | ||
} | ||
|
||
public static function fromArray(array $result): self | ||
{ | ||
return new self( | ||
isset($result['_index']) && is_string($result['_index']) ? $result['_index'] : '', | ||
isset($result['_id']) && is_string($result['_id']) ? $result['_id'] : '', | ||
isset($result['_type']) && is_string($result['_type']) ? $result['_type'] : '_doc', | ||
); | ||
} | ||
|
||
public function getIndex(): string | ||
{ | ||
return $this->index; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeDuck\Elasticsearch; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class QueryResult | ||
{ | ||
/** @var Document[] */ | ||
private array $documents = []; | ||
private int $took; | ||
private float $maxScore; | ||
|
||
/** | ||
* @param Document[] $documents | ||
*/ | ||
public function __construct(array $documents, int $took, float $maxScore) | ||
{ | ||
$this->documents = $documents; | ||
$this->took = $took; | ||
$this->maxScore = $maxScore; | ||
} | ||
|
||
public static function fromArray(array $result): self | ||
{ | ||
$documents = []; | ||
|
||
/** @psalm-suppress MixedAssignment */ | ||
if (isset($result['hits']['hits']) && is_array($result['hits']['hits'])) { | ||
foreach ($result['hits']['hits'] as $documentArray) { | ||
/** @psalm-suppress MixedArgument */ | ||
$documents[] = Document::fromArray($documentArray); | ||
} | ||
} | ||
|
||
return new self( | ||
$documents, | ||
isset($result['took']) && is_int($result['took']) ? $result['took'] : 0, | ||
isset($result['hits']['max_score']) && is_float($result['hits']['max_score']) | ||
? $result['hits']['max_score'] | ||
: 0.0 | ||
); | ||
} | ||
|
||
/** | ||
* @return Document[] | ||
*/ | ||
public function getDocuments(): array | ||
{ | ||
return $this->documents; | ||
} | ||
|
||
public function getCount(): int | ||
{ | ||
return count($this->documents); | ||
} | ||
|
||
public function getTook(): int | ||
{ | ||
return $this->took; | ||
} | ||
|
||
public function getMaxScore(): float | ||
{ | ||
return $this->maxScore; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.