Skip to content

[Discover] add metadata fields _id and _index to esql query search#258451

Closed
neptunian wants to merge 3 commits intoelastic:mainfrom
neptunian:add-metadata-to-esql-mode-doc
Closed

[Discover] add metadata fields _id and _index to esql query search#258451
neptunian wants to merge 3 commits intoelastic:mainfrom
neptunian:add-metadata-to-esql-mode-doc

Conversation

@neptunian
Copy link
Copy Markdown
Contributor

@neptunian neptunian commented Mar 18, 2026

Summary

Problem

The Log AI Insight attachment doesn't appear when using ES|QL mode in Discover. In KQL/Lucene mode, the standard _search API automatically includes _id and _index metadata on every hit. ES|QL, however, returns tabular results where metadata fields are opt-in via the METADATA clause. The Log AI Insight component relies on _id and _index to identify documents, so it silently returns null when they're missing effectively making the feature unavailable in ES|QL mode.

Solution

Auto-inject METADATA _id, _index into ES|QL queries before execution in Discover's fetchEsql, like KQL mode. Uses @elastic/esql AST utilities (copied approach Security uses for _id injection in detection rules into kbn-esql-utils) to safely modify the query, with KEEP command compatibiity to prevent the injected fields from being dropped. Falls back to the original query on failure.

@neptunian neptunian requested review from a team as code owners March 18, 2026 21:31
@neptunian neptunian added release_note:fix backport:version Backport to applied version labels v9.3.0 labels Mar 18, 2026
Copy link
Copy Markdown
Contributor

@davismcphee davismcphee left a comment

Choose a reason for hiding this comment

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

This PR still has test failures, and unless something has changed, our rule in Discover has been to not silently modify the main query before running it. @stratoula wdyt?

@neptunian neptunian requested a review from a team as a code owner March 19, 2026 02:23
@elasticmachine
Copy link
Copy Markdown
Contributor

elasticmachine commented Mar 19, 2026

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] src/platform/test/functional/apps/discover/esql/config.ts / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #20 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #30 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #100 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #109 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] src/platform/test/functional/apps/discover/esql/config.ts / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #20 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #109 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #30 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #100 / discover/esql discover esql view errors should show error messages for syntax errors in query
  • [job] [logs] FTR Configs #87 / discover/group4 Discover document comparison ES|QL mode should allow selecting comparison fields
  • [job] [logs] FTR Configs #87 / discover/group4 Discover document comparison ES|QL mode should allow selecting comparison fields
  • [job] [logs] Scout: [ security / entity_store ] plugin / local-stateful-classic - Entity Store Main logs extraction - Should extract properly extract user

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
agentBuilder 1795 1796 +1
cases 2099 2100 +1
console 379 380 +1
controls 423 424 +1
dashboard 1091 1092 +1
dashboardAgent 605 606 +1
data 619 620 +1
datasetQuality 1121 1122 +1
dataVisualizer 871 872 +1
discover 2001 2002 +1
esql 964 965 +1
eventAnnotationListing 697 698 +1
infra 1827 1828 +1
lens 1707 1708 +1
lists 464 465 +1
maps 1368 1369 +1
ml 4156 4157 +1
observability 1776 1777 +1
observabilityAIAssistantApp 806 807 +1
securitySolution 9206 9207 +1
slo 1284 1285 +1
stackAlerts 226 227 +1
streamsApp 1840 1841 +1
triggersActionsUi 1326 1327 +1
unifiedDocViewer 931 932 +1
visTypeVega 1826 1827 +1
total +26

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/esql-utils 138 140 +2

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.6MB 1.6MB +1.2KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
observabilityAgentBuilder 9.3KB 9.4KB +138.0B
Unknown metric groups

API count

id before after diff
@kbn/esql-utils 202 205 +3

ESLint disabled line counts

id before after diff
discover 17 18 +1

Total ESLint disabled count

id before after diff
discover 19 20 +1

History

@stratoula
Copy link
Copy Markdown
Contributor

stratoula commented Mar 19, 2026

This PR still has test failures, and unless something has changed, our rule in Discover has been to not silently modify the main query before running it. @stratoula wdyt?

Yes I agree, this PR comes out of surprise. We don't want to do "magic" things in discover. This hasn't changed. We were discussing with ES the possibility to have a Setting for this but this hasn't prioritized. This is also tricky technically, it adds complexity that we don't want.

I suggest to bring the problem you are trying to solve in one discover sync. Then we can discuss alternatives and also check with ES.

@neptunian neptunian marked this pull request as draft March 19, 2026 13:07
@elasticmachine
Copy link
Copy Markdown
Contributor

🤖 Jobs for this PR can be triggered through checkboxes. 🚧

ℹ️ To trigger the CI, please tick the checkbox below 👇

  • Click to trigger kibana-pull-request for this PR!
  • Click to trigger kibana-deploy-project-from-pr for this PR!
  • Click to trigger kibana-deploy-cloud-from-pr for this PR!
  • Click to trigger kibana-entity-store-performance-from-pr for this PR!
  • Click to trigger kibana-storybooks-from-pr for this PR!

@neptunian
Copy link
Copy Markdown
Contributor Author

@stratoula @davismcphee Understandable and sorry for the noise. I've opened a workaround PR using available fields instead of fetching the fresh doc with the id #258595. This is actually sufficient for our needs. It would be nice-to-have, to have the id and index available.

@neptunian neptunian closed this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:version Backport to applied version labels release_note:fix v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants