Zvec Logo

Conditional Filtering

Conditional filtering lets you retrieve documents that match specific criteria based on their scalar fields — similar to a WHERE clause in SQL.


Performance Considerations

  • Indexed scalar fields: Can be efficiently searched.
  • Unindexed scalar fields: Can still be searched, but with significantly lower performance.

For optimal performance, ensure frequently filtered fields are indexed.
For more details, please see the Inverted Index.


Prerequisites

This guide assumes you have opened a collection containing scalar fields.


Performing Conditional Filtering

Apply conditional filtering by passing a filter expression to the query() method.

The expression uses SQL-like syntax to define search conditions.

The topk parameter specifies the maximum number of matching documents to return.

If more documents satisfy the filter than the specified topk, only the first topk results are returned.
Results are returned in no guaranteed order (typically in internal storage order).

The query() method with a filter returns a list of matching Doc objects.

Each Doc object includes:

  1. id: The document identifier.
  2. vectors: A dictionary of vector embeddings.
    This is only populated if include_vector=True is passed in the query.
  3. fields: A dictionary of scalar field values.
    By default, all scalar fields are returned; this can be restricted using the output_fields parameter.

Supported Filter Syntax

Comparison Operators

OperatorDescriptionSupported Data TypesExample Expression
<Less thanIntegers, Floats, Stringspublish_year < 2000
<=Less than or equal toIntegers, Floats, Stringsprice <= 29.99
author_name <= 'M'
=Equal toIntegers, Floats, Strings, Booleansin_stock = true
name = 'Michael'
!=Not equal toIntegers, Floats, Strings, Booleansrating != 5
status != 'active'
>=Greater than or equal toIntegers, Floats, Stringsscore >= 85.5
>Greater thanIntegers, Floats, Stringsage > 12
is nullChecks if a field has no valueAll data typesemail is null
is not nullChecks if a field has a valueAll data typesemail is not null
  • String comparisons use lexicographic ordering: 'apple' < 'banana' evaluates to true
  • String literals must be enclosed in single (') or double (") quotes: 'hello world'
  • Boolean comparisons use the keywords true and false (case-insensitive)
  • Range queries: When enable_range_optimization is set to true, range queries run efficiently. If it's false, they still work — but may be significantly slower.

Membership Operators

OperatorDescriptionSupported Data TypesExample ExpressionExplanation
inIs one ofIntegers, Floats, Stringserror_code in (400, 403, 404)
user_name in ('admin', 'root')
Evaluates to true if the field value matches any of the listed values.

error_code is 400, 403, or 404true
user_name is 'admin' or 'root'true
not inIs not one ofIntegers, Floats, Stringsstatus not in ('deleted', 'archived')Evaluates to true if the field value does not match any of the listed values.

status is anything other than 'deleted' or 'archived'true
contain_allArray includes all listed valuesArray Typestags contain_all ('urgent', 'bug')Evaluates to true only if the array contains every value in the list.

tags = ['bug', 'urgent', 'ui']true
tags = ['bug', 'ui']false (missing 'urgent')
contain_anyArray includes at least one of the listed valueArray Typespermissions contain_any ('execute', 'write')Evaluates to true if the array contains at least one of the listed values.

permissions = ['admin', 'execute']true
permissions = ['read', 'forbidden']false (neither 'execute' nor 'write')
array_lengthArray lengthArray Typesarray_length(tags) > 2Evaluates to true if the array's length satisfies the condition.

tags = ['bug']false (length is 1)
tags = ['bug', 'urgent', 'fix']true (length is 3)
  • Parentheses () are required around the list of values.
  • String literals must be enclosed in single (') or double (") quotes: 'hello world'

String Operators

OperatorDescriptionSupported Data TypesExample ExpressionExplanation
likePattern match with wildcardsStringsproduct_name like 'Smart%'
file_name like '%.log'
'Smart%': matches values starting with 'Smart' (e.g., 'SmartPhone', 'SmartWatch')
'%.log': matches values ending with '.log' (e.g., 'app.log', 'debug_2025.log')

Performance Considerations:
For optimal LIKE query performance, fields should be indexed.

  • Unindexed fields still support filtering, but queries may be significantly slower.
  • For efficient infix/suffix patterns ('abc%def', '%abc'), configure an inverted index with enable_extended_wildcard = true option.
  • Patterns with multiple wildcards (e.g., '%abc%def%') are inherently expensive and should be used sparingly.

Logical Operators

OperatorDescriptionExample ExpressionExplanation
andLogical ANDstatus = 'active' and score > 90Evaluates to true only if all conditions are true.
orLogical ORrole = 'admin' or permission = 'write'Evaluates to true if at least one condition is true.

Tip: Use parentheses () to group expressions and control evaluation order, e.g., expr1 and (expr2 or expr3).