Skip to content

Exclusion pattern not working in extended search #712

Closed as not planned
Closed as not planned

Description

@Dhron

Describe the bug

Pattern for exclusion not excluding keywords.

Version

6.6.2

Is this a regression?

Not sure.

🔬Minimal Reproduction

Using the live demo:

  1. Set useExtendedSearch: true
  2. Add pattern '!Syrup'
  3. Scroll down to last element returned in list.

image

Activity

  1. nichita-pasecinic commented on Feb 13, 2023

    @nichita-pasecinic

    I'm facing the same issue!

  2. github-actions commented on Jun 14, 2023

    @github-actions

    This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 30 days

  3. nichita-pasecinic commented on Jul 19, 2023

    @nichita-pasecinic

    why closing though ?

  4. added a commit that references this issue on Apr 2, 2026
  5. krisk commented on Apr 3, 2026

    @krisk
    Owner

    This is a known limitation of how extended search interacts with multiple keys. The ! operator works correctly per-field, but the cross-key aggregation uses "include if ANY key matches" — which is wrong for inverse patterns since almost every item has at least one field that doesn't contain the excluded term.

    The workaround is to use logical queries, which give you explicit control over which keys are searched:

    // Instead of: fuse.search('!Syrup')
    fuse.search({
      $and: [
        { title: '!Syrup' },
        { 'author.firstName': '!Syrup' }
      ]
    })

    Fixing this in the general case (especially mixed queries like ^hello !syrup) requires architectural changes to how the searcher communicates match types back to the aggregation layer. Leaving this open as a known issue.

  6. krisk commented on Apr 3, 2026

    @krisk
    Owner

    Some more context on why this is hard to fix:

    The core issue is that _searchObjectList searches each key independently via searcher.searchIn(text) and includes the item if ANY key returns isMatch: true. This is correct for positive patterns but wrong for inverse ones:

    Positive hello with keys [title, author]:

    • title="hello world" → isMatch: true
    • author="Bob Smith" → isMatch: false
    • → include (correct: found in at least one key)

    Inverse !Syrup with keys [title, author]:

    • title="Maple Syrup Pancakes" → isMatch: false (contains Syrup)
    • author="Chef Bob" → isMatch: true (no Syrup)
    • → include (wrong: should exclude because title contains Syrup)

    The fix requires "ANY key" aggregation for positive patterns but "ALL keys" for inverse. However, searchIn() returns a single { isMatch, score } with no information about whether the result came from a positive or inverse match.

    For mixed patterns like ^hello !Syrup, it gets worse — a key could fail because of the positive term OR the inverse term, and we can't tell which. We'd need ExtendedSearch.searchIn to return per-term results, which means redesigning the Searcher interface.

    This is documented in the source at _searchObjectList in src/core/index.ts.

  7. added a commit that references this issue on Apr 3, 2026
    9351882
  8. krisk commented on Apr 3, 2026

    @krisk
    Owner

    Fixed in 9351882. ExtendedSearch.searchIn now sets hasInverse on the result when inverse matchers are involved, and _searchObjectList uses this to switch from "ANY key matches" to "ALL keys must match" aggregation.

    !Syrup with keys [title, author] now correctly excludes items where Syrup appears in any searched field.

  9. krisk commented on Apr 3, 2026

    @krisk
    Owner

    Update: disregard my earlier comments about this being an architectural limitation — I found a clean solution.

    The fix adds a hasInverse flag to search results. When ExtendedSearch detects inverse matchers in the query, it signals this to the aggregation layer, which then requires ALL keys to match (instead of ANY). Pure inverse queries like !Syrup are fully fixed. Mixed queries like ^hello !Syrup conservatively exclude on any key failure, which is the correct behavior in practice.

    The logical query workaround is no longer needed for this case, though it remains available for more fine-grained control.

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