Skip to content

Conversation

@miguelgrinberg
Copy link
Contributor

@miguelgrinberg miguelgrinberg commented Sep 10, 2025

This PR contains a prototype of an ES|QL query builder feature for the PHP client based on the one I built for the Python client.

Example usage:

$query = Esql\Query::from("books", "books*")
    ->where('author == "King"', 'year == 1982')
    ->limit(10);
echo $query;

Output:

FROM books, books*
| WHERE author == "King" AND year == 1982
| LIMIT 10

In IDEs, you get correct suggestions based on context. For example:

Screenshot 2025-09-11 at 2 43 24 PM

@ezimuel
Copy link
Contributor

ezimuel commented Sep 10, 2025

@miguelgrinberg thanks for the PR it looks interesting. The only comment that I've at the moment is to split each class in a separate file. The name of the file must be the same of the class. This is a best practices in PHP .

@miguelgrinberg
Copy link
Contributor Author

miguelgrinberg commented Sep 11, 2025

@ezimuel I refactored into multiple files (one per class) and added a couple more commands now. Let me know if this structure seems reasonable and I'll add the remaining commands and all the docs.

Two usage examples:

use Elastic\Elasticsearch\Helper\Esql;

$query = Esql\Query::from("books", "books*")
    ->where('author == "King"', 'year == 1982')
    ->limit(10);
echo $query;

$query = Esql\Query::row(a: 123, b: "test")
    ->limit(1);
echo $query;

@miguelgrinberg miguelgrinberg force-pushed the esql-query-builder branch 2 times, most recently from b54a783 to c602ff7 Compare October 7, 2025 17:33
@miguelgrinberg miguelgrinberg marked this pull request as ready for review October 8, 2025 16:02
@miguelgrinberg miguelgrinberg requested a review from a team as a code owner October 8, 2025 16:02
@miguelgrinberg
Copy link
Contributor Author

miguelgrinberg commented Oct 8, 2025

@ezimuel This is ready to review!

Maybe what makes the most sense is for you to read the unit test file first, so that you see how all the commands are constructed.

Copy link
Contributor

@ezimuel ezimuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @miguelgrinberg for this awesome PR! I just left a couple of comments, the most relevant that I would suggest to investigate is the possibility to use Traits instead of passing the parent in EsqlBase.
Moreover, we should add also some documentation about this helper with an example on how to call it using the Esql endpoint.

/**
* Helper function that checks if a forking command has been issued already.
*/
protected function isForked(): bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can avoid using this function and avoid to pass the parent reference for each class using a Trait here. I used this mechanism for code reuse in the AbstractEndpoint to share common code on each Endpoint implementation.

@github-actions
Copy link

github-actions bot commented Nov 6, 2025

🔍 Preview links for changed docs

@miguelgrinberg miguelgrinberg changed the title ESQL query builder prototype ESQL query builder Nov 6, 2025
@miguelgrinberg miguelgrinberg changed the title ESQL query builder ES|QL query builder Nov 6, 2025
@miguelgrinberg miguelgrinberg merged commit a42e2dd into main Nov 6, 2025
32 checks passed
@miguelgrinberg miguelgrinberg deleted the esql-query-builder branch November 6, 2025 12:12
@github-actions
Copy link

github-actions bot commented Nov 6, 2025

The backport to 9.2 failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-9.2 9.2
# Navigate to the new working tree
cd .worktrees/backport-9.2
# Create a new branch
git switch --create backport-1462-to-9.2
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 a42e2dd906dbb27f7a263ae28538608463422363
# Push it to GitHub
git push --set-upstream origin backport-1462-to-9.2
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-9.2

Then, create a pull request where the base branch is 9.2 and the compare/head branch is backport-1462-to-9.2.

miguelgrinberg added a commit that referenced this pull request Nov 6, 2025
ES|QL query builder

(cherry picked from commit a42e2dd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants