Skip to content

Commit f1feaca

Browse files
add remaining commands
1 parent 04d3f81 commit f1feaca

23 files changed

+1553
-0
lines changed

src/Helper/Esql/Branch.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 Branch extends EsqlBase {
18+
protected function render_internal(): string
19+
{
20+
return "";
21+
}
22+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 = "";
27+
private string $pvalue_name = "";
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 key The column with the key to order the values by. If not specified,
39+
* `@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 type_name The name of the output column with the change point type.
51+
* If not specified, `type` is used.
52+
* @param pvalue_name: The name of the output column with the p-value that indicates
53+
* how extreme the change point is. If not specified, `pvalue` is used.
54+
*/
55+
public function as(string $type_name, string $pvalue_name): ChangePointCommand
56+
{
57+
$this->type_name = $type_name;
58+
$this->pvalue_name = $pvalue_name;
59+
return $this;
60+
}
61+
62+
protected function render_internal(): string
63+
{
64+
$key = $this->key ? " ON " . $this->format_id($this->key) : "";
65+
$names = ($this->type_name && $this->pvalue_name) ? " AS " . $this->format_id($this->type_name || "type") . ", " . $this->format_id($this->pvalue_name || "value") : "";
66+
return "CHANGE_POINT " . $this->value . $key . $names;
67+
}
68+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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, string ...$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 inference_id The ID of the inference endpoint to use for the task. The
48+
* inference endpoint must be configured with the completion task type.
49+
*/
50+
public function with(string $inference_id): CompletionCommand
51+
{
52+
$this->inference_id = $inference_id;
53+
return $this;
54+
}
55+
56+
protected function render_internal(): string
57+
{
58+
if (!$this->inference_id) {
59+
throw new RuntimeException("The completion command requires an inference ID");
60+
}
61+
$with = ["inference_id" => $this->inference_id];
62+
if ($this->named_prompt) {
63+
return "COMPLETION " .
64+
$this->format_id(array_keys($this->prompt)[0]) . " = " .
65+
$this->format_id(array_values($this>prompt)[0]) .
66+
" WITH " . json_encode($with);
67+
}
68+
else {
69+
return "COMPLETION " . $this->format_id($this>prompt) .
70+
" WITH " . json_encode($with);
71+
}
72+
}
73+
}

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, array $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 separator A string used as the separator between appended values,
39+
* 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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(", ", array_map($this->format_id, $this->columns));
35+
}
36+
}

src/Helper/Esql/EnrichCommand.php

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

0 commit comments

Comments
 (0)