Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions src/Concerns/BuildsFluentQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ trait BuildsFluentQueries
*/
protected $source;

/**
* Query returned fields list
*
* @var array|null
*/
protected $fields;

/**
* Mapping type
* ============
Expand Down Expand Up @@ -923,19 +930,27 @@ public function select(...$args): self
{
$fields = $this->flattenArgs($args);

$this->source[Query::SOURCE_INCLUDES] = array_values(array_unique(array_merge(
$this->source[Query::SOURCE_INCLUDES] ?? [],
$fields
)));

$this->source[Query::SOURCE_EXCLUDES] = array_values(array_filter(
$this->source[Query::SOURCE_EXCLUDES] ?? [], function ($field) {
return ! in_array(
$field,
if (Query::$getDataFrom == Query::FIELD_FIELDS) {
$this->fields = array_values(array_unique(array_merge(
$this->source[Query::SOURCE_INCLUDES] ?? [],
false
);
}));
$fields
)));
}
else {
$this->source[Query::SOURCE_INCLUDES] = array_values(array_unique(array_merge(
$this->source[Query::SOURCE_INCLUDES] ?? [],
$fields
)));

$this->source[Query::SOURCE_EXCLUDES] = array_values(array_filter(
$this->source[Query::SOURCE_EXCLUDES] ?? [], function ($field) {
return !in_array(
$field,
$this->source[Query::SOURCE_INCLUDES] ?? [],
false
);
}));
}

return $this;
}
Expand Down Expand Up @@ -1364,6 +1379,16 @@ protected function getBody(): array
);
}

if ($this->fields !== null) {
$fields = $body[Query::FIELD_FIELDS] ?? [];

$body[Query::FIELD_FIELDS] = array_merge(
$fields,
$this->fields
);
$body[Query::FIELD_SOURCE] = false;
}

$body[self::FIELD_QUERY] = $body[self::FIELD_QUERY] ?? [];

if (count($this->must)) {
Expand Down Expand Up @@ -1512,4 +1537,19 @@ private function resolveRegexpFlags(int $flags): ?string

return implode('|', $stringFlags);
}

public function selectFromSource(): self
{
Query::$getDataFrom = Query::FIELD_SOURCE;

return $this;
}

public function selectFromFields(): self
{
Query::$getDataFrom = Query::FIELD_FIELDS;

return $this;
}

}
32 changes: 30 additions & 2 deletions src/Concerns/ExecutesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ public function update(array $attributes, $id = null): object
*/
protected function createModelInstance(array $document): Model
{
$data = $document[Query::FIELD_SOURCE] ?? [];
$data = $this->flattenValues($document[Query::$getDataFrom] ?? []);
$metadata = array_diff_key($document, array_flip([
Query::FIELD_SOURCE,
Query::$getDataFrom,
]));

return $this->getModel()->newInstance(
Expand Down Expand Up @@ -669,4 +669,32 @@ private function addBaseParams(array $params): array

return $params;
}

/**
* ES always returns an array when using "fields" to select columns
* This method turns single element arrays into strings
*
* @param array<string, mixed> $params Recieved values
*
* @return array<string, mixed> Flattened values
*/
private function flattenValues(array $values): ?array
{
if (Query::$getDataFrom == Query::FIELD_SOURCE) {
return $values;
}

$flattened = [];

foreach ($values as $key => $value) {
if (is_array($value) && count($value) == 1) {
$flattened[$key] = $value[0];
} else {
$flattened[$key] = $value;
}
}

return $flattened;
}

}
4 changes: 4 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Query implements Arrayable, JsonSerializable, Jsonable, IteratorAggregate

protected const FIELD_SOURCE = '_source';

protected const FIELD_FIELDS = 'fields';

protected const FIELD_TYPE = '_type';

public const GT = self::OPERATOR_GREATER_THAN;
Expand Down Expand Up @@ -143,6 +145,8 @@ class Query implements Arrayable, JsonSerializable, Jsonable, IteratorAggregate
self::SOURCE_EXCLUDES => [],
];

public static $getDataFrom = self::FIELD_FIELDS;

/**
* @var null
* @deprecated Use getConnection()->getClient() to access the client instead
Expand Down