Skip to content

Commit 2757f1d

Browse files
committed
Function updated and bug fixes
- functionScore() working - Matrix agg against multiple fields - mappings default to flat, - Direct Client indices() - model docs
1 parent 99f93cf commit 2757f1d

15 files changed

+95
-131
lines changed

src/Connection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use Elastic\Elasticsearch\Client;
99
use Elastic\Elasticsearch\ClientBuilder;
10+
use Elastic\Elasticsearch\Endpoints\Indices;
1011
use Elastic\Elasticsearch\Exception\AuthenticationException;
1112
use Elastic\Elasticsearch\Exception\ClientResponseException;
1213
use Elastic\Elasticsearch\Exception\MissingParameterException;
@@ -413,6 +414,11 @@ public function getMappings($index): array
413414
return $this->connection->getMappings($index);
414415
}
415416

417+
public function indices(): Indices
418+
{
419+
return $this->connection->indices();
420+
}
421+
416422
// ----------------------------------------------------------------------
417423
// Query Execution
418424
// ----------------------------------------------------------------------

src/ElasticClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PDPhilip\Elasticsearch;
44

55
use Elastic\Elasticsearch\Client;
6+
use Elastic\Elasticsearch\Endpoints\Indices;
67
use Elastic\Elasticsearch\Exception\ClientResponseException;
78
use Elastic\Elasticsearch\Exception\MissingParameterException;
89
use Elastic\Elasticsearch\Exception\ServerResponseException;
@@ -95,6 +96,11 @@ public function clearScroll(array $params = []): Elasticsearch|Promise
9596
// Indices
9697
// ----------------------------------------------------------------------
9798

99+
public function indices(): Indices
100+
{
101+
return $this->client->indices();
102+
}
103+
98104
/**
99105
* @throws ServerResponseException
100106
* @throws ClientResponseException

src/Eloquent/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,19 @@ public function deleteIndexIfExists(): void
560560
Schema::connection($this->query->connection->getName())->dropIfExists($this->from);
561561
}
562562

563-
public function getIndexMappings(bool $flatten = true): array
563+
public function getIndexMappings(bool $raw = false): array
564564
{
565-
return Schema::on($this->query->connection->getName())->getMappings($this->from, $flatten);
565+
return Schema::on($this->query->connection->getName())->getMappings($this->from, $raw);
566566
}
567567

568-
public function getFieldMappings(bool $flatten = true): array
568+
public function getFieldMappings(bool $raw = false): array
569569
{
570-
return Schema::connection($this->query->connection->getName())->getFieldMapping($this->from, '*', $flatten);
570+
return Schema::connection($this->query->connection->getName())->getFieldMapping($this->from, '*', $raw);
571571
}
572572

573-
public function getFieldMapping(string|array $field = '*', bool $flatten = true): array
573+
public function getFieldMapping(string|array $field = '*', bool $raw = false): array
574574
{
575-
return Schema::connection($this->query->connection->getName())->getFieldMapping($this->from, $field, $flatten);
575+
return Schema::connection($this->query->connection->getName())->getFieldMapping($this->from, $field, $raw);
576576
}
577577

578578
public function getIndexSettings(): array

src/Eloquent/Docs/ModelDocs.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
use Illuminate\Contracts\Database\Query\Expression;
99
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
1010
use Illuminate\Pagination\Cursor;
11-
use PDPhilip\Elasticsearch\Collection\ElasticCollection;
12-
use PDPhilip\Elasticsearch\Connection;
11+
use PDPhilip\Elasticsearch\Eloquent\Builder;
12+
use PDPhilip\Elasticsearch\Eloquent\ElasticCollection;
1313
use PDPhilip\Elasticsearch\Eloquent\Model;
1414
use PDPhilip\Elasticsearch\Pagination\SearchAfterPaginator;
15-
use PDPhilip\Elasticsearch\Query\Builder;
1615

1716
/**
1817
* Query Builder Methods ---------------------------------
1918
*
20-
* @method static $this query()
19+
* @method static Builder query()
2120
* @method static $this where(array|Closure|Expression|string $column, $operator = null, $value = null, $boolean = 'and')
2221
* @method static $this whereIn(string $column, array $values)
2322
* @method static $this whereExact(string $column, string $value, $boolean = 'and')
@@ -86,15 +85,14 @@
8685
* @method static ElasticCollection insert($values, $returnData = null):
8786
* @method static ElasticCollection insertWithoutRefresh($values, $returnData = null)
8887
* @method static array toDsl(array $columns = ['*'])
88+
* @method static array toSql(array $columns = ['*'])
8989
* @method static mixed rawDsl(array $bodyParams)
9090
* @method static ElasticCollection rawSearch(array $bodyParams)
9191
* @method static array rawAggregation(array $bodyParams)
9292
* @method static bool chunk(mixed $count, callable $callback, string $keepAlive = '5m')
9393
* @method static bool chunkById(mixed $count, callable $callback, $column = '_id', $alias = null, $keepAlive = '5m')
9494
*
9595
* Index Methods ---------------------------------
96-
* @method static string getQualifiedKeyName()
97-
* @method static Connection getConnection()
9896
* @method static void truncate()
9997
* @method static bool indexExists()
10098
* @method static bool deleteIndexIfExists()
@@ -104,27 +102,6 @@
104102
* @method static array getFieldMapping(string|array $field = '*', $raw = false)
105103
* @method static array getIndexOptions()
106104
*
107-
* Search Methods - Due for sunsetting, keep for now
108-
* @method static $this term(string $term, $boostFactor = null)
109-
* @method static $this andTerm(string $term, $boostFactor = null)
110-
* @method static $this orTerm(string $term, $boostFactor = null)
111-
* @method static $this fuzzyTerm(string $term, $boostFactor = null)
112-
* @method static $this andFuzzyTerm(string $term, $boostFactor = null)
113-
* @method static $this orFuzzyTerm(string $term, $boostFactor = null)
114-
* @method static $this regEx(string $term, $boostFactor = null)
115-
* @method static $this andRegEx(string $term, $boostFactor = null)
116-
* @method static $this orRegEx(string $term, $boostFactor = null)
117-
* @method static $this phrase(string $term, $boostFactor = null)
118-
* @method static $this andPhrase(string $term, $boostFactor = null)
119-
* @method static $this orPhrase(string $term, $boostFactor = null)
120-
* @method static $this minShouldMatch(int $value)
121-
* @method static $this highlight(array $fields = [], string|array $preTag = '<em>', string|array $postTag = '</em>', $globalOptions = [])
122-
* @method static $this minScore(float $value)
123-
* @method static $this field(string $field, int $boostFactor = null)
124-
* @method static $this fields(array $fields)
125-
* @method static array searchModels(array $columns = ['*'])
126-
* @method static ElasticCollection search(array $columns = ['*'])
127-
*
128105
* @property object $search_highlights
129106
* @property object $with_highlights
130107
* @property array $search_highlights_as_array

src/Eloquent/ElasticsearchModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Str;
1313
use PDPhilip\Elasticsearch\Connection;
1414
use PDPhilip\Elasticsearch\Data\ModelMeta;
15+
use PDPhilip\Elasticsearch\Eloquent\Docs\ModelDocs;
1516
use PDPhilip\Elasticsearch\Exceptions\RuntimeException;
1617
use PDPhilip\Elasticsearch\Traits\HasOptions;
1718

@@ -21,10 +22,11 @@
2122
* @property object $withHighlights
2223
*
2324
* @mixin \PDPhilip\Elasticsearch\Query\Builder
25+
* @mixin \PDPhilip\Elasticsearch\Eloquent\Builder
2426
*/
2527
trait ElasticsearchModel
2628
{
27-
use HasOptions, HybridRelations;
29+
use HasOptions, HybridRelations, ModelDocs;
2830

2931
protected ?ModelMeta $meta;
3032

src/Query/Builder.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,12 +1750,15 @@ public function stringStats($columns, $options = [])
17501750
*/
17511751
protected function aggregateMetric($function, $columns = ['*'], $options = [])
17521752
{
1753+
if ($function == 'matrix_stats' && is_array($columns)) {
1754+
$args = $columns;
1755+
}
17531756
// Each column we want aggregated
17541757
$columns = Arr::wrap($columns);
17551758
foreach ($columns as $column) {
17561759
$this->metricsAggregations[] = [
17571760
'key' => $column,
1758-
'args' => $column,
1761+
'args' => ! empty($args) ? $args : $column,
17591762
'type' => $function,
17601763
'options' => $options,
17611764
];
@@ -2034,15 +2037,15 @@ public function dynamicFilter(string $method, array $args): self
20342037
/**
20352038
* Adds a function score of any type
20362039
*
2037-
* @param string $boolean
2038-
* @param array $options see elastic search docs for options
2040+
* @param string $functionType
2041+
* @param array $functionOptions see elastic search docs for options
20392042
*/
2040-
public function functionScore($functionType, callable $query, $boolean = 'and', $options = []): self
2043+
public function functionScore($functionType, array $functionOptions, callable $query, string $boolean = 'and'): self
20412044
{
2042-
20432045
$type = 'FunctionScore';
2046+
$options = $functionOptions;
20442047

2045-
call_user_func($query, $query = $this->newQuery());
2048+
call_user_func($query, $query = $this->newQuery($this->from));
20462049

20472050
$this->wheres[] = compact('functionType', 'query', 'type', 'boolean', 'options');
20482051

@@ -2101,7 +2104,7 @@ public function getMapping()
21012104
{
21022105
if (empty($this->mapping)) {
21032106
$index = $this->getFrom();
2104-
$this->mapping = Schema::connection($this->connection->getName())->getFieldsMapping($index, true);
2107+
$this->mapping = Schema::connection($this->connection->getName())->getFieldsMapping($index);
21052108
}
21062109

21072110
return $this->mapping;

src/Query/DSL/DslFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ public static function prefix(string $field, string $value, array $options = [])
255255
];
256256
}
257257

258-
public static function functionScore(array $query, string $functionType, array $options = []): array
258+
public static function functionScore(array $query, string $functionType, array $functionOptions = []): array
259259
{
260260
return [
261261
'function_score' => array_merge(
262-
[$functionType => ['query' => $query]],
263-
$options
262+
['query' => $query],
263+
[$functionType => $functionOptions],
264264
),
265265
];
266266
}
@@ -355,11 +355,11 @@ public static function composite(array $sources, ?int $size = null, ?array $afte
355355
];
356356
}
357357

358-
public static function matrixStats(string $field, array $options = []): array
358+
public static function matrixStats(mixed $fields, array $options = []): array
359359
{
360360
return [
361361
'matrix_stats' => array_merge(
362-
['fields' => $field],
362+
['fields' => $fields],
363363
$options
364364
),
365365
];

src/Query/Grammar.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ protected function compileWhereBetween(Builder $builder, array $where): array
444444
protected function compileWhereFunctionScore(Builder $builder, array $where): array
445445
{
446446
$compiled = $this->compileWheres($where['query']);
447-
448447
foreach ($compiled as $queryPart => $clauses) {
449448
$compiled[$queryPart] = array_map(function ($clause) use ($where) {
450449
if ($clause) {
@@ -457,9 +456,9 @@ protected function compileWhereFunctionScore(Builder $builder, array $where): ar
457456

458457
$compiled = array_filter($compiled);
459458
$functionType = $where['functionType'];
460-
$options = $where['options'] ?? [];
459+
$functionOptions = $where['options'];
461460

462-
return DslFactory::functionScore($compiled['query'], $functionType, $options);
461+
return DslFactory::functionScore($compiled['query'], $functionType, $functionOptions);
463462
}
464463

465464
/**
@@ -960,7 +959,6 @@ protected function compileMetricAggregations(Builder $builder): array
960959

961960
foreach ($builder->metricsAggregations as $aggregation) {
962961
$result = $this->compileAggregation($builder, $aggregation);
963-
964962
$aggregations = array_merge_recursive($aggregations, $result);
965963
}
966964

@@ -1138,10 +1136,11 @@ protected function compileBoxplotAggregation(Builder $builder, array $aggregatio
11381136
*/
11391137
protected function compileMatrixStatsAggregation(Builder $builder, array $aggregation): array
11401138
{
1139+
11411140
$options = $aggregation['options'] ?? [];
1142-
$field = is_array($aggregation['args']) ? $aggregation['args']['field'] : $aggregation['args'];
1141+
$fields = $aggregation['args'];
11431142

1144-
return DslFactory::matrixStats($field, $options);
1143+
return DslFactory::matrixStats($fields, $options);
11451144
}
11461145

11471146
/**

0 commit comments

Comments
 (0)