Skip to content

fix(es): match exact date metadata values on date-typed fields - #20532

Open
Xavierando wants to merge 1 commit into
infiniflow:mainfrom
Xavierando:fix/go-es-date-metadata-filter
Open

Xavierando wants to merge 1 commit into
infiniflow:mainfrom
Xavierando:fix/go-es-date-metadata-filter

Conversation

@Xavierando

@Xavierando Xavierando commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #16832: the Go fix for its exact-match part, as requested on #16833 ("please make modifications in Go first"). The other two points are covered below.

The document-metadata index maps meta_fields dynamically (conf/doc_meta_es_mapping.json, "dynamic": true), so Elasticsearch date detection maps a field whose values look like YYYY-MM-DD as date. A date field has no .keyword sub-field, but the metadata push-down builds every string equality as a term on <field>.keyword (termOrMatch in internal/engine/elasticsearch/meta_filter.go, used by =, ≠ and the string members of in/not in). So = and in on a date value match nothing. An empty push-down result is final (no in-memory fallback), so the filter returns no documents. This hits every caller of the metadata push-down: metadata_condition on POST /api/v1/retrieval, chat meta_data_filter, the agent Retrieval component and the agentic RAG metadata search.

Fix: for a value matching the existing dateRegex (^\d{4}-\d{2}-\d{2}$), termOrMatch emits a bool.should over a term on the field itself and the existing case-insensitive term on .keyword, minimum_should_match: 1. That matches whether the field was mapped as date or as text+keyword. Non-date strings and numbers produce the same clause as before.

The other two points of #16832 on the Go path:

How this was tested

  1. Unit tests. bash build.sh --test ./internal/engine/elasticsearch/... → ok. New translator tests: =, ≠ and in on date values (the first three fail on main, where the clause is a term on meta_fields.<field>.keyword only) and a guard that a plain string keeps the single .keyword term.

  2. Elasticsearch 8.11.3, real data. A self-hosted deployment (v1.0.0-rc1 base, ~13 000 documents with metadata). The tenant's ragflow_doc_meta_<tenant> index has three dynamically mapped date fields with no .keyword (date, data_pubblicazione, data); all other metadata fields are text + keyword. _count with the clause from main and with the clause from this PR:

    field = value main: term <field>.keyword this PR: should[term <field>, term <field>.keyword]
    date = 2026-04-22 0 34
    data_pubblicazione = 2026-07-24 0 13
  3. End to end on the same deployment with this change built in, POST /api/v1/retrieval on one dataset with metadata_condition on the date field:

    condition total documents returned
    = "2026-04-22" 40 6, all with date = 2026-04-22 in the metadata index
    in ["2026-04-22", "2026-04-23"] 64 (candidate window) 16
    = "1999-01-01" 0 0

Known limits, left out to keep this minimal:

  • A value that matches the pattern but is not a valid date (2026-02-30) makes Elasticsearch reject the term on a date field. The push-down then errors and the caller falls back to the in-memory filter, so the result is still correct, just not pushed down.
  • contains / start with / end with still target .keyword, so on a date-typed field they return nothing through the push-down. Uncommon for dates.
  • Only plain YYYY-MM-DD values get the extra clause. A value with a time of day (2026-07-23T10:30:00Z) does not match dateRegex and still targets .keyword only.
  • Infinity's push-down is JSON-based and unaffected.

The doc-metadata index maps meta_fields dynamically, so ES date detection
types a YYYY-MM-DD field as `date`. A date field has no `.keyword`
sub-field, and the push-down built every string equality as a term on
`<field>.keyword`: `=` and `in` on a date matched nothing, and because an
empty push-down result is definitive there was no in-memory fallback.

Exact-match clauses for ISO-date values now match either the field itself
or its `.keyword` sub-field, which covers both the date-typed and the
text-typed mapping.
@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 1c7f7cd1-9b50-4970-94ec-984749513138


📥 Commits

Reviewing files that changed from the base of the PR and between 98b48a0 and 78cbb4e.


📒 Files selected for processing (2)
  • internal/engine/elasticsearch/meta_filter.go
  • internal/engine/elasticsearch/meta_filter_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.



📝 Walkthrough

Walkthrough

Elasticsearch metadata query translation now matches ISO-date strings against both the field and its .keyword subfield. Tests cover date equality, inequality, and in queries, along with plain-string equality.

Changes

Metadata query translation

Layer / File(s) Summary
Date-aware exact-match translation
internal/engine/elasticsearch/meta_filter.go, internal/engine/elasticsearch/meta_filter_test.go
termOrMatch adds a field-level term alternative for strings matching YYYY-MM-DD. Tests check equality, inequality, and in translation for dates, and keyword-term translation for plain strings.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix


Merge Risk | ⚪ Minimal · up to 78cbb

Merge Risk: ⚪ Minimal · up to 78cbb

This change makes exact date metadata filters match date-typed Elasticsearch fields. It leaves other filter behavior unchanged. No merge-blocking risk was identified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 78cbb

Date filters now support both storage representations while preserving the search-scope restriction in the inspected path. No newly bypassed control was established, but end-to-end authorization and fallback behavior remain incompletely verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected search path limits the changed matching behavior to a tenant metadata index and the supplied knowledge-base IDs. This bounds query execution but does not establish that every external caller is authorized to supply those IDs.

Trust Boundaries and Controls

  • observed — The translator validates supported operators and required values before constructing structured query maps. Condition values remain data in JSON-encoded term queries rather than being interpolated into executable query text.

Resilience and Maintainability Implications

  • observed — The existing search consumer returns nil on query or search failures and on potentially incomplete capped results. Its comments describe in-memory fallback, but downstream enforcement of that fallback was not hydrated; this is an exposure gap, not an established PR-introduced fail-open path.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check Passed The title clearly and concisely describes the primary change: fixing exact date metadata matching on date-typed Elasticsearch fields.
Description check Passed The description includes the required Summary section, explains the problem and fix, documents testing, and identifies known limits. It is complete and directly related to the pull request.

  • Fix all pre-merge checks with AI
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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

I’m a rabbit with dates in my query patch,
Two field paths help the right values match.
Equality and in hop into view,
Inequality joins the testing crew.
Plain strings keep their keyword track,
I nibble a carrot and hop right back.

Comment @coderabbitai help to get the list of available commands.

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.

[Bug]: Elasticsearch metadata date filtering — exact-match "=" returns 0 and LLM range operators are silently dropped

2 participants