Skip to content

Fixes #38379: escape Lucene regexp metacharacters in search aggregate include patterns - #38414

Merged
TeddyCr merged 5 commits into
mainfrom
detail/bug-fix/fixes-38379-escape-lucene-regexp-metacharacters-in-f24b99
Nov 26, 2026
Merged

TeddyCr merged 5 commits into
mainfrom
detail/bug-fix/fixes-38379-escape-lucene-regexp-metacharacters-in-f24b99

Conversation

@detail-app

@detail-app detail-app Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Detail bug report: View on Detail

Describe your changes:

Fixes #38379

I worked on fixing a server-side Lucene regexp escaping gap in the /v1/search/aggregate quick-filter path because direct REST consumers that sent a fieldValue containing metacharacters OpenMetadata itself permits in tag/glossary/owner/service names (Pii.Phone (mobile), R&D Team, C++, A|B, trailing \) either got an HTTP 500 for the whole aggregation request or silently wrong counts.

Type of change:

  • Bug fix

High-level design:

N/A — small change.

Tests:

Use cases covered

  • Direct REST consumer posts .*Pii.Phone (mobile).* → HTTP 200 with the literal bucket pii.phone (mobile) (previously: (mobile) was a Lucene group → 0 buckets).
  • .*C++.* → matches only the c++ bucket, not c/cc/ccc (previously: ++ quantifier over-matched).
  • .*R&D Team.* → matches the r&d team bucket (previously: & intersection operator → empty automaton → 0 buckets).
  • Pre-built alternations like (pii\.a|pii\.b) posted via postExactAggregateFieldOptions (the useTagUsageCounts path) still match either FQN — they pass through verbatim, the server does NOT escape their ( | ) operators.
  • Both Elasticsearch and OpenSearch aggregation managers apply the shared escaping.
  • Shipped UI quick-filter dropdowns continue to work (the UI no longer pre-escapes the aggregate value; the server owns escaping, avoiding double-escaping).

Unit tests

  • Added SearchUtils.escapeLuceneRegexp + SearchUtils.buildBestMatchIncludePatterns helpers and unit tests in openmetadata-service/src/test/java/org/openmetadata/service/search/SearchUtilsTest.java covering: every Lucene regexp metacharacter (compile + literal-match + no-overmatch), the trailing-\ request-rejection case, the literal quick-filter path (full escaping for exact/prefix/contains), pre-built alternation pass-through, and dot-only-input preservation.
  • Added UI tests in openmetadata-ui/src/main/resources/ui/src/rest/miscAPI.test.ts pinning that getAggregateFieldOptions/postAggregateFieldOptions send .*value.* raw (no client-side backslash escaping) for C++, R&D Team, A|B, Revenue (net of tax) [US], john?, user.id, and .* for empty value.

Backend integration tests

  • Added SearchAggregateRbacIT.test_aggregate_fieldValueWithRegexpMetacharactersMatchesLiteralTerm in openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/search/SearchAggregateRbacIT.java. It seeds glossary terms with metacharacter-bearing display names and POSTs fieldValue containing parens, &, ++, and [], asserting HTTP 200 (the SDK client throws on non-2xx) and that each bucket key is the literal term — not a group, intersection, or quantifier result.

PR checks run only the Java unit tests; the integration tests run in the merge queue and JavaUIIT / search-it nightly. Ran the impacted integration tests locally for both engines (see Manual testing).

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI behavior change visible to Playwright; the UI contract test is covered by the Jest unit test above).

Manual testing performed

  1. Ran SearchAggregateRbacIT against Testcontainers Elasticsearch (-Pmysql-elasticsearch) — 8/8 pass (7 original + the new metacharacter test). Same against OpenSearch (-Ppostgres-opensearch) — 7/7 pass.
  2. Ran the additional aggregation ITs selected by the search area of .github/java-tests/impact-map.json for both engines — FieldNamesAggregationIT (5/5), CustomPropertyAggregationIT (2/2), TableTotalCountAggregationIT (1/1) on each engine.
  3. Built the server locally with the fix and ran it against standalone MySQL 8.4 + Elasticsearch 9.3 containers, then authenticated as admin and POSTed /v1/search/aggregate with the UI's exact request shape:
    • fieldValue: ".*pii.phone (mobile).*" → HTTP 200, bucket pii.phone (mobile) (doc_count 1).
    • fieldValue: ".*c++.*" → HTTP 200, bucket c++ (doc_count 1) — no over-match of c/cc/ccc.
    • fieldValue: ".*r&d team.*" → HTTP 200, bucket r&d team (doc_count 1) — no empty-automaton zero-bucket result.
    • Raw alternation fieldValue: "(alt\\.one|alt\\.two)" (no .*….* wrapper, as postExactAggregateFieldOptions sends) → HTTP 200, both alt.one and alt.two buckets — confirms pre-built regexps still pass through verbatim.
  4. Ran make java_affected_run ARGS="--scope focused" → 204/204 unit tests pass; the planner (run with make java_affected ARGS="--scope focused") identified the impacted unit class (SearchUtilsTest) and ran it.
  5. Ran mvn spotless:check for openmetadata-service and openmetadata-integration-tests, and yarn lint:base + yarn pretty:base on the changed UI files — all clean. yarn tsc:check shows only the pre-existing OMSelectWidget.tsx error (unchanged by this PR).

Local Java test run: PASSED — make java_affected_run ARGS="--scope focused" (SearchUtilsTest 204/204, 0 failed). Impacted integration tests SearchAggregateRbacIT, FieldNamesAggregationIT, CustomPropertyAggregationIT, TableTotalCountAggregationIT ran locally on both Elasticsearch and OpenSearch engines (all pass, 8+7+5+5+2+2+1+1 tests, 0 failures).

Not applicable — no UI behavior change visible to Playwright.

UI screen recording:

Not applicable — backend-focused fix; the UI change (miscAPI.ts) only removes redundant client-side escaping that the server now owns, with no user-visible behavior change. The Jest unit test pins the new contract.

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.
  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed.
  • For a UI feature or feature task: as part of PR creation, I ran the PR build in Docker with sample data and embedded a verified GitHub-hosted recording plus build/health evidence above (or explained why this requirement is not applicable).
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • Self-review: I reviewed the final diff against REVIEW.md (the pr-checklist skill runs it) and fixed every 🔴 finding, or answered it on the line below.
  • Decision check: if this PR decides something the next contributor could undo — a schema field, REST path or format another component or repo must agree with, a new default or limit, an ordering that must hold, or a rule stated only in this description — it adds a record under docs/decisions/ (see its README.md). This description is gone from git log after the squash-merge.

Root cause and fix

The bestMatchAggregations method in both ElasticSearchAggregationManager and OpenSearchAggregationManager built Lucene regexp include patterns for the terms aggregation that only escaped . (exact/prefix) and passed the contains pattern completely raw. Every other Lucene regexp metacharacter (()[]{}+*?|&~#@!<>=-^"\/) was left to be interpreted by the engine, so values that are legal data in OpenMetadata (Pii.Phone (mobile), R&D Team, C++) either rejected the whole _search request (unbalanced/dangling regexp → HTTP 500) or silently mis-matched (& intersection, () group, | union, +/? quantifiers).

The fix adds a shared SearchUtils.escapeLuceneRegexp helper (escaping the full metacharacter set, mirroring the existing ColumnAggregator.toCaseInsensitiveRegex) and SearchUtils.buildBestMatchIncludePatterns, which routes literal quick-filter values (the .*….*-wrapped shape) through escaping for all three sub-aggregations while passing pre-built regexps (e.g. the tag-FQN alternation (pii\.a|pii\.b) built by the UI's buildTagFqnIncludeRegex and posted via postExactAggregateFieldOptions) through verbatim. Both aggregation managers now call the shared helper. The UI's getAggregateFieldOptions/postAggregateFieldOptions stop pre-escaping with escapeESReservedCharacters so the server's single escaping pass is not double-escaped; escapeESReservedCharacters is still used for the q query-string path.


Automatic Fixes PRs can be configured here.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the remaining concern is a non-blocking test gap.

Findings

  1. P2 Tests promise unsupported matching ▶

Summary

The PR moves escaping for quick-filter values to the server and shares it across Elasticsearch and OpenSearch.

  • Aggregate quick filters now match names with punctuation literally.

Reviews (2) · Last reviewed commit: "fix(search): use pre-built regexp verbat..." · Reviewed by Greptile

Appending .* for the prefix sub-agg lets child tags (e.g. pii.a.child)
displace requested siblings (e.g. pii.b) under the best-match merge
limit, producing false-zero bucket counts. Use the caller's pattern
verbatim for exact/prefix/contains in the pre-built regexp path.

Also drop the anchored (^…$) pre-built regexp test: ^ and $ are anchors
in Java Pattern but literal in Lucene, so the test promised anchor
behavior that does not hold in production.
@TeddyCr TeddyCr removed their assignment Nov 24, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

UI browser tests

Ran: all test files (no impact map from main yet)

Result: ✅ 1094 passed · 154 skipped

Changed code executed by these browser tests: 2 / 2 lines (100%)

File Changed lines Executed %
src/rest/miscAPI.ts 2 2 100%

@TeddyCr
TeddyCr enabled auto-merge (squash) November 25, 2026 22:46
@TeddyCr
TeddyCr merged commit 881d635 into main Nov 26, 2026
61 checks passed
@TeddyCr
TeddyCr deleted the detail/bug-fix/fixes-38379-escape-lucene-regexp-metacharacters-in-f24b99 branch November 26, 2026 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Detail Bug] Search Aggregations: /v1/search/aggregate breaks or miscounts when filter values contain Lucene regex metacharacters

2 participants