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

[KQL] Generate expression string from AST node #77971

Closed
darnautov opened this issue Sep 19, 2020 · 7 comments · Fixed by #161601
Closed

[KQL] Generate expression string from AST node #77971

darnautov opened this issue Sep 19, 2020 · 7 comments · Fixed by #161601
Assignees
Labels
enhancement New value added to drive a business result Feature:KQL KQL Feature:Search Querying infrastructure in Kibana impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. loe:medium Medium Level of Effort Team:DataDiscovery Discover App Team (Document Explorer, Saved Search, Surrounding documents, Data, DataViews)

Comments

@darnautov
Copy link
Contributor

darnautov commented Sep 19, 2020

Describe the feature:

The @kbn/es-query KQL utilities currently exports these methods:

  • fromKueryExpression: Receives a KQL expression and returns the generated KQL AST.
  • toElasticsearchQuery: Receives KQL AST and returns the generated Elasticsearch query.

There is no current utility for taking KQL AST and generating the KQL expression (toKueryExpression).

Describe a specific use case for the feature:

In the ML plugin, we allow users to link anomaly records with the other Kibana pages by configuring custom URLs. Most of those URLs contain a kuery string to populate a KQL bar with a query on the target page. At the moment we parse a URL template, replace tokens with values from the anomaly record and generate a result query using regular expressions. With all possible special characters and escaping rules, the current solution has potential issues. Adjusting KueryNode and receiving a result kuery expression seems like a more robust solution in this case.

@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app-arch (Team:AppArch)

@lukasolson lukasolson added Feature:KQL KQL enhancement New value added to drive a business result labels Mar 11, 2021
@exalate-issue-sync exalate-issue-sync bot added impact:low Addressing this issue will have a low level of impact on the quality/strength of our product. loe:small Small Level of Effort labels Jun 2, 2021
@qn895
Copy link
Member

qn895 commented Dec 16, 2021

Revisiting this issue and the ML team thinks that we should escalate the impact level to something higher. As integration between different teams increases, having a one source of truth to build and generate the query expressions will provide better consistency, reduce brittle code (e.g. regex expressions 👀 ), and lead to less potential bugs.

@exalate-issue-sync exalate-issue-sync bot added impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. loe:medium Medium Level of Effort and removed impact:low Addressing this issue will have a low level of impact on the quality/strength of our product. loe:small Small Level of Effort labels Dec 17, 2021
@Dosant
Copy link
Contributor

Dosant commented Dec 17, 2021

impact:low was an integration issue. It was our default until triaged properly :(

Revisiting this issue and the ML team thinks that we should escalate the impact level to something higher. As integration between different teams increases, having a one source of truth to build and generate the query expressions will provide better consistency, reduce brittle code (e.g. regex expressions 👀 ), and lead to less potential bugs.

Could you point me to that code as an example please?

@qn895
Copy link
Member

qn895 commented Dec 20, 2021

Hi @Dosant, an example of is in Machine Learning's Anomaly detection/alerts where we allow users to add custom url templates to different parts of Kibana (like Discover, Dashboard, Metrics, APM, etc.) I also want to clarify that this chunk of code was written a long time ago, so perhaps there are already better mechanisms in place to help achieve this functionality.

The url template looks something like:

dashboards#/view/351de820-f2bb-11ea-ab06-cb93221707e9?_a=(filters:!(),query:(language:kuery,query:'at@name:"$at@name$" and singlequote!'name:"$singlequote!'name$"'))&_g=(filters:!(),time:(from:'$earliest$',mode:absolute,to:'$latest$'))

Which we then substitute the tokens like $singlequote!'name$ or $earliest$ to real-time values at the time of which the url is opened. As you can see it can get pretty complicated to unpack when the query is complex. This gets extra complicated for us especially because kuery and lucene have different escaping rules for different special characters.

So just one use case but I bet there are other solutions who might benefit from the ability to build expressions from the nodes.

@ppisljar
Copy link
Member

cc @vadimkibana i think for bulding custom url templates to different parts of kibana url locators should be used

@lukasolson lukasolson changed the title Building a kuery expression from the KueryNode [KQL] Add support for converting a node to an expression Apr 4, 2022
@exalate-issue-sync exalate-issue-sync bot changed the title [KQL] Add support for converting a node to an expression Building a kuery expression from the KueryNode Apr 4, 2022
@lukasolson lukasolson changed the title Building a kuery expression from the KueryNode [KQL] Generate expression string from AST node Apr 6, 2022
@petrklapka petrklapka added Feature:Search Querying infrastructure in Kibana Team:DataDiscovery Discover App Team (Document Explorer, Saved Search, Surrounding documents, Data, DataViews) and removed Team:AppServicesSv labels Nov 23, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-data-discovery (Team:DataDiscovery)

lukasolson added a commit that referenced this issue Jul 14, 2023
## Summary

Resolves #77971.

Adds a `toKqlExpression` method to the `@kbn/es-query` that allows
generating a KQL expression from an AST node.

Example:

```ts
const node = fromKueryExpression('extension: "jpg"');
const kql = toKqlExpression(node); // 'extension: "jpg"'
```

Note that the generated KQL expression may not exactly match the
original text (whitespace is not preserved, parentheses may be added,
etc.).

### Checklist

Delete any items that are not applicable to this PR.

- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
@darnautov
Copy link
Contributor Author

many thanks @lukasolson 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New value added to drive a business result Feature:KQL KQL Feature:Search Querying infrastructure in Kibana impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. loe:medium Medium Level of Effort Team:DataDiscovery Discover App Team (Document Explorer, Saved Search, Surrounding documents, Data, DataViews)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants