Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Helper/Esql/EsqlBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

abstract class EsqlBase {
private ?EsqlBase $parent = null;

protected function format_kv(array $map): string
{
return implode(", ", array_map(
function($k, $v) {
return $k . "=" . json_encode($v);
},
array_keys($map),
$map,
));
}

public function render(): string
{
$query = "";
if ($this->parent) {
$query .= $this->parent->render() . "\n| ";
}
$query .= $this->render_internal();
return $query;
}

protected abstract function render_internal(): string;

public function __construct(EsqlBase $parent)
{
$this->parent = $parent;
}

public function __toString(): string
{
return $this->render() . "\n";
}

public function limit(int $maxNumberOfRows): LimitCommand
{
return new LimitCommand($this, $maxNumberOfRows);
}

public function where(string ...$expressions): WhereCommand
{
return new WhereCommand($this, $expressions);
}
}
29 changes: 29 additions & 0 deletions src/Helper/Esql/FromCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

class FromCommand extends EsqlBase {
private array $indices;

public function __construct(array $indices)
{
$this->indices = $indices;
}

protected function render_internal(): string
{
return "FROM " . implode(", ", $this->indices);
}
}
30 changes: 30 additions & 0 deletions src/Helper/Esql/LimitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

class LimitCommand extends EsqlBase {
private int $maxNumberOfRows;

public function __construct(EsqlBase $parent, int $maxNumberOfRows)
{
parent::__construct($parent);
$this->maxNumberOfRows = $maxNumberOfRows;
}

protected function render_internal(): string
{
return "LIMIT " . json_encode($this->maxNumberOfRows);
}
}
27 changes: 27 additions & 0 deletions src/Helper/Esql/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

abstract class Query {
public static function from(string ...$indices): FromCommand
{
return new FromCommand($indices);
}

public static function row(string ...$params): RowCommand
{
return new RowCommand($params);
}
}
29 changes: 29 additions & 0 deletions src/Helper/Esql/RowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

class RowCommand extends EsqlBase {
private array $params;

public function __construct(array $params)
{
$this->params = $params;
}

protected function render_internal(): string
{
return "ROW " . $this->format_kv($this->params);
}
}
30 changes: 30 additions & 0 deletions src/Helper/Esql/WhereCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Helper\Esql;

class WhereCommand extends EsqlBase {
private array $expressions;

public function __construct(EsqlBase $parent, array $expressions)
{
parent::__construct($parent);
$this->expressions = $expressions;
}

protected function render_internal(): string
{
return "WHERE " . implode(" AND ", $this->expressions);
}
}