Skip to content

Commit da9f861

Browse files
add remaining commands
1 parent 04d3f81 commit da9f861

23 files changed

+1631
-7
lines changed

src/Helper/Esql/Branch.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 a branch inside a `FORK` 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 Branch extends EsqlBase {
24+
public function __construct()
25+
{
26+
parent::__construct(null);
27+
}
28+
29+
protected function render_internal(): string
30+
{
31+
return "";
32+
}
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 `CHANGE_POINT` 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 ChangePointCommand extends EsqlBase {
24+
private string $value;
25+
private string $key = "";
26+
private string $type_name = "type";
27+
private string $pvalue_name = "value";
28+
29+
public function __construct(EsqlBase $parent, string $value)
30+
{
31+
parent::__construct($parent);
32+
$this->value = $value;
33+
}
34+
35+
/**
36+
* Continuation of the `CHANGE_POINT` command.
37+
*
38+
* @param string $key The column with the key to order the values by. If not
39+
* specified, `@timestamp` is used.
40+
*/
41+
public function on(string $key): ChangePointCommand
42+
{
43+
$this->key = $key;
44+
return $this;
45+
}
46+
47+
/**
48+
* Continuation of the `CHANGE_POINT` command.
49+
*
50+
* @param string $type_name The name of the output column with the change
51+
* point type. If not specified, `type` is used.
52+
* @param string $pvalue_name The name of the output column with the p-value
53+
* that indicates how extreme the change point is.
54+
* If not specified, `pvalue` is used.
55+
*/
56+
public function as(string $type_name, string $pvalue_name): ChangePointCommand
57+
{
58+
$this->type_name = $type_name;
59+
$this->pvalue_name = $pvalue_name;
60+
return $this;
61+
}
62+
63+
protected function render_internal(): string
64+
{
65+
$key = $this->key ? " ON " . $this->format_id($this->key) : "";
66+
$names = ($this->type_name && $this->pvalue_name) ?
67+
" AS " . $this->format_id($this->type_name) . ", " .
68+
$this->format_id($this->pvalue_name)
69+
: "";
70+
return "CHANGE_POINT " . $this->value . $key . $names;
71+
}
72+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
use RuntimeException;
18+
19+
/**
20+
* Implementation of the `COMPLETION` processing command.
21+
*
22+
* This class inherits from EsqlBase to make it possible to chain all the commands
23+
* that belong to an ES|QL query in a single expression.
24+
*/
25+
class CompletionCommand extends EsqlBase {
26+
private string $prompt;
27+
private array $named_prompt;
28+
private string $inference_id;
29+
30+
public function __construct(EsqlBase $parent, array $prompt)
31+
{
32+
if (sizeof($prompt) != 1) {
33+
throw new RuntimeException("Only one prompt is supported");
34+
}
35+
parent::__construct($parent);
36+
if ($this->is_named_argument_list($prompt)) {
37+
$this->named_prompt = $prompt;
38+
}
39+
else {
40+
$this->prompt = $prompt[0];
41+
}
42+
}
43+
44+
/**
45+
* Continuation of the `COMPLETION` command.
46+
*
47+
* @param string $inference_id The ID of the inference endpoint to use for
48+
* the task. The inference endpoint must be
49+
* configured with the `completion` task type.
50+
*/
51+
public function with(string $inference_id): CompletionCommand
52+
{
53+
$this->inference_id = $inference_id;
54+
return $this;
55+
}
56+
57+
protected function render_internal(): string
58+
{
59+
if (!$this->inference_id) {
60+
throw new RuntimeException("The completion command requires an inference ID");
61+
}
62+
$with = ["inference_id" => $this->inference_id];
63+
if ($this->named_prompt) {
64+
return "COMPLETION " .
65+
$this->format_id(array_keys($this->named_prompt)[0]) . " = " .
66+
$this->format_id(array_values($this->named_prompt)[0]) .
67+
" WITH " . json_encode($with);
68+
}
69+
else {
70+
return "COMPLETION " . $this->format_id($this->prompt) .
71+
" WITH " . json_encode($with);
72+
}
73+
}
74+
}

src/Helper/Esql/DissectCommand.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 `DISSECT` 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 DissectCommand extends EsqlBase {
24+
private string $input;
25+
private string $pattern;
26+
private string $separator = "";
27+
28+
public function __construct(EsqlBase $parent, string $input, string $pattern)
29+
{
30+
parent::__construct($parent);
31+
$this->input = $input;
32+
$this->pattern = $pattern;
33+
}
34+
35+
/**
36+
* Continuation of the `DISSECT` command.
37+
*
38+
* @param string $separator A string used as the separator between appended
39+
* values, when using the append modifier.
40+
*/
41+
public function append_separator(string $separator): DissectCommand
42+
{
43+
$this->separator = $separator;
44+
return $this;
45+
}
46+
47+
protected function render_internal(): string
48+
{
49+
$sep = $this->separator ? " APPEND_SEPARATOR=" . json_encode($this->separator) : "";
50+
return "DISSECT " . $this->format_id($this->input) . " " . json_encode($this->pattern) . $sep;
51+
}
52+
}

src/Helper/Esql/DropCommand.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 `DROP` 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 DropCommand extends EsqlBase {
24+
private array $columns;
25+
26+
public function __construct(EsqlBase $parent, array $columns)
27+
{
28+
parent::__construct($parent);
29+
$this->columns = $columns;
30+
}
31+
32+
protected function render_internal(): string
33+
{
34+
return "DROP " . implode(
35+
", ", array_map(array($this, "format_id"), $this->columns)
36+
);
37+
}
38+
}

src/Helper/Esql/EnrichCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 `ENRICH` 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 EnrichCommand extends EsqlBase {
24+
private string $policy;
25+
private string $match_field;
26+
private array $fields;
27+
private array $named_fields;
28+
29+
public function __construct(EsqlBase $parent, string $policy)
30+
{
31+
parent::__construct($parent);
32+
$this->policy = $policy;
33+
}
34+
35+
/**
36+
* Continuation of the `ENRICH` command.
37+
*
38+
* @param string $match_field The match field. `ENRICH` uses its value to
39+
* look for records in the enrich index. If not
40+
* specified, the match will be performed on the
41+
* column with the same name as the `match_field`
42+
* defined in the enrich policy.
43+
*/
44+
public function on(string $match_field): EnrichCommand
45+
{
46+
$this->match_field = $match_field;
47+
return $this;
48+
}
49+
50+
/**
51+
* Continuation of the `ENRICH` command.
52+
*
53+
* @param string ...$fields The enrich fields from the enrich index that
54+
* are added to the result as new columns, given
55+
* as positional or named arguments. If a column
56+
* with the same name as the enrich field already
57+
* exists, the existing column will be replaced by
58+
* the new column. If not specified, each of the
59+
* enrich fields defined in the policy is added. A
60+
* column with the same name as the enrich field
61+
* will be dropped unless the enrich field is
62+
* renamed.
63+
*/
64+
public function with(string ...$fields): EnrichCommand
65+
{
66+
if ($this->is_named_argument_list($fields)) {
67+
$this->named_fields = $fields;
68+
}
69+
else {
70+
$this->fields = $fields;
71+
}
72+
return $this;
73+
}
74+
75+
protected function render_internal(): string
76+
{
77+
$on = $this->match_field ? " ON " . $this->format_id($this->match_field) : "";
78+
if ($this->named_fields) {
79+
$items = array_map(
80+
function(string $key): string {
81+
return $this->format_id($key) . " = " .
82+
$this->format_id($this->named_fields[$key]);
83+
},
84+
array_keys($this->named_fields)
85+
);
86+
}
87+
else {
88+
$items = array_map(fn($value): string => $this->format_id($value), $this->fields);
89+
}
90+
return "ENRICH " . $this->policy . implode(", ", $items);
91+
}
92+
}

0 commit comments

Comments
 (0)