Skip to content

refactor(engine): extract shared ES/OS helpers into esoscommon - #22432

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:refactor/esoscommon-shared-helpers
Nov 17, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:refactor/esoscommon-shared-helpers

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Summary

The Elasticsearch and OpenSearch backends carried five byte-identical pure helpers plus two package-level regexes. This PR moves them into internal/engine/internal/esoscommon and consolidates the duplicated tests.

Moved helpers:

  • dedupeStrings → esoscommon.DedupeStrings
  • totalHitsFromESResponse → esoscommon.TotalHitsFromSearchResponse (dropped the misleading ES-only wording — OpenSearch calls it too)
  • buildMetadataIndexName → esoscommon.BuildMetadataIndexName
  • Preprocess → esoscommon.PreprocessSQL (together with its two package-level regexes)
  • isTimeoutError → esoscommon.IsTimeoutError

All 24 production call sites (12 ES / 12 OS) now use the shared versions. The seven duplicated test functions are merged into a single copy under esoscommon, while engine-specific RunSQL timeout/retry tests stay in their respective backends.

Verification

  • bash build.sh --test ./internal/engine/... passes for every package, including both backends' RunSQL retry tests.
  • Acceptance grep for the five old symbols under elasticsearch/ and opensearch/ returns zero hits.
  • Source comparison confirms the moved bodies are byte-identical to the originals; only the regex variable whitespaceRe → preprocessWhitespaceRe was renamed, and the totalHits comment adopts the neutral wording from the OpenSearch copy.

Scope

Behavior-preserving move only: no wire-format, error-message, or SQL-semantics changes. buildBoolQueryFromConditionRaw (the ES/OS id-filter divergence) and the SearchResponse / searchAfterFetcher types are deliberately left out.

Move five byte-identical pure helpers from the Elasticsearch and OpenSearch backends into internal/engine/internal/esoscommon:

- dedupeStrings -> DedupeStrings
- totalHitsFromESResponse -> TotalHitsFromSearchResponse
- buildMetadataIndexName -> BuildMetadataIndexName
- Preprocess -> PreprocessSQL (with its two package-level regexes)
- isTimeoutError -> IsTimeoutError

Consolidate the seven duplicated tests into a single esoscommon copy and update all 24 production call sites. This is a behavior-preserving move: no wire-format, error-message or SQL-semantics changes.
@xugangqiang xugangqiang added the ci Continue Integration label Nov 17, 2026
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change moves metadata naming, push-down utilities, SQL preprocessing, and timeout classification into esoscommon. Elasticsearch and OpenSearch call sites now use the shared helpers, and helper tests are added or updated.

Changes: Shared engine helper refactor

Layer / File(s) Summary
Implement and test shared helpers
internal/engine/internal/esoscommon/*
Adds shared helpers for metadata index names, string deduplication, total-hit parsing, SQL preprocessing, and timeout classification, with tests for their behavior.
Use shared metadata helpers
internal/engine/elasticsearch/common.go, internal/engine/elasticsearch/metadata.go, internal/engine/elasticsearch/metadata_test.go, internal/engine/opensearch/common.go, internal/engine/opensearch/metadata.go, internal/engine/opensearch/metadata_test.go
Both backends use the shared helpers for metadata index naming, push-down deduplication, and total-hit parsing. Their file-local helpers and standalone total-hit tests are removed.
Use shared SQL helpers
internal/engine/elasticsearch/sql.go, internal/engine/elasticsearch/sql_test.go, internal/engine/opensearch/sql.go, internal/engine/opensearch/sql_test.go
Both RunSQL implementations use shared SQL preprocessing and timeout classification. The local implementations and their tests are removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • infiniflow/ragflow#22323: It moves functions into the same esoscommon package and changes shared Elasticsearch and OpenSearch helper call sites.

Suggested reviewers: pyyuhao, yuzhichang, qaqadws

Merge Risk: Minimal · up to fdcdd

This refactor moves duplicated Elasticsearch and OpenSearch helpers into a shared package without intended behavior changes. No concrete risk was found, and the author reports passing tests, so it looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the shared Elasticsearch and OpenSearch helpers are moved into the esoscommon package. The changeset matches this. The title uses the conventional refactor prefix and …
Description check Passed The description follows the required Summary section. It explains what moved, why, how the change was verified, and what is out of scope. It does not include a separate heading for the template's back…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the shared Elasticsearch and OpenSearch helpers are moved into the esoscommon package. The changeset matches this. The title uses the conventional refactor prefix and is concise.

Full details: Description check

Explanation

The description follows the required Summary section. It explains what moved, why, how the change was verified, and what is out of scope. It does not include a separate heading for the template's background text, but the Summary section covers the purpose and background, so the template is met.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit hops through shared code,
Two search engines share the road.
SQL is tidied, timeouts are caught,
Metadata helpers now do the lot.
The tests keep watch as changes flow.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bc7d7b86-610f-45e3-972d-1bcbcae7c77d
Commits

Reviewing files that changed from the base of the PR and between 356a928 and fdcddc0.

Files selected for processing: 15

  • internal/engine/elasticsearch/common.go
  • internal/engine/elasticsearch/metadata.go
  • internal/engine/elasticsearch/metadata_test.go
  • internal/engine/elasticsearch/sql.go
  • internal/engine/elasticsearch/sql_test.go
  • internal/engine/internal/esoscommon/helpers.go
  • internal/engine/internal/esoscommon/metadata.go
  • internal/engine/internal/esoscommon/metadata_test.go
  • internal/engine/internal/esoscommon/sql.go
  • internal/engine/internal/esoscommon/sql_test.go
  • internal/engine/opensearch/common.go
  • internal/engine/opensearch/metadata.go
  • internal/engine/opensearch/metadata_test.go
  • internal/engine/opensearch/sql.go
  • internal/engine/opensearch/sql_test.go
Files with no reviewable changes: 4

  • internal/engine/elasticsearch/common.go
  • internal/engine/elasticsearch/sql_test.go
  • internal/engine/opensearch/common.go
  • internal/engine/opensearch/sql_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@wangq8
wangq8 requested a review from JinHai-CN November 17, 2026 03:43
@wangq8
wangq8 removed the request for review from yuzhichang November 17, 2026 03:52

@yuzhichang yuzhichang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 356a928c..fdcddc0d (15 files, +479/−708).

Verdict: no blocking findings.

Verification of the move (static, against the base SHA):

  • dedupeStrings, totalHitsFromESResponse, isTimeoutError, Preprocess bodies were byte-identical between elasticsearch/ and opensearch/ at base; I diffed the removed and added bodies pairwise. The only deltas in esoscommon are the identifier renames (DedupeStrings, TotalHitsFromSearchResponse, IsTimeoutError, PreprocessSQL, preprocessWhitespaceRe), the parameter name esResponse→searchResponse, and the neutralized totalHits comment.
  • Import hygiene checks out: errors, net, regexp, strings, tokenizer are gone from both sql.go files and have zero remaining uses; fmt in both common.go files stays used after buildMetadataIndexName removal (8 call sites each); encoding/json/bytes/io/http remain used in the metadata.go files.
  • No stale call sites: a repo-wide grep for buildMetadataIndexName, dedupeStrings, totalHitsFromESResponse, isTimeoutError, whitespaceRe, lktksMatchRe, Preprocess under internal/engine/{elasticsearch,opensearch} and internal/engine/internal/esoscommon returns only the new qualified names. The dedupeStrings hits that remain outside are unrelated private helpers in internal/agent/canvas, internal/service/document, internal/utility, and internal/engine/infinity keeps its own two-line preprocessSQL for a different SQL dialect — correctly left out of this change.
  • Import graph is acyclic: esoscommon → tokenizer → internal/binding/internal/common, and esoscommon is only consumed by internal/engine/elasticsearch and internal/engine/opensearch, all under the internal/engine/internal/ visibility boundary. No duplicate declarations of the five exported symbols or the two regex vars in the target package.
  • Both backends' RunSQL retry/timeout tests stay in place and now exercise esoscommon.IsTimeoutError through the shared path; metadata_pushdown_test.go and TestPushdownCapSemantics were updated to the qualified name rather than dropped, so the overflow-fallback contract is still covered.

One observation, not blocking and not introduced by this PR's production code: the new TestMain in internal/engine/internal/esoscommon/sql_test.go sets the process-global tokenizer engine to infinity, which now also applies to the pre-existing tests in that package (e.g. TestMapMemoryMessageToESDoc trivially passes either way). That is test-only scoping with no production effect, so it is not a merge blocker.

@yuzhichang
yuzhichang merged commit 8327e78 into infiniflow:main Nov 17, 2026
6 checks passed
@xugangqiang
xugangqiang deleted the refactor/esoscommon-shared-helpers branch November 26, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants