Skip to content

[ES|QL][Lens] Improves the ES|QL suggestions logic when a query changes#231767

Merged
stratoula merged 32 commits intoelastic:mainfrom
stratoula:lens-esql-improve-table-suggestions
Sep 10, 2025
Merged

[ES|QL][Lens] Improves the ES|QL suggestions logic when a query changes#231767
stratoula merged 32 commits intoelastic:mainfrom
stratoula:lens-esql-improve-table-suggestions

Conversation

@stratoula
Copy link
Contributor

@stratoula stratoula commented Aug 14, 2025

Summary

Improves this #220939

meow

How it improves it:

  1. When the user edits an existing chart, we try to keep the chart type the viz has as long as the query returns compatible columns. If the user changes the chart type from the dropdown then this means he deliberately wants to change it and we try to respect the new choice.
  2. When the user creates a new chart we are following the suggestions until the user starts changing something in the visualization parameters panel. Then we are assuming that the user wants this chart type and we try to respect it (according to this comment). If the user changes the chart type from the dropdown then we try to respect this choice.
  3. There is a special treatment for tables as they are not suggested from the suggestions api (they are always hidden)
  4. The above work as long as the query doesnt change significantly. When the query changes significantly there is the possibility that the columns used in the chart are not being returned from the _query api. For example if I have a stats by date_histogram and I change to from index you have now all index columns but not the histogram buckets, this will reset. In the future we should inform the user that this change will reset the visualization.

TL;DR

  • If the user edits an existing chart then we try to respect the chart type defined there
  • If the user creates a new one we follow the suggestions as long as the user hasn't changed something in the visualization settings panel
  • If the user changes the query and the returned columns don't contain the visualization columns we reset. Here we should inform the users in the future

Checklist

let queryColumns = results.response.columns;
// if the query columns are empty, we need to use the all_columns property
// which has all columns regardless if they have data or not
if (queryColumns.length === 0 && results.response.all_columns) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sure that all columns are returned if they exist in the payload. This ensures that the textBasedColumns that are passed in the api has all columns with or without data.

layer.columns?.some(
(c: { fieldName: string }) =>
!context?.textBasedColumns?.find((col) => col.id === c.fieldName)
) || layer.columns?.length !== context?.textBasedColumns?.length
Copy link
Contributor Author

@stratoula stratoula Aug 14, 2025

Choose a reason for hiding this comment

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

I removed the columns length check, we dont want to revert if the user selected different columns as long as they exist in the response

});
});

describe('injectESQLQueryIntoLensLayers', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move this to suggestions api tests

}
return undefined;
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moves this into suggestions api logic

!context?.textBasedColumns?.find((col) => col.id === c.fieldName)
) || layer.columns?.length !== context?.textBasedColumns?.length
// Check if index patterns match when context has a query
if (context && 'query' in context && context.query && 'esql' in context.query) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we will return the suggestion if the user changed datasource

@stratoula stratoula added the Feature:ES|QL ES|QL related features in Kibana label Aug 18, 2025
@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🎉 All tests passed! - kibana-flaky-test-suite-runner#9164

[✅] x-pack/platform/test/functional/apps/lens/group7/config.ts: 50/50 tests passed.

see run history

@stratoula stratoula changed the title [ES|QL][Lens] Improves the datatable suggestions Aug 20, 2025
@stratoula stratoula added backport:skip This PR does not require backporting v9.2.0 Team:Visualizations Team label for Lens, elastic-charts, Graph, legacy editors (TSVB, Visualize, Timelion) t// Feature:Lens labels Aug 25, 2025
@stratoula stratoula marked this pull request as ready for review August 25, 2025 09:46
@stratoula stratoula requested a review from a team as a code owner August 25, 2025 09:46
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-visualizations (Team:Visualizations)

* Get the user's preferred chart type from localStorage
*/
export const getUserChartTypeFromLocalStorage = (): string | null => {
const storage = new Storage(localStorage);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to use the sessionStorage instead of the localStorage to avoid conflicts and changes of the same variable across different tabs.
I tested and clicking Close on a tab besically removes the currently set viz type from the other tab and viceversa.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I will address tomorrow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Marco changed it to use session storage instead 211cf65

Copy link
Contributor

Choose a reason for hiding this comment

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

Tested few cases here, but the totality of the bugs I've found are tied to the lack of handling of the ESC key down event on the new logic.

Typical bug scenario:

  • create a ES|QL chart on a dashboard
  • close the inline initial editing
  • reopen the inline editor
  • tweak the query or change the chart type
  • now press ESC
  • the chart is saved in the current state

A a user the ESC button is a tricky point: it "sounds" something similar to a Cancel than a Save logically, but I can see how much of a slippery slope that can be.

Maybe pressing ESC should have a confirmation modal before cancelling? cc @gvnmagni

Copy link
Contributor Author

@stratoula stratoula Sep 9, 2025

Choose a reason for hiding this comment

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

The ESC works the same in the main (I mean it applies the changes). You mean that it doesnt reset the session? I don't think it will be a problem mostly because when you edit another panel it will add to the session storage the new chart type.

Which bug have you found that is related with the ESC button functionality and the changes this PR is introducing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Synced with Marco offline, I can reset on ESC, there is a keydown handler already

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

I didn't know we had added this handler, thanx for pointing out Marco!

stratoula and others added 4 commits September 9, 2025 12:59
…dit_on_the_fly/helpers.ts

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
…line_editing/setup_inline_editing.tsx

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
lens 1414 1415 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
lens 1.5MB 1.5MB +904.0B

History

@stratoula stratoula enabled auto-merge (squash) September 10, 2025 08:10
@stratoula stratoula merged commit c1f9728 into elastic:main Sep 10, 2025
12 checks passed
eleonoramicozzi pushed a commit to eleonoramicozzi/kibana that referenced this pull request Sep 10, 2025
…es (elastic#231767)

## Summary

Improves this elastic#220939


![meow](https://github.com/user-attachments/assets/87f68a1a-1110-4969-9f93-b5fa1c740c89)


How it improves it:

1. When the user edits an existing chart, we try to keep the chart type
the viz has as long as the query returns compatible columns. If the user
changes the chart type from the dropdown then this means he deliberately
wants to change it and we try to respect the new choice.
2. When the user creates a new chart we are following the suggestions
until the user starts changing something in the visualization parameters
panel. Then we are assuming that the user wants this chart type and we
try to respect it (according to this
[comment](elastic#220939 (comment))).
If the user changes the chart type from the dropdown then we try to
respect this choice.
4. There is a special treatment for tables as they are not suggested
from the suggestions api (they are always hidden)
5. **The above work as long as the query doesnt change significantly**.
When the query changes significantly there is the possibility that the
columns used in the chart are not being returned from the _query api.
For example if I have a stats by date_histogram and I change to `from
index` you have now all index columns but not the histogram buckets,
this will reset. In the future we should inform the user that this
change will reset the visualization.


TL;DR

- If the user edits an existing chart then we try to respect the chart
type defined there
- If the user creates a new one we follow the suggestions as long as the
user hasn't changed something in the visualization settings panel
- If the user changes the query and the returned columns don't contain
the visualization columns we reset. Here we should inform the users in
the future


### Checklist
- [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

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
KodeRad pushed a commit to KodeRad/kibana that referenced this pull request Sep 15, 2025
…es (elastic#231767)

## Summary

Improves this elastic#220939


![meow](https://github.com/user-attachments/assets/87f68a1a-1110-4969-9f93-b5fa1c740c89)


How it improves it:

1. When the user edits an existing chart, we try to keep the chart type
the viz has as long as the query returns compatible columns. If the user
changes the chart type from the dropdown then this means he deliberately
wants to change it and we try to respect the new choice.
2. When the user creates a new chart we are following the suggestions
until the user starts changing something in the visualization parameters
panel. Then we are assuming that the user wants this chart type and we
try to respect it (according to this
[comment](elastic#220939 (comment))).
If the user changes the chart type from the dropdown then we try to
respect this choice.
4. There is a special treatment for tables as they are not suggested
from the suggestions api (they are always hidden)
5. **The above work as long as the query doesnt change significantly**.
When the query changes significantly there is the possibility that the
columns used in the chart are not being returned from the _query api.
For example if I have a stats by date_histogram and I change to `from
index` you have now all index columns but not the histogram buckets,
this will reset. In the future we should inform the user that this
change will reset the visualization.


TL;DR

- If the user edits an existing chart then we try to respect the chart
type defined there
- If the user creates a new one we follow the suggestions as long as the
user hasn't changed something in the visualization settings panel
- If the user changes the query and the returned columns don't contain
the visualization columns we reset. Here we should inform the users in
the future


### Checklist
- [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

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Sep 24, 2025
…es (elastic#231767)

## Summary

Improves this elastic#220939


![meow](https://github.com/user-attachments/assets/87f68a1a-1110-4969-9f93-b5fa1c740c89)


How it improves it:

1. When the user edits an existing chart, we try to keep the chart type
the viz has as long as the query returns compatible columns. If the user
changes the chart type from the dropdown then this means he deliberately
wants to change it and we try to respect the new choice.
2. When the user creates a new chart we are following the suggestions
until the user starts changing something in the visualization parameters
panel. Then we are assuming that the user wants this chart type and we
try to respect it (according to this
[comment](elastic#220939 (comment))).
If the user changes the chart type from the dropdown then we try to
respect this choice.
4. There is a special treatment for tables as they are not suggested
from the suggestions api (they are always hidden)
5. **The above work as long as the query doesnt change significantly**.
When the query changes significantly there is the possibility that the
columns used in the chart are not being returned from the _query api.
For example if I have a stats by date_histogram and I change to `from
index` you have now all index columns but not the histogram buckets,
this will reset. In the future we should inform the user that this
change will reset the visualization.


TL;DR

- If the user edits an existing chart then we try to respect the chart
type defined there
- If the user creates a new one we follow the suggestions as long as the
user hasn't changed something in the visualization settings panel
- If the user changes the query and the returned columns don't contain
the visualization columns we reset. Here we should inform the users in
the future


### Checklist
- [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

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
niros1 pushed a commit that referenced this pull request Sep 30, 2025
…es (#231767)

## Summary

Improves this #220939


![meow](https://github.com/user-attachments/assets/87f68a1a-1110-4969-9f93-b5fa1c740c89)


How it improves it:

1. When the user edits an existing chart, we try to keep the chart type
the viz has as long as the query returns compatible columns. If the user
changes the chart type from the dropdown then this means he deliberately
wants to change it and we try to respect the new choice.
2. When the user creates a new chart we are following the suggestions
until the user starts changing something in the visualization parameters
panel. Then we are assuming that the user wants this chart type and we
try to respect it (according to this
[comment](#220939 (comment))).
If the user changes the chart type from the dropdown then we try to
respect this choice.
4. There is a special treatment for tables as they are not suggested
from the suggestions api (they are always hidden)
5. **The above work as long as the query doesnt change significantly**.
When the query changes significantly there is the possibility that the
columns used in the chart are not being returned from the _query api.
For example if I have a stats by date_histogram and I change to `from
index` you have now all index columns but not the histogram buckets,
this will reset. In the future we should inform the user that this
change will reset the visualization.


TL;DR

- If the user edits an existing chart then we try to respect the chart
type defined there
- If the user creates a new one we follow the suggestions as long as the
user hasn't changed something in the visualization settings panel
- If the user changes the query and the returned columns don't contain
the visualization columns we reset. Here we should inform the users in
the future


### Checklist
- [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

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting Feature:ES|QL ES|QL related features in Kibana Feature:Lens needs_docs release_note:enhancement Team:Visualizations Team label for Lens, elastic-charts, Graph, legacy editors (TSVB, Visualize, Timelion) t// v9.2.0

6 participants