Skip to content

Commit 9b4b9c3

Browse files
committed
Add getColumns method and improve aggregation options
Introduces a getColumns method to Schema\Builder for retrieving column metadata from Elasticsearch index mappings. Also updates Query\Grammar to allow 'unmapped_type' in aggregation order options and to respect query limits when setting the 'size' parameter in aggregations.
1 parent e32ab6a commit 9b4b9c3

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/Query/Grammar.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,13 @@ public function compileSelect($query): array
229229
}
230230

231231
$dsl->setBody(['aggs'], $this->compileBucketAggregations($query, $sorts));
232-
$dsl->setBody(['size'], 0);
233-
234-
// return $dsl->getDsl();
232+
$dsl->setBody(['size'], $query->getSetLimit() ?? 0);
235233
}
236234

237235
// Else if we have metrics aggregations
238236
elseif ($query->metricsAggregations) {
239237
$dsl->setBody(['aggs'], $this->compileMetricAggregations($query));
240-
$dsl->setBody(['size'], 0);
238+
$dsl->setBody(['size'], $query->getSetLimit() ?? 0);
241239
}
242240

243241
if (! $dsl->getBodyValue(['query'])) {
@@ -806,7 +804,7 @@ protected function compileOrders(Builder|BaseBuilder $builder, $orders = []): ar
806804
'order' => $order['direction'] < 0 ? 'desc' : 'asc',
807805
];
808806

809-
$allowedOptions = ['missing', 'mode', 'nested'];
807+
$allowedOptions = ['missing', 'mode', 'nested', 'unmapped_type'];
810808

811809
$options = isset($order['options']) ? array_intersect_key($order['options'], array_flip($allowedOptions)) : [];
812810

src/Schema/Builder.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Elastic\Elasticsearch\Exception\ServerResponseException;
1212
use Illuminate\Database\Schema\Builder as BaseBuilder;
1313
use Illuminate\Support\Arr;
14+
use Illuminate\Support\Str;
1415
use PDPhilip\Elasticsearch\Connection;
1516
use PDPhilip\Elasticsearch\Exceptions\LogicException;
1617
use PDPhilip\Elasticsearch\Laravel\Compatibility\Schema\BuilderCompatibility;
@@ -68,6 +69,46 @@ public function hasTable($table): bool
6869
return $this->connection->elastic()->indices()->exists($params)->asBool();
6970
}
7071

72+
public function getColumns($table): array
73+
{
74+
$index = $this->parseIndexName($table);
75+
$params = ['index' => $index, 'fields' => ['*']];
76+
$result = $this->connection->elastic()->indices()->getFieldMapping($params)->asArray();
77+
$mappings = $result[$index]['mappings'] ?? [];
78+
$columns[] = [
79+
'name' => 'id',
80+
'type_name' => 'text',
81+
'type' => 'text',
82+
'collation' => null,
83+
'nullable' => false,
84+
'default' => null,
85+
'auto_increment' => false,
86+
'comment' => null,
87+
'generation' => null,
88+
];
89+
foreach ($mappings as $field => $mapping) {
90+
91+
if (! Str::startsWith($field, '_')) {
92+
93+
$mapCollection = collect($mapping)->dot()->toArray();
94+
$type = $mapCollection['mapping.'.$field.'.types'] ?? 'text';
95+
$columns[] = [
96+
'name' => $field,
97+
'type_name' => $type,
98+
'type' => $type,
99+
'collation' => null,
100+
'nullable' => false,
101+
'default' => null,
102+
'auto_increment' => false,
103+
'comment' => null,
104+
'generation' => null,
105+
];
106+
}
107+
}
108+
109+
return $columns;
110+
}
111+
71112
public function hasColumn($table, $column): bool
72113
{
73114
$index = $this->parseIndexName($table);

0 commit comments

Comments
 (0)