Skip to content

Conversation

@abrookins
Copy link
Collaborator

Summary

Fixes #557 - Using an OR expression with a KNN expression was raising ResponseError: Syntax error at offset.

Problem

When combining an OR expression like (Album.tags == "x") | (Album.tags == "y") with a KNN expression, the generated query was invalid:

(@tags:{x})| (@tags:{y})=>[KNN $K @embedding $vec AS score]

The issue is that the KNN suffix =>[KNN ...] was only being applied to the second term of the OR expression, not the entire filter. RediSearch requires the filter to be a single expression before the KNN syntax.

Solution

Always wrap the filter expression in parentheses when appending KNN syntax (unless it's the wildcard *):

((@tags:{x})| (@tags:{y}))=>[KNN $K @embedding $vec AS score]

The previous logic checked startswith("(") to determine if wrapping was needed, but this was insufficient for OR expressions that happen to start with ( but aren't fully wrapped.

Changes

  • Fixed FindQuery.query property to always wrap filter expressions in parentheses when combining with KNN
  • Added test_or_expression_with_knn test that reproduces the exact scenario from the issue

Testing

The test creates albums with different tags, then queries using:

  1. OR expression only (works)
  2. KNN only (works)
  3. OR expression + KNN (was failing, now works)

abrookins and others added 3 commits January 23, 2026 11:45
When combining an OR expression like (A) | (B) with a KNN expression,
the generated query was invalid because the KNN suffix was only applied
to the second term: (A)| (B)=>[KNN ...]

The fix ensures the entire filter expression is wrapped in parentheses
before appending the KNN syntax: ((A)| (B))=>[KNN ...]

Fixes #557
@abrookins abrookins merged commit a134482 into main Jan 23, 2026
15 checks passed
@abrookins abrookins deleted the fix/557-or-knn-syntax-error branch January 23, 2026 20:10
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.

Using an or expression with a knn expression raises a ResponseError with a Syntax error

2 participants