Skip to content

Fixes #36463: Fix find_context and company_context on Elasticsearch - #36548

Merged
pmbrull merged 5 commits into
open-metadata:mainfrom
MostafaAbdelsalam11:fix/find-context-elasticsearch
Nov 4, 2026
Merged

pmbrull merged 5 commits into
open-metadata:mainfrom
MostafaAbdelsalam11:fix/find-context-elasticsearch

Conversation

@MostafaAbdelsalam11

Copy link
Copy Markdown

Describe your changes:

Fixes #36463

find_context and company_context returned nothing on Elasticsearch even with semantic search enabled and embeddings present. AIContextFinder and CompanyContextTool read OpenSearchVectorService.getInstance(), a singleton that SearchRepository.initializeVectorSearchService() only creates when searchType is OpenSearch. On Elasticsearch it is always null, so the finder skipped its search silently ({"items":[],"candidateAssets":[],"empty":true}) and company_context answered "Semantic search is not enabled".

Both now read Entity.getSearchRepository().getVectorIndexService(), the accessor SemanticSearchTool and VectorSearchResource already use. On OpenSearch it returns the same OpenSearchVectorService instance as before, so nothing changes there. Both call sites only use search(...) overloads declared on VectorIndexService, and ElasticSearchVectorService applies the same memory-visibility filter (readableAnchors, appendMemoryVisibilityFilter), so the per-user filter in company_context still applies on Elasticsearch.

Same fix as #32547, which its author closed before review after the PR metadata check failed.

Overlap with #36517: that PR (opened earlier today) contains the identical five-line change in the same four files. I prepared this independently as the reporter of #36463 and am opening it because it carries extra verification (a real Elasticsearch before/after run and the planner-selected integration tests, below). I'm happy to close this in favour of #36517 if the maintainers prefer, and to move any of the evidence onto it.

Type of change:

  • Bug fix

High-level design:

N/A, small change: 5 changed lines in 2 files.

Out of scope, not changed here: GetEntityTool and AIContextBuilder call searchChunksByParent, which exists only on OpenSearchVectorService (Elasticsearch has no chunk index), so they fall back to the full text on Elasticsearch. The remaining OpenSearchVectorService.getInstance() callers are index-write paths (OpenSearchBulkSink, DistributedJobParticipant, RecreateWithEmbeddings, VectorSearchUtils).

Tests:

Use cases covered

  • find_context / GET /api/v1/ai/context/find returns matching glossary terms, metrics and pages on Elasticsearch.
  • company_context with a query runs its vector query on Elasticsearch instead of reporting "Semantic search is not enabled".
  • When embeddings are enabled but no vector service was created, company_context still reports "Semantic search is not enabled".

Unit tests

  • I added unit tests for the new/changed logic.
  • Files updated: AIContextFinderTest (new: find_usesTheBackendNeutralVectorServiceWhenTheOpenSearchSingletonIsNull, with the OpenSearch singleton null and the neutral service returning one hit), CompanyContextToolTest (3 existing tests now inject VectorIndexService through SearchRepository; new searchReportsSemanticSearchNotEnabledWhenNoVectorServiceWasCreated).
  • Red before the fix: AIContextFinderTest.find_usesTheBackendNeutralVectorService... fails with expected: <[Business.BounceRate]> but was: <[]>, and 3 of the CompanyContextToolTest tests fail. Green after.
  • Coverage (Jacoco, -Pstatic-code-analysis, AIContextFinderTest, CompanyContextToolTest, AIContextFinderAccessTest): every changed executable line is covered. Whole-class line coverage is 83.7% for AIContextFinder (118/141) and 89.1% for CompanyContextTool (106/119), below the 90% target; the uncovered lines are existing code this PR does not touch (candidate-asset routing and the other company_context paths).
  • The new searchReports...NotEnabled... test is a regression guard for the null branch; its assertion alone also passes on the old code, and it fails on a revert only through Mockito strict stubs.

Backend integration tests

  • I added integration tests in openmetadata-integration-tests/ for new/changed API endpoints.
  • Not applicable (no new or changed API endpoints). The impacted integration tests were run locally, see below. A real-Elasticsearch IT for find_context is left as a follow-up.

make java_affected_run selected 3 unit classes and 50 integration classes (45 in the parallel lane, 3 isolated, 2 rdf). 2,024 tests ran and passed (35 unit, 1,934 + 12 + 43 integration), 147 skipped themselves, and none failed. The parallel step is marked FAILED only because McpServiceResourceIT is selected but cannot produce a report: the file is a TODO comment with no class or tests, and it is identical on origin/main. The "uncommitted changes" note in the block below refers to two unrelated local files that are not in this PR's commit.

Local Java test run: FAILED

  • Commit: f7a8a71ba3c9 (base origin/main) — uncommitted changes were present
  • Finished: 2026-10-29 10:49 UTC · 14.6 min
Step Scope Classes Tests Failed Skipped Min Result
unit openmetadata-mcp (1 class), openmetadata-service (2 classes) 3 35 0 0 1.1 passed
integration mysql-elasticsearch · parallel 44 2081 0 147 3.5 FAILED
integration mysql-elasticsearch · isolated 3 12 0 0 2.5 passed
integration mysql-elasticsearch · rdf 2 43 0 0 7.5 passed

Selected but produced no report: McpServiceResourceIT (mysql-elasticsearch · parallel)

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

Built a server image from main (a9757cf0), then from this branch, and ran each against Elasticsearch 9.4.2 and Postgres in Docker.

  1. Built with mvn -DskipTests -Dskip.ui.build=true -pl openmetadata-dist -am package and docker build -f docker/development/Dockerfile.
  2. Started with SEARCH_TYPE=elasticsearch SEMANTIC_SEARCH_ENABLED=true LLM_ENABLED=true LLM_PROVIDER=openai EMBEDDING_PROVIDER=openai (text-embedding-3-small). The log shows ElasticSearchVectorService initialized and no OpenSearch service.
  3. Created a glossary with 3 approved terms and one metric through the REST API, and confirmed all 4 documents carry an embedding.
  4. Called the same endpoints before and after the change:
Call main This branch
GET /api/v1/ai/context/find?q=bounce rate {"items":[],"candidateAssets":[],"empty":true} 4 items, Bounce Rate first
GET /api/v1/ai/context/find?q=qualified visits empty 4 items, Qualified Visit first
MCP find_context "bounce rate" "No company knowledge found for this query." # Relevant Knowledge, Bounce Rate first
MCP company_context with query "Semantic search is not enabled..." {"returnedCount":0,"results":[]} (no pills exist)
MCP semantic_search (control) 4 results 4 results

UI screen recording:

Not applicable, backend-only change.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • I have commented on my code, particularly in hard-to-understand areas. (N/A: a one-line lookup change.)
  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. (N/A: no schema change.)
  • For a UI feature or feature task: ran the PR build in Docker and embedded a recording. (N/A: backend-only.)
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • Self-review: I reviewed the final diff against REVIEW.md and fixed every 🔴 finding, or answered it on the line below.
    • Reviewed commit f7a8a71ba3c9ceca1c1a872049f9378fc79c78d8 with a fresh reviewer: no 🔴 findings. Left as 🟡 notes: no real-engine IT (follow-up), the finder still turns a failed search into an empty result (pre-existing), and the new null-branch test is a weak guard (disclosed above).
  • Decision check: this PR decides nothing a later contributor could undo (it reuses an existing accessor), so no record under docs/decisions/.

Bug fix

  • I have added a test that covers the exact scenario we are fixing.

…asticsearch

AIContextFinder and CompanyContextTool read OpenSearchVectorService.getInstance(), which is only
initialised when searchType is OpenSearch. On Elasticsearch it is always null, so find_context
silently returned an empty result and company_context reported "Semantic search is not enabled"
even with embeddings present.

Read the backend-neutral SearchRepository.getVectorIndexService() instead, as SemanticSearchTool
already does. On OpenSearch it returns the same OpenSearchVectorService instance as before.
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@MostafaAbdelsalam11

Copy link
Copy Markdown
Author

Local Java test run: FAILED — details

  • Commit: f7a8a71ba3c9 (base origin/main) — uncommitted changes were present
  • The summary is in the PR description.

Selected but produced no report: McpServiceResourceIT (mysql-elasticsearch · parallel)

Tests run locally

  • unit · openmetadata-mcp (1 class), openmetadata-service (2 classes): AIContextFinderTest (10 passed), CompanyContextToolTest (18 passed), ConceptContextCatalogTest (7 passed)
  • integration · mysql-elasticsearch · parallel: 44 classes, 1934 tests executed (listed below)
  • integration · mysql-elasticsearch · isolated: ContextMemoryDerivationIT (5 passed), ContextMemoryExtractionIT (5 passed), ConversationSchemaMigrationIT (2 passed)
  • integration · mysql-elasticsearch · rdf: AgentSparqlResourceIT (22 passed), RdfMcpKnowledgeGraphIT (21 passed)

Per-class counts come from the test reports, which can credit a test to the wrong class when classes run concurrently, and keep only the last run of a nested class that several test classes inherit. The summary's step totals are Maven's own counts.

integration · mysql-elasticsearch · parallel: 45 classes

AIApplicationResourceIT (3 passed), AIContextKnowledgeAuthorizationIT (5 passed), AIContextMcpIT (6 passed), AIContextRestIT (40 passed, 4 skipped), AIFrameworkControlResourceIT (5 passed), AIGovernancePolicyResourceIT (59 passed), AgentExecutionResourceIT (8 passed, 3 skipped), BaseEntityIT$ListEntityHistoryByTimestampPaginationTest (147 passed, 25 skipped), ConceptContextIT (10 passed), ConfigResourceIT (12 passed), ContextFileIT (216 passed, 24 skipped), ContextMemoryAnchorIT (45 passed, 3 skipped), ContextMemoryConflictReviewIT (30 passed, 3 skipped), ContextMemoryCreateGrantIT (4 passed), ContextMemoryIT (393 passed, 38 skipped), ContextMemoryLifecycleIT (8 passed, 1 skipped), ContextMemoryQueryEvidenceIT (18 passed, 1 skipped), ContextMemoryRelatedEntitiesReconcileIT (10 passed), ContextMemorySearchMatrixIT (28 passed), ContextMemorySourceEntityTiebreakIT (14 passed), ContextMemoryStatusMigrationIT (1 passed), ContextMemoryWriteAccessIT (1 passed), ConversationResourceIT (1 passed), DatabaseSmokeIT (2 passed), GlossaryTermResourceIT (230 passed, 6 skipped), IngestionPipelineAgentTypeIT (1 passed), LLMModelResourceIT (8 passed), LLMServiceResourceIT (23 passed, 5 skipped), McpContextMemoryVisibilityIT (32 passed, 2 skipped), McpExecutionResourceIT (1 passed), McpIntegrationIT (51 passed, 4 skipped), McpOAuthIT (14 passed, 2 skipped), McpPermissionIT (7 passed), McpRdfNotEnabledIT (21 passed, 2 skipped), McpSearchIT (41 passed, 1 skipped), McpServerResourceIT (58 passed, 2 skipped), McpToolsValidationIT (5 passed), McpUserContextIT (6 passed), MetricAssetsIT (28 passed, 1 skipped), OntologyAiResourceIT (3 passed), PersonaAIContextIT (28 passed, 1 skipped), PromptTemplateResourceIT (20 passed, 1 skipped), SeedDataPresenceIT (5 passed), TableResourceIT (163 passed, 4 skipped), UserResourceIT (7 passed)

Why these tests
  • area ai-mcp-context ← openmetadata-mcp/src/main/java/org/openmetadata/mcp/tools/CompanyContextTool.java, openmetadata-mcp/src/test/java/org/openmetadata/mcp/tools/CompanyContextToolTest.java, openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextFinder.java
  • called from entity AIContext ← openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextFinder.java
  • changed test ← openmetadata-mcp/src/test/java/org/openmetadata/mcp/tools/CompanyContextToolTest.java, openmetadata-service/src/test/java/org/openmetadata/service/aicontext/AIContextFinderTest.java
  • smoke ← code other than a test class changed
Commands
mvn -B package -pl openmetadata-mcp,openmetadata-service -am -Dspring-boot.repackage.skip=true -Dtest=AIContextFinderTest,CompanyContextToolTest,ConceptContextCatalogTest -Dsurefire.failIfNoSpecifiedTests=false
mvn -B verify -pl :openmetadata-integration-tests -am -Dspring-boot.repackage.skip=true -Pmysql-elasticsearch -DintegrationTests.lane=parallel '-Dit.test=<45 classes>' -Dfailsafe.failIfNoSpecifiedTests=false -Dtest=NoUnitTestsInThisRun -Dsurefire.failIfNoSpecifiedTests=false
mvn -B verify -pl :openmetadata-integration-tests -am -Dspring-boot.repackage.skip=true -Pmysql-elasticsearch -DintegrationTests.lane=global-state -Dit.test=ContextMemoryDerivationIT,ContextMemoryExtractionIT,ConversationSchemaMigrationIT -Dfailsafe.failIfNoSpecifiedTests=false -Dtest=NoUnitTestsInThisRun -Dsurefire.failIfNoSpecifiedTests=false
mvn -B verify -pl :openmetadata-integration-tests -am -Dspring-boot.repackage.skip=true -Pmysql-elasticsearch -DintegrationTests.lane=rdf -Dit.test=AgentSparqlResourceIT,RdfMcpKnowledgeGraphIT -Dfailsafe.failIfNoSpecifiedTests=false -Dtest=NoUnitTestsInThisRun -Dsurefire.failIfNoSpecifiedTests=false

@MostafaAbdelsalam11
MostafaAbdelsalam11 marked this pull request as ready for review October 29, 2026 12:10
@MostafaAbdelsalam11
MostafaAbdelsalam11 requested a review from a team as a code owner October 29, 2026 12:10
@MostafaAbdelsalam11
MostafaAbdelsalam11 requested review from pmbrull and a balanced review from Copilot October 29, 2026 12:10
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues remain for the change to use the configured backend-neutral vector service.

No open issues 🎉

What changed in this PR

Fixes #36463 by routing find_context and company_context through the configured backend-neutral vector service, enabling Elasticsearch searches while preserving the existing OpenSearch behavior.

Changes:

  • Replaces OpenSearch singleton lookups with SearchRepository.getVectorIndexService() in both search paths.
  • Adds finder regression coverage, updates company-context tests to use VectorIndexService, and covers an unavailable vector service.
File Description
openmetadata-service/​src/​test/​java/​org/​openmetadata/​service/​aicontext/​AIContextFinderTest.java Tests returned knowledge when the OpenSearch singleton is null.
openmetadata-service/​src/​main/​java/​org/​openmetadata/​service/​aicontext/​AIContextFinder.java Resolves the configured vector service for knowledge searches.
openmetadata-mcp/​src/​test/​java/​org/​openmetadata/​mcp/​tools/​CompanyContextToolTest.java Updates service mocks and tests the unavailable-service response.
openmetadata-mcp/​src/​main/​java/​org/​openmetadata/​mcp/​tools/​CompanyContextTool.java Uses the backend-neutral service for company-context searches.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 29, 2026 13:57
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are reported for the backend-neutral vector search update.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 30, 2026 08:30
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are reported for the vector-service routing change.

No open issues 🎉

Review effort: Balanced

@pmbrull pmbrull self-assigned this Nov 3, 2026
Copilot AI balanced review requested due to automatic review settings November 3, 2026 16:53
@pmbrull pmbrull added safe to test Add this label to run secure Github workflows on PRs 2.1 labels Nov 3, 2026

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are recorded for the change routing context searches through the configured vector service.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 4, 2026 06:30
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@gitar-bot

gitar-bot Bot commented Nov 4, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Switching context tools to the backend-neutral vector service enables Elasticsearch retrieval, which could surface unexpected semantic matches in AI context results.

Switches AIContextFinder and CompanyContextTool to read the backend-neutral getVectorIndexService() accessor so find_context and company_context work on Elasticsearch, not only OpenSearch. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@pmbrull pmbrull added the safe to test Add this label to run secure Github workflows on PRs label Nov 4, 2026

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are reported for the backend-neutral vector-service changes.

No open issues 🎉

Review effort: Balanced

@pmbrull
pmbrull merged commit 961fb5f into open-metadata:main Nov 4, 2026
91 of 97 checks passed
pmbrull pushed a commit that referenced this pull request Nov 4, 2026
Backport #36548 to 2.0.

(cherry picked from commit 961fb5f)
pmbrull added a commit that referenced this pull request Nov 4, 2026
Backport #36548 to 2.0.

(cherry picked from commit 961fb5f)

Co-authored-by: Mostafa Abdelsalam <[email protected]>
joaopamaral pushed a commit to Automattic/OpenMetadata that referenced this pull request Nov 12, 2026
…asticsearch (open-metadata#36548)

AIContextFinder and CompanyContextTool read OpenSearchVectorService.getInstance(), which is only
initialised when searchType is OpenSearch. On Elasticsearch it is always null, so find_context
silently returned an empty result and company_context reported "Semantic search is not enabled"
even with embeddings present.

Read the backend-neutral SearchRepository.getVectorIndexService() instead, as SemanticSearchTool
already does. On OpenSearch it returns the same OpenSearchVectorService instance as before.

Co-authored-by: Pere Miquel Brull <[email protected]>
(cherry picked from commit 961fb5f)

Backport to 2.0.7: import-only conflict in AIContextFinder (2.0.7 has no
MetricRepository import); AIContextFinderTest keeps the 2.0.7 tests and adds
only the test this commit introduced.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.1 safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP find_context and company_context return no results on Elasticsearch (OpenSearchVectorService singleton is null)

3 participants