Skip to content

[Metrics][Discover] Fix fields dropdown by casting dimension fields to string#242432

Merged
MiriamAparicio merged 12 commits intoelastic:mainfrom
MiriamAparicio:239952-cast-dimensions-to-string
Nov 13, 2025
Merged

[Metrics][Discover] Fix fields dropdown by casting dimension fields to string#242432
MiriamAparicio merged 12 commits intoelastic:mainfrom
MiriamAparicio:239952-cast-dimensions-to-string

Conversation

@MiriamAparicio
Copy link
Copy Markdown
Contributor

@MiriamAparicio MiriamAparicio commented Nov 10, 2025

Closes #239952

Summary

This PR resolves the data inconsistency between metric charts and the "Filter by value" dropdown, where values visible in charts were missing from the filter list.

Problem

The root cause was that the same dimension field (e.g., attributes.cpu) could have different data types across different indices:

  • long in one index
  • keyword in another index

Elasticsearch aggregations and ES|QL queries fail when trying to process fields with inconsistent types, resulting in incomplete dimension values in the dropdown.

Solution

1. Updated get_dimentions.ts:

  • Migrated from Elasticsearch _search API with terms aggregations to ES|QL
  • Added explicit type casting to string using ::STRING syntax
  • Increased dimension value limit from 20 to 1000
  • Simplified response mapping logic

2. Updated create_esql_query.ts:

  • Added type casting logic for dimensions in WHERE clauses
  • Applied the same needsStringCasting() check used in CONCAT operations
  • Ensures consistent string type handling for null checks

BEFORE
image

AFTER
Screenshot 2025-11-10 at 14 37 35

@MiriamAparicio MiriamAparicio requested a review from a team November 10, 2025 14:51
@MiriamAparicio MiriamAparicio added release_note:skip Skip the PR/issue when compiling release notes backport:version Backport to applied version labels v9.2.0 v9.3.0 v9.2.1 Team:obs-exploration Observability Exploration team labels Nov 10, 2025
@jennypavlova jennypavlova self-requested a review November 10, 2025 16:13
Copy link
Copy Markdown
Member

@jennypavlova jennypavlova left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!
Just a question, when I tested with edge lite data, I saw that if we have the 4 values but the charts are supporting 2 each, I saw an error when filtered by it in the charts where the value is not present:
first_values
second_values
error_filter_

Is that expected, or maybe a data issue on my end?

const source = fromCommand(indices);
const query = source
.pipe(
evaluate('??dim = ??dim::string', { dim: dimensions[0] }),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For now, it's ok, but in the future, we might support multi-dimensions, and we'll have to rethink this query.

dim: dimensions[0],
}),
sort('??dim', { dim: dimensions[0] }),
limit(1000)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we increase the number of items returned?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't know, it's what I saw on your POC, should I keep it to 20 then?

@MiriamAparicio
Copy link
Copy Markdown
Contributor Author

@jennypavlova I couldn't reproduce the error, but I will check as @crespocarlos mention to add it also for the IN clause
image

@MiriamAparicio MiriamAparicio force-pushed the 239952-cast-dimensions-to-string branch from 59559f9 to f5071f6 Compare November 11, 2025 11:20
Copy link
Copy Markdown
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

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

LGTM

@MiriamAparicio MiriamAparicio force-pushed the 239952-cast-dimensions-to-string branch from 101a7a6 to 751603a Compare November 11, 2025 12:54
@MiriamAparicio MiriamAparicio force-pushed the 239952-cast-dimensions-to-string branch from ed9f7b2 to b69c0e9 Compare November 11, 2025 15:28
@@ -0,0 +1,307 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We already have a functional test covering this, btw.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, the one failing 😅
I can remove delete this unit test then

const response = await esClient.esql(
'get_dimensions',
{
query: query.toString(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we already call toString() on line 47. Do we need this one too?

@crespocarlos crespocarlos requested a review from a team as a code owner November 12, 2025 14:36
@MiriamAparicio
Copy link
Copy Markdown
Contributor Author

Thanks @crespocarlos for the fix, I tested it and run the api test and works as expected

@elasticmachine
Copy link
Copy Markdown
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #12 / Alerting alerts_as_data alerts as data flapping should drop tracked alerts early after hitting the alert limit
  • [job] [logs] FTR Configs #26 / Search Profiler Profile should return profile results for a valid index

Metrics [docs]

Async chunks

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

id before after diff
discover 1.2MB 1.2MB +114.0B

History

@MiriamAparicio MiriamAparicio merged commit 08460f6 into elastic:main Nov 13, 2025
12 checks passed
@kibanamachine
Copy link
Copy Markdown
Contributor

Starting backport for target branches: 9.2

https://github.com/elastic/kibana/actions/runs/19327018702

@kibanamachine
Copy link
Copy Markdown
Contributor

💔 All backports failed

Status Branch Result
9.2 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 242432

Questions ?

Please refer to the Backport tool documentation

@MiriamAparicio
Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
9.2

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

MiriamAparicio added a commit to MiriamAparicio/kibana that referenced this pull request Nov 14, 2025
…o string (elastic#242432)

Closes elastic#239952

### Summary
This PR resolves the data inconsistency between metric charts and the
"Filter by value" dropdown, where values visible in charts were missing
from the filter list.

#### Problem

The root cause was that the same dimension field (e.g.,
`attributes.cpu`) could have different data types across different
indices:
- `long` in one index
- `keyword` in another index

Elasticsearch aggregations and ES|QL queries fail when trying to process
fields with inconsistent types, resulting in incomplete dimension values
in the dropdown.

#### Solution

**1. Updated `get_dimentions.ts`:**
- Migrated from Elasticsearch `_search` API with `terms` aggregations to
ES|QL
- Added explicit type casting to string using `::STRING` syntax
- Increased dimension value limit from 20 to 1000
- Simplified response mapping logic

**2. Updated `create_esql_query.ts`:**
- Added type casting logic for dimensions in `WHERE` clauses
- Applied the same `needsStringCasting()` check used in `CONCAT`
operations
- Ensures consistent string type handling for null checks

BEFORE
<img width="2304" height="560" alt="image"
src="https://github.com/user-attachments/assets/e74c73af-838e-4282-b6f7-9778f03e562d"
/>

AFTER
<img width="1043" height="292" alt="Screenshot 2025-11-10 at 14 37 35"
src="https://github.com/user-attachments/assets/ade48ce5-1290-4fc7-afb4-57adcf3ebaf3"
/>

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Carlos Crespo <carloshenrique.leonelcrespo@elastic.co>
(cherry picked from commit 08460f6)

# Conflicts:
#	src/platform/packages/shared/kbn-traced-es-client/src/create_traced_es_client.ts
#	src/platform/plugins/shared/metrics_experience/tsconfig.json
@MiriamAparicio MiriamAparicio added backport:skip This PR does not require backporting and removed backport:version Backport to applied version labels v9.2.0 v9.2.1 labels Nov 14, 2025
eokoneyo pushed a commit to eokoneyo/kibana that referenced this pull request Dec 2, 2025
…o string (elastic#242432)

Closes elastic#239952

### Summary
This PR resolves the data inconsistency between metric charts and the
"Filter by value" dropdown, where values visible in charts were missing
from the filter list.

#### Problem

The root cause was that the same dimension field (e.g.,
`attributes.cpu`) could have different data types across different
indices:
- `long` in one index
- `keyword` in another index

Elasticsearch aggregations and ES|QL queries fail when trying to process
fields with inconsistent types, resulting in incomplete dimension values
in the dropdown.

#### Solution

**1. Updated `get_dimentions.ts`:**
- Migrated from Elasticsearch `_search` API with `terms` aggregations to
ES|QL
- Added explicit type casting to string using `::STRING` syntax
- Increased dimension value limit from 20 to 1000
- Simplified response mapping logic

**2. Updated `create_esql_query.ts`:**
- Added type casting logic for dimensions in `WHERE` clauses
- Applied the same `needsStringCasting()` check used in `CONCAT`
operations
- Ensures consistent string type handling for null checks

BEFORE
<img width="2304" height="560" alt="image"
src="https://github.com/user-attachments/assets/e74c73af-838e-4282-b6f7-9778f03e562d"
/>

AFTER
<img width="1043" height="292" alt="Screenshot 2025-11-10 at 14 37 35"
src="https://github.com/user-attachments/assets/ade48ce5-1290-4fc7-afb4-57adcf3ebaf3"
/>

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Carlos Crespo <carloshenrique.leonelcrespo@elastic.co>
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 release_note:skip Skip the PR/issue when compiling release notes Team:obs-exploration Observability Exploration team v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Metrics][Discover] Filter by value: Dropdown list is inconsistent with charts data

5 participants