Skip to content

Metadata filter: negative operators (is not, ≠, not contains, not in) exclude documents that don't have the field — intended? #44752

Closed

Description

@cty-ut

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.17.1 (also reproducible on current main)

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. Create a knowledge base with two metadata fields: writer (string) and year (number).

  2. Add three documents:

    document metadata
    alice writer = "Alice", year = 2023
    bob writer = "Bob", year = 2025
    missing no metadata values set
  3. Filter with each condition below.

condition value current result
writer is not Alice {bob}
year ≠ 2023 {bob}
writer not contains Ali {bob}
writer not in Alice {bob}
writer is Alice {alice}
writer in Alice {alice}
year > 2024 {bob}

missing is returned by none of the negative operators.

Cause. writer is not "Alice" is compiled to:

WHERE (documents.doc_metadata ->> 'writer') != 'Alice'

For missing, ->> 'writer' is NULL, so the predicate is NULL != 'Alice', which evaluates to unknown rather than TRUE. WHERE keeps only TRUE rows, so the document is excluded. NOT LIKE and NOT IN behave the same way.

✔️ Expected Behavior

This is a question about intended semantics rather than a clear bug. There are two readings:

  • A (current): "is not Alice" means the writer is known and is not Alice. A missing value is unknown, so the document is excluded.
  • B: "is not Alice" means there is no indication that the writer is Alice. A missing value is not Alice, so the document is included, and the four negative operators would return {bob, missing}.

Reasons I think B fits better:

  1. empty already treats a missing key as an empty value. missing matches writer empty. An empty value is clearly not "Alice", yet missing fails writer is not "Alice".
  2. is and is not should be complementary. Users expect is X and is not X to split the documents into two groups, but missing currently matches neither. Filtering out Alice's documents also silently drops every document whose metadata hasn't been filled in.
  3. Other systems behave like B. In MongoDB, $ne and $nin match documents that do not contain the field. In Elasticsearch, bool.must_not with a term query returns documents that lack the field.

❌ Actual Behavior

Documents without the metadata key are excluded by is not / ≠ / not contains / not in, as shown above.
Question: Is reading A intended? If B is preferred, I've opened #44753 which adds OR <field> IS NULL to these four operators only; >/< and the positive operators are unchanged. Happy to close it if the current behavior is intended.

This affects knowledge retrieval in chat apps, the Knowledge Retrieval node in workflows, and hit testing, since they share process_metadata_filter_func.

Related: #43346 changes empty / not empty in the same function; this is independent of it.

Activity

  1. added
    🐞 bug
    on Oct 25, 2026
  2. asukaminato0721 commented on Oct 25, 2026

    @asukaminato0721
    Contributor

    @dosu what do you think

  3. dosubot commented on Oct 25, 2026

    @dosubot

    I’d favor B as the product semantics, but treat it as an intentional behavior change rather than an unquestionable bug fix.

    For a user-facing exclusion filter, I think “exclude Alice” should leave documents with no writer value eligible. Making positive and negative filters complementary for non-null comparison values gives a clearer rule than exposing SQL’s three-valued logic. There is already a precedent in this function: not in [] explicitly evaluates to TRUE, even for missing metadata. [1]

    I’d define the contract explicitly:

    • Missing keys and JSON null match the four negative operators.
    • Positive operators and ordered comparisons stay unchanged.
    • Blank strings remain actual string values for comparisons, rather than being automatically equated with missing metadata. Whether empty also matches them is a separate decision.

    The compatibility caveat matters: the existing predicates exclude SQL NULL values, so adopting B would broaden the eligible document set for existing negative filters. I’d document that change in the release notes and pin down missing-key, JSON-null, blank-string, and populated-value behavior in regression tests before shipping it. [2]

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about dify

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions