Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES|QL kql function. #116764

Merged
merged 37 commits into from
Nov 25, 2024
Merged

ES|QL kql function. #116764

merged 37 commits into from
Nov 25, 2024

Conversation

afoucret
Copy link
Contributor

@afoucret afoucret commented Nov 13, 2024

This PR introduces support for the KQL query language as an ES|QL function.
Our main goal is to provides users a way forward to migrate from KQL to ES|QL by integrating KQL queries directly into ES|QL queries.

We recently added the support of KQL directly form the query DSL:

GET movies/_search
{
  "query": {
    "kql": {
      "query": "actors: \"Kurt Russel\" AND title: \"Big trouble\""
    }
  }
}

This PR expose this new capacity of the query DSL through a new KQL fulltext filtering function

POST _query
{
  "query": """
    FROM movies
    | WHERE kql("actors: \"Kurt Russel\" AND title: \"Big trouble\"")
  """
}

The KQL function is conceptually very similar to the QSTR function and come with the same limitations.

The new function is enabled only in snapshots builds (same for the KQL query in the query DSL)

Copy link
Contributor

Documentation preview:

@afoucret afoucret force-pushed the esql-kql-function branch 2 times, most recently from e13af7c to 44bb5a0 Compare November 14, 2024 11:10
@afoucret afoucret added :Analytics/ES|QL AKA ESQL :Search Relevance/Search Catch all for Search Relevance v8.17.0 >non-issue auto-backport Automatically create backport pull requests when merged labels Nov 19, 2024
Copy link
Member

@fang-xing-esql fang-xing-esql left a comment

Choose a reason for hiding this comment

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

Thanks for putting these together @afoucret ! It looks pretty good to me, I added some comments.

Here are my understanding of the KQL function, and please correct me if I missed anything:

  • In terms of the interface, the KQL function is very similar to QSTR so it makes sense to follow a similar approach as QSTR to support it in the ES|QL.
  • However they are different in syntax according to this google doc, although their syntax look very similar to me, I won't be surprised that I might make mistakes on the syntax when using them. From users perspective, it will be helpful to have a page(or a link to existing ES docs) in the ES|QL doc to describe when to use KQL and when to use QSTR, and have examples to describe the usages. Especially they are now both available through ES|QL we may want to suggest the best practice to reduce confusions. Perhaps it is only me, but I'm not clear when I should use which function. This is not a needed for a snapshot function, but it will be helpful to get us prepared for a tech preview or GA.
  • I suggest to add ES|QL-ui label to notify kibana about this new function, it is near the FF of 8.17, it will be nice to give them a heads up if this is targeting to 8.17.
  • I suggest to add test-release label to run this through release tests, sometimes it does catch surprise for us.

@@ -788,6 +789,14 @@ private static void checkFullTextQueryFunctions(LogicalPlan plan, Set<Failure> f
qsf -> "[" + qsf.functionName() + "] " + qsf.functionType(),
failures
);
checkCommandsBeforeExpression(
Copy link
Member

Choose a reason for hiding this comment

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

We could do a refactor here to reduce repetitive code, if Kql and QueryString have the same restrictions, they can be added in a collection and call checkCommandsBeforeExpression in a loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a loop that remove the duplicated code.

@@ -409,7 +410,8 @@ private static FunctionDefinition[][] snapshotFunctions() {
// This is an experimental function and can be removed without notice.
def(Delay.class, Delay::new, "delay"),
def(Categorize.class, Categorize::new, "categorize"),
def(Rate.class, Rate::withUnresolvedTimestamp, "rate") } };
def(Rate.class, Rate::withUnresolvedTimestamp, "rate"),
def(Kql.class, Kql::new, "kql") } };
Copy link
Member

Choose a reason for hiding this comment

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

Shall we follow the alphabetical order here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you.

Comment on lines 63 to 66
@Override
public String functionName() {
return "KQL";
}
Copy link
Member

Choose a reason for hiding this comment

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

The override to functionName can be skipped, as the function name is the same as class name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the override.

import java.util.List;
import java.util.function.Supplier;

@FunctionName("kql")
Copy link
Member

Choose a reason for hiding this comment

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

This annotation can be skipped, as the class name is the same as function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the annotation.

@afoucret afoucret added the test-release Trigger CI checks against release build label Nov 20, 2024
@afoucret afoucret added the ES|QL-ui Impacts ES|QL UI label Nov 21, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/kibana-esql (ES|QL-ui)

@afoucret
Copy link
Contributor Author

@fang-xing-esql Thanks for the detailed review and feedback!

You're absolutely right that the KQL and QSTR functions are quite similar.
Both allow to leverage the _search infrastructure to execute another query language (respectively the Kibana Query Language and Lucene query string).

That said, for KQL, our intention is not to promote it as a standalone feature but rather to facilitate users migrating to ES|QL. This is why we’ve kept communication about it relatively low-key.
I will work on enriching the documentation by adding a link to the existing KQL page.

Few other points:

  • I updated the code per your suggestions.
  • I added the test-release label to ensure those tests are sucessful
  • I updated the version label to target 8.18.0 instead of 8.17.0
  • I added the ES|QL-ui label to keep the Kibana team informed of the progress

Copy link
Member

@fang-xing-esql fang-xing-esql left a comment

Choose a reason for hiding this comment

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

Thank you @afoucret! LGTM. And thanks to @carlosdelest for reviewing as well.

@afoucret afoucret requested review from a team as code owners November 22, 2024 13:59
@afoucret afoucret merged commit ff58d89 into elastic:main Nov 25, 2024
17 checks passed
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

afoucret added a commit to afoucret/elasticsearch that referenced this pull request Nov 25, 2024
elasticsearchmachine pushed a commit that referenced this pull request Nov 25, 2024
* ES|QL kql function. (#116764)

* Fix test compile error in branch 8.x
alexey-ivanov-es pushed a commit to alexey-ivanov-es/elasticsearch that referenced this pull request Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged ES|QL-ui Impacts ES|QL UI >non-issue :Search Relevance/Search Catch all for Search Relevance Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch test-release Trigger CI checks against release build v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants