Skip to content

Elasticsearch metadata NE filters generate invalid field: NOT value queries #7153

Open

Description

@CryoThrust

Bug description

ElasticsearchAiSearchFilterExpressionConverter places unary NOT after the field colon for NE filters. Elasticsearch rejects the resulting query string, so a metadata != filter fails the entire vector search.

This is separate from the date escaping change in #7146: the plain-string reproduction fails with that change present and with the converter's date output reverted to its base behavior.

Environment

  • Spring AI 2.1.0-SNAPSHOT, Escape dates in Elasticsearch filter queries #7146 head e7cda7beadeacd333de6d96e1aa6ba94d4fb9dba; base 5631edc7281cf6a665f6a32316a2868f8d37308e has the same NE placement.
  • Elasticsearch 9.2.0, real Testcontainers instance, native arm64.
  • JDK 25, compiled for Java 17.
  • FixedDimensionEmbeddingModel: no external embedding provider.

Steps to reproduce

Store three documents with metadata label equal to first, second and third, refresh the index, then search with:

FilterExpressionBuilder b = new FilterExpressionBuilder();
store.similaritySearch(SearchRequest.builder()
        .query("anything")
        .topK(10)
        .similarityThresholdAll()
        .filterExpression(b.ne("label", "second").build())
        .build());

The generated filter is:

metadata.label: NOT "second"

Elasticsearch returns:

[es/search] failed: [search_phase_execution_exception] all shards failed
root cause: [query_shard_exception]
Failed to parse query [metadata.label: NOT "second"]

Expected behavior

The search should return the first and third documents. Negation must apply to a valid field/value clause, rather than appear between the field colon and its value. Missing-field behavior should remain aligned with the existing filter contract.

Minimal Complete Reproducible example

This test can be added to ElasticsearchVectorStoreDateFilterIT from #7146, with label added to its three documents:

@Test
void notEqualWithPlainStringExcludesTheBoundaryDocument() {
    withStore(store -> {
        FilterExpressionBuilder b = new FilterExpressionBuilder();
        List<Document> results = store.similaritySearch(
                request().filterExpression(b.ne("label", "second").build()).build());
        assertThat(ids(results)).containsExactlyInAnyOrder("jan7", "jan9");
    });
}

I executed this against the real database. With #7146 present, the expanded fixture runs 13 cases: 11 pass, and the plain-string/date NE cases error on query parsing. Reverting only the date-escaping production change still makes the plain-string NE case fail with the identical query; the Java Date equality control then also fails as expected. Production was restored afterward. This is a focused converter/vector-store reproduction, not full-repository validation.

Please treat this as a separate follow-up rather than expanding the date fix. A fix should retain coverage for plain strings, dates, grouped expressions and missing metadata fields.

Activity

  1. Emryswu commented on Oct 14, 2026

    @Emryswu

    Hi, I'd like to work on this. I plan to move the negation in front of the field for NE (similar to how NIN is handled), and add converter unit tests plus a Testcontainers IT covering plain strings, dates, grouped expressions and missing metadata fields. I'll open a PR against main shortly.

  2. added a commit that references this issue on Oct 14, 2026
    592c67a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions