Skip to content

Commit 491b3b2

Browse files
add latest ES|QL commands
1 parent bee8abb commit 491b3b2

File tree

8 files changed

+327
-36
lines changed

8 files changed

+327
-36
lines changed

src/Helper/Esql/EsqlBase.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,22 @@ public function fork(
298298
return new ForkCommand($this, $fork1, $fork2, $fork3, $fork4, $fork5, $fork6, $fork7, $fork8);
299299
}
300300

301+
/**
302+
* The `FUSE` processing command merges rows from multiple result sets and assigns
303+
* new relevance scores.
304+
*
305+
* @param string $method Defaults to `RRF`. Can be one of `RRF` (for Reciprocal Rank Fusion)
306+
* or `LINEAR` (for linear combination of scores). Designates which
307+
* method to use to assign new relevance scores.
308+
*
309+
* Examples:
310+
*
311+
*/
312+
public function fuse(string $method = ""): FuseCommand
313+
{
314+
return new FuseCommand($this, $method);
315+
}
316+
301317
/**
302318
* `GROK` enables you to extract structured data out of a string.
303319
*
@@ -334,6 +350,25 @@ public function grok(string $input, string $pattern): GrokCommand
334350
return new GrokCommand($this, $input, $pattern);
335351
}
336352

353+
/**
354+
* The `INLINE STATS` processing command groups rows according to a common value
355+
* and calculates one or more aggregated values over the grouped rows.
356+
*
357+
* The command is identical to ``STATS`` except that it preserves all the columns from
358+
* the input table.
359+
*
360+
* @param string ...$expressions A list of boolean expressions, given as
361+
* positional or named arguments. These
362+
* expressions are combined with an `AND`
363+
* logical operator.
364+
*
365+
* Examples:
366+
*/
367+
public function inlineStats(string ...$expressions): InlineStatsCommand
368+
{
369+
return new InlineStatsCommand($this, $expressions);
370+
}
371+
337372
/**
338373
* The `KEEP` processing command enables you to specify what columns are returned
339374
* and the order in which they are returned.
@@ -538,8 +573,6 @@ public function sort(string ...$columns): SortCommand
538573
* or named arguments. The argument names are
539574
* used for the returned aggregated values.
540575
*
541-
* Note that only one of `expressions` and `named_expressions` must be provided.
542-
*
543576
* Examples:
544577
*
545578
* $query1 = Query::from("employees")

src/Helper/Esql/FromCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Elastic\Elasticsearch\Helper\Esql;
1616

1717
class FromCommand extends EsqlBase {
18+
const name = "FROM";
1819
private array $indices;
1920
private array $metadata_fields = [];
2021

@@ -24,7 +25,7 @@ public function __construct(array $indices)
2425
}
2526

2627
/**
27-
* Continuation of the `FROM` source command.
28+
* Continuation of the `FROM` or `TS` source commands.
2829
*
2930
* *param string ...$metadata_fields Metadata fields to retrieve, given as
3031
* positional arguments.
@@ -37,7 +38,7 @@ public function metadata(string ...$metadata_fields): FromCommand
3738

3839
protected function renderInternal(): string
3940
{
40-
$s = "FROM " . implode(", ", $this->indices);
41+
$s = $this::name . " " . implode(", ", $this->indices);
4142
if (sizeof($this->metadata_fields)) {
4243
$s .= " METADATA " . implode(
4344
", ", array_map(array($this, "formatId"), $this->metadata_fields)

src/Helper/Esql/FuseCommand.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
/**
18+
* Implementation of the `FUSE` processing command.
19+
*
20+
* This class inherits from EsqlBase to make it possible to chain all the commands
21+
* that belong to an ES|QL query in a single expression.
22+
*/
23+
class FuseCommand extends EsqlBase {
24+
private string $method;
25+
private array $columns = [];
26+
private array $options = [];
27+
28+
public function __construct(EsqlBase $parent, string $method)
29+
{
30+
parent::__construct($parent);
31+
$this->method = $method;
32+
}
33+
34+
public function by(string ...$columns): FuseCommand
35+
{
36+
$this->columns = $columns;
37+
return $this;
38+
}
39+
40+
public function with(mixed ...$options): FuseCommand
41+
{
42+
$this->options = $options;
43+
return $this;
44+
}
45+
46+
protected function renderInternal(): string
47+
{
48+
$method = ($this->method != "") ? " " . strtoupper($this->method) : "";
49+
$by = sizeof($this->columns) ? " " . implode(" ", array_map(function($column) {
50+
return "BY " . $column;
51+
}, $this->columns)) : "";
52+
$with = sizeof($this->options) ? " WITH " . json_encode($this->options) : "";
53+
return "FUSE" . $method . $by . $with;
54+
}
55+
56+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
/**
18+
* Implementation of the `STATS` processing command.
19+
*
20+
* This class inherits from EsqlBase to make it possible to chain all the commands
21+
* that belong to an ES|QL query in a single expression.
22+
*/
23+
class InlineStatsCommand extends StatsCommand {
24+
const name = "INLINE STATS";
25+
}

src/Helper/Esql/Query.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,67 +16,84 @@
1616

1717
abstract class Query {
1818
/**
19-
The `FROM` source command returns a table with data from a data stream,
20-
index, or alias.
19+
* The `FROM` source command returns a table with data from a data stream,
20+
* index, or alias.
2121
*
22-
@param string $indices A list of indices, data streams or aliases. Supports
23-
wildcards and date math.
22+
* @param string $indices A list of indices, data streams or aliases. Supports
23+
* wildcards and date math.
2424
*
25-
Examples:
25+
* Examples:
2626
*
27-
$query1 = Query::from("employees");
28-
$query2 = Query::from("<logs-{now/d}>");
29-
$query3 = Query::from("employees-00001", "other-employees-*");
30-
$query4 = Query::from("cluster_one:employees-00001", "cluster_two:other-employees-*");
31-
$query5 = Query::from("employees")->metadata("_id");
27+
* $query1 = Query::from("employees");
28+
* $query2 = Query::from("<logs-{now/d}>");
29+
* $query3 = Query::from("employees-00001", "other-employees-*");
30+
* $query4 = Query::from("cluster_one:employees-00001", "cluster_two:other-employees-*");
31+
* $query5 = Query::from("employees")->metadata("_id");
3232
*/
3333
public static function from(string ...$indices): FromCommand
3434
{
3535
return new FromCommand($indices);
3636
}
3737

3838
/**
39-
The ``ROW`` source command produces a row with one or more columns with
40-
values that you specify. This can be useful for testing.
39+
* The ``ROW`` source command produces a row with one or more columns with
40+
* values that you specify. This can be useful for testing.
4141
*
42-
@param string ...$params the column values to produce, given as keyword
43-
arguments.
42+
* @param string ...$params the column values to produce, given as keyword
43+
* arguments.
4444
*
45-
Examples:
45+
* Examples:
4646
*
47-
$query1 = Query::row(a: 1, b: "two", c: null);
48-
$query2 = Query::row(a: [2, 1]);
47+
* $query1 = Query::row(a: 1, b: "two", c: null);
48+
* $query2 = Query::row(a: [2, 1]);
4949
*/
5050
public static function row(mixed ...$params): RowCommand
5151
{
5252
return new RowCommand($params);
5353
}
5454

5555
/**
56-
The `SHOW` source command returns information about the deployment and
57-
its capabilities.
56+
* The `SHOW` source command returns information about the deployment and
57+
* its capabilities.
5858
*
59-
@param string $item Can only be `INFO`.
59+
* @param string $item Can only be `INFO`.
6060
*
61-
Examples:
61+
* Examples:
6262
*
63-
$query = Query::show("INFO");
63+
* $query = Query::show("INFO");
6464
*/
6565
public static function show(string $item): ShowCommand
6666
{
6767
return new ShowCommand($item);
6868
}
6969

7070
/**
71-
This method can only be used inside a `FORK` command to create each branch.
71+
* The `TS` source command is similar to ``FROM``, but for time series indices.
7272
*
73-
Examples:
73+
* @param string $indices A list of indices, data streams or aliases. Supports
74+
* wildcards and date math.
7475
*
75-
$query = Query::from("employees")
76-
->fork(
77-
Query::branch()->where("emp_no == 10001"),
78-
Query::branch()->where("emp_no == 10002"),
79-
)
76+
* Examples:
77+
*
78+
* $query = Query::ts("metrics")
79+
* ->where("@timestamp >= now() - 1 day")
80+
* ->stats("SUM(AVG_OVER_TIME(memory_usage))").by("host", "TBUCKET(1 hour)")
81+
*/
82+
public static function ts(string ...$indices): TSCommand
83+
{
84+
return new TSCommand($indices);
85+
}
86+
87+
/**
88+
* This method can only be used inside a `FORK` command to create each branch.
89+
*
90+
* Examples:
91+
*
92+
* $query = Query::from("employees")
93+
* ->fork(
94+
* Query::branch()->where("emp_no == 10001"),
95+
* Query::branch()->where("emp_no == 10002"),
96+
* )
8097
*/
8198
public static function branch(): Branch
8299
{

src/Helper/Esql/StatsCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* that belong to an ES|QL query in a single expression.
2222
*/
2323
class StatsCommand extends EsqlBase {
24+
const name = "STATS";
2425
private array $expressions = [];
2526
private array $named_expressions = [];
2627
private array $grouping_expressions = [];
@@ -53,8 +54,9 @@ public function by(string ...$grouping_expressions): StatsCommand
5354

5455
protected function renderInternal(): string
5556
{
57+
$indent = str_repeat(" ", strlen($this::name) + 3);
5658
if (sizeof($this->named_expressions)) {
57-
$expr = $this->formatKeyValues($this->named_expressions, implodeText: ",\n ");
59+
$expr = $this->formatKeyValues($this->named_expressions, implodeText: ",\n" . $indent);
5860
}
5961
else {
6062
$expr = implode(
@@ -64,8 +66,8 @@ protected function renderInternal(): string
6466
}
6567
$by = "";
6668
if (sizeof($this->grouping_expressions)) {
67-
$by = "\n BY " . implode(", ", $this->grouping_expressions);
69+
$by = "\n" . $indent . "BY " . implode(", ", $this->grouping_expressions);
6870
}
69-
return "STATS " . $expr . $by;
71+
return $this::name . " " . $expr . $by;
7072
}
7173
}

src/Helper/Esql/TSCommand.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
class TSCommand extends FromCommand {
18+
const name = "TS";
19+
}

0 commit comments

Comments
 (0)