Skip to content

Harden Foundatio.Lucene for 1.0: SQL/ES parity, scoped config swapping, API lock - #65

Merged
ejsmith merged 11 commits into
mainfrom
zealous-curran-58226c
Sep 29, 2026
Merged

ejsmith merged 11 commits into
mainfrom
zealous-curran-58226c

Conversation

@ejsmith

@ejsmith ejsmith commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Context

Prepares the library for a stable 1.0, centered on the primary use case: high-performance per-scope configuration swapping (each scope/tenant has its own custom fields, aliases, and options) that works equally well with SQL (EF Core) and Elasticsearch. Delivered as six milestones, one commit each (plus two small cleanups).

What changed

M1 — core/lexer hardening (61feaae)

  • LuceneParser.MaxDepth (default 100) bounds group recursion → fixes an uncatchable StackOverflowException DoS on deeply nested input.
  • Culture-invariant boost (^) and fuzzy/slop (~N) parsing.
  • Lexer no longer overruns the buffer on a trailing backslash in a quoted string/regex.
  • ParseError.Code (QueryErrorCode) classification; removed dead ParseNumberTerm.

M2 — symmetric scope-swap mechanism (13444ca)

  • Deleted the orphaned, untested TenantOptionsCache/EntityOptionsCache/TenantEntityOptionsCache/VisitorPool (~700 LOC). Per-request options (immutable records) are the blessed swap path; apps own caching.
  • Elasticsearch carries per-scope field map/includes on the visitor context and reuses singleton visitors → no per-query visitor allocation on the swap path.
  • Added ConfigSwapBenchmarks (EF + ES).

M3 — SQL/ES parity (2fb0cf0)

  • EF now runs the shared pre-build pipeline (field-alias resolution, @include expansion, date math) — these were exposed on the options API but silently ignored in SQL. Added a configurable TimeProvider to the EF config.
  • Capability model: EF throws QueryBuildException (UnsupportedQueryType) for fuzzy/proximity queries instead of silently degrading them to substring matches.
  • Cross-engine parity gate: new Foundatio.Lucene.Parity.Tests seeds identical data into SQL Server + Elasticsearch (Testcontainers) and asserts the same Lucene query returns the same result set on both engines.

M4 — public API lock (a96ba78)

  • Removed the dead Elasticsearch geo surface (builder never emitted geo; async resolver couldn’t be awaited in the sync pipeline). Geo is deferred to a post-1.0 design that keeps the visitor pipeline synchronous.
  • Unified the duplicate default-operator enum: QueryOperator → Ast.BooleanOperator.
  • LuceneParseResult.Document/Errors are init-only; EntityFieldInfo equality keys on FullName (was the leaf Name); removed unused IsMoney.

M5 — test coverage (e2b572c) — QueryResult model, ES/EF TryBuild* failure paths, registered-options mutation under concurrent builds.

M6 — release essentials (60b98fc) — root LICENSE (Apache-2.0), CHANGELOG.md, and a full docs-drift fix: the docs site assumed a non-existent async visitor API and AST shape; examples now use the real synchronous API and reflect the geo removal + EF pipeline support. Corrected AGENTS.md (recursive-descent parser; sync QueryVisitor.Visit).

Breaking changes

  • Removed TenantOptionsCache/VisitorPool family, the Elasticsearch geo config surface, the QueryOperator enum, and EntityFieldInfo.IsMoney.
  • EF fuzzy/proximity queries now throw instead of silently matching.
  • LuceneParseResult members are init-only.

Warrants a v1.0.0 tag (not a patch).

Testing

  • dotnet build clean; dotnet format --verify-no-changes clean.
  • 875/875 tests pass on a full dotnet test run — core, EF (incl. SQL Server FTS integration), Elasticsearch (incl. real ES 9.0), and the cross-engine parity gate, all concurrent. (ES test-container heap is capped so the suites run side by side.)

Follow-ups (documented in CHANGELOG, not blockers)

  • Adopt Microsoft.CodeAnalysis.PublicApiAnalyzers with committed baselines (surface is already cleaned; this adds the build-time guardrail — best generated via the IDE one-click fix).
  • Foundatio.Parsers → Foundatio.Lucene migration guide.

ejsmith and others added 11 commits June 24, 2026 23:38
- Add LuceneParser.MaxDepth (default 100) bounding group recursion to prevent
  a StackOverflow DoS from deeply nested input. Over-depth records a
  MaxDepthExceeded ParseError and the group is discarded iteratively.
- Parse boost (^) and fuzzy/slop (~N) with InvariantCulture so values such as
  2.5 do not change meaning on comma-decimal locales.
- Fix a lexer trailing-backslash overrun in quoted-string and regex scanning
  that advanced past the end of the buffer (could throw inside Slice).
- Add a Code (QueryErrorCode) field to ParseError and classify depth, bracket,
  range and unexpected-character errors.
- Remove the dead ParseNumberTerm method.
- Delete the orphaned, untested TenantOptionsCache / EntityOptionsCache /
  TenantEntityOptionsCache and VisitorPool (~700 lines, zero references).
  Per-request options (ElasticsearchQueryOptions / EntityFrameworkQueryOptions,
  both immutable records) are the blessed scope-swap path; apps own caching.
- ElasticsearchQueryParser now carries the per-scope field map and includes on
  the visitor context and reuses singleton FieldResolver/Include visitors instead
  of allocating a new visitor per query when a scope supplies its own config.
  Removes per-query visitor allocation on the swap path.
- Add ConfigSwapBenchmarks (ES + EF) measuring per-query allocations when each
  query swaps a different scope against a shared parser.
- EntityFrameworkQueryParser now runs the shared pre-build visitor pipeline
  (field-alias resolution, @include expansion, date-math evaluation) before
  building the LINQ expression, so per-scope FieldMap, includes, and date math
  behave identically to the Elasticsearch integration (previously silently
  ignored in SQL). Adds a configurable TimeProvider to the EF configuration.
- Capability model: the EF builder throws QueryBuildException
  (UnsupportedQueryType) for fuzzy (term~N) and proximity/slop ("..."~N)
  queries instead of silently degrading them to substring matches.
- Add Foundatio.Lucene.Parity.Tests: starts SQL Server + Elasticsearch
  containers, seeds identical data, and asserts the same Lucene query returns
  the same result set on both engines across the supported construct set.
- Remove the dead Elasticsearch geo surface (IsGeoPointField, GeoLocationResolver,
  UseGeoFields/UseGeoLocationResolver/WithGeoPointFields, context geo members and
  the geo lines in CreateContext). The builder never emitted geo queries and the
  async resolver could not be awaited in the synchronous pipeline; geo is deferred
  to a post-1.0 design that keeps the visitor pipeline synchronous.
- Unify the duplicate default-operator enum: Elasticsearch now uses
  Foundatio.Lucene.Ast.BooleanOperator everywhere and the separate QueryOperator
  enum is deleted.
- Make LuceneParseResult.Document/Errors init-only.
- Fix EntityFieldInfo equality/hash to key on FullName instead of the leaf Name
  (nested fields sharing a leaf name no longer collide) and remove the unused
  public IsMoney property.
- QueryResultTests: success/failure model, GetValueOrThrow, GetValueOrDefault,
  Map, Try, Deconstruct, and implicit-bool behavior.
- ElasticsearchErrorAndConcurrencyTests: TryBuildQuery returns a Failure (not a
  throw) on malformed input, and a stress test mutating registered per-index
  options (SetOptions/RemoveOptions) while building queries on a shared parser.
- EF: TryBuildFilter returns a Failure on malformed input.
- Add root LICENSE (Apache-2.0) matching the package license expression.
- Add CHANGELOG documenting the 1.0 hardening (added/changed/removed/fixed,
  breaking changes, and the PublicApiAnalyzers follow-up).
- Fix pervasive documentation drift: examples now use the real synchronous API
  (no ParseAsync/BuildQueryAsync/RunAsync/AcceptAsync/VisitAsync), the real AST
  shape (no BooleanQueryNode.Left/Right, GroupNode.Child, FieldQueryNode.Value,
  or IncludeNode), BooleanOperator instead of QueryOperator, and reflect the geo
  removal and the EF field-map/include/date-math pipeline. Correct AGENTS.md
  (recursive-descent parser; synchronous QueryVisitor.Visit pattern).
Follow-up to the Elasticsearch enum unification and EF pipeline wiring.
Adding the cross-engine parity project introduced a second Elasticsearch
container; running the full solution test suite then launched 2x ES + 2x SQL
concurrently and exhausted Docker memory. Bound the ES JVM heap (-Xms512m
-Xmx512m) in both ES fixtures (the test data sets are tiny) so the full
`dotnet test` run is reliable.
State the API is synchronous positively instead of enumerating *Async
methods that do not exist.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ejsmith
ejsmith merged commit 8f58eba into main Sep 29, 2026
5 checks passed
@ejsmith
ejsmith deleted the zealous-curran-58226c branch September 29, 2026 02:47
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.

1 participant