|
| 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