Skip to content

Fix Elasticsearch NE filter producing invalid query string - #7158

Open
Emryswu wants to merge 1 commit into
spring-projects:mainfrom
Emryswu:fix/elasticsearch-ne-filter
Open

Emryswu wants to merge 1 commit into
spring-projects:mainfrom
Emryswu:fix/elasticsearch-ne-filter

Conversation

@Emryswu

@Emryswu Emryswu commented Oct 14, 2026

Copy link
Copy Markdown

Fixes #7153

The Elasticsearch converter emitted NE as metadata.label: NOT "second", which query_string rejects.
Handle NE separately and emit NOT metadata.label:"second", following the existing NIN placement.

Wrap both OR operands in parentheses: A OR NOT B makes B a MUST_NOT clause of the entire Boolean query, excluding B even when A matches.
(A) OR (NOT B) keeps the negation inside its operand.
The converter retains the inherited doNot implementation; the shared double-negation problem is outside this change.

Regression coverage and validation

On Ubuntu WSL with OpenJDK 21.0.12 and Maven 3.9.16, compiling for Java 17, a clean reactor build and verification completed successfully with the build cache and automatic source formatting disabled.
All 21 converter unit tests and all 5 ElasticsearchVectorStoreFilterIT cases passed with zero failures, errors, or skips.
The integration tests ran against a real Elasticsearch 9.2.0 instance managed by Testcontainers and used a fixed embedding model with no external service calls.
Coverage includes string and numeric NE, AND, both OR operand orders, grouping, not(group(eq)), and matching NE/NIN behavior when metadata is missing.
Formatting and Checkstyle checks also passed.

Missing metadata and date scope

The integration test confirms that both NE and NIN include a document without the label field, alongside first and third, while excluding second.
This preserves Elasticsearch's existing NIN complement behavior and matches the inspected MongoDB, Qdrant, and SimpleVectorStore implementations.
The common filter API does not define a universal missing-field contract; PgVector delegates inequality to native JSONPath, whose absent-path behavior can exclude a document.
No existence condition or cross-provider semantic normalization is added.

Date NE unit tests cover Java Date and ISO date values while retaining main's existing serialization.
The independent date escaping problem tracked by #7146 remains outside this change, and no code from that unmerged PR is used.

Contribution checks

  • One commit with a DCO Signed-off-by trailer.
  • Based on the latest main, with changes squashed into one commit.
  • Converter unit tests and integration regression tests added or updated.
  • Local build, module unit tests, and the related integration tests passed.

Place NE negation before the metadata field and isolate OR operands.
Add converter tests and Testcontainers filter regression coverage.

Fixes spring-projects#7153

Signed-off-by: Emrys <[email protected]>
@Chandra324

Copy link
Copy Markdown

Reproduced the OR-operand case on Elasticsearch 9.2.0 (Lucene 10.3.1) with the query string from ElasticsearchAiSearchFilterExpressionConverter at 97219e5c2 (Spring AI 2.1.0-SNAPSHOT).

Filter: year >= 2020 OR (country == "BG" AND city != "Sofia").

Query string: metadata.year:>=2020 OR metadata.country:"BG" AND NOT metadata.city:"Sofia".

Elasticsearch explains that as year:[2020 TO *] +country:BG -city:Sofia.

Indexed documents: year-sofia (2021, BG, Sofia), bg-plovdiv (2019, BG, Plovdiv), old-sofia (2010, BG, Sofia), other (2022, NL, Sofia).

That query returned only bg-plovdiv. The grouped form (metadata.year:>=2020) OR (metadata.country:"BG" AND NOT metadata.city:"Sofia") returned year-sofia, other, and bg-plovdiv.

ElasticsearchAiSearchFilterExpressionConverterTest on that commit: 16 tests, 0 failures, 0 errors, BUILD SUCCESS (JDK 21.0.12.1, Maven 3.9.16). The unit test asserts the ungrouped string, so it passes while Elasticsearch still returns the wrong document.

@Emryswu

Emryswu commented Nov 19, 2026

Copy link
Copy Markdown
Author

Thanks for reproducing this on Elasticsearch 9.2.0, @Chandra324! The explain output makes it really clear why the OR operands need parentheses here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Elasticsearch metadata NE filters generate invalid field: NOT value queries

3 participants