Customize Filters
This section details how to apply custom filters to your search query.
KDB.AI combines vector database functionality with traditional kdb database queries. In particular, you can combine a vector similarity search with traditional where
clauses. The filter
parameter is used for applying custom filtering to the query. This is specified using a list of triples where each triple has the form (operator; column name; values).
[("<=", "valFloat", 100), ("within", "qual", [0,2])]
[["<=", "valFloat", 100], ["within", "qual", [0,2]]]
The filters are applied in the order they are defined when selecting from the table.
Supported filter functions
Function | Parameters | Example |
---|---|---|
in |
Filters data that is not in the list of possible alternatives. | ["in", "sym", ["AAPL", "MSFT"]] keeps symbols that are either AAPL or MSFT . |
within |
Keeps numeric data that is within the bounds of a range using inclusive limits. | ["within", "price", [100, 200]] keeps prices that are greater than or equal to 100 but less than or equal to 200. |
< |
Keeps numeric data that is less than a threshold. | ["<", "price", 100] keeps data that is less than 100. |
> |
Keeps numeric data that is greater than a threshold. | [">", "price", 100] keeps data that is greater than 100. |
<= |
Keeps numeric data that is less than or equal to a threshold. | ["<=", "price", 100] keeps data that is less than or equal to 100. |
>= |
Keeps numeric data that is greater than or equal to a threshold. | [">=", "price", 100] keeps data that is greater than or equal to 100. |
= |
Keeps numeric data equals another value. | ["=", "sym", "AAPL"] keeps only AAPL data. |
<> |
Keeps data that is not equal to a value. | ["<>", "sym", "AAPL"] keeps all data that is not AAPL . |
like |
Filters string data that matches a simple expression | ["like", "sym", "A*"] matches any symbols that start with an A . |
Filtered search example
This example demonstrates a filtered query where the similarity search is run against vectors satisfying the filter condition.
documents.search(vectors=[[1.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,1.0]], n=3, filter=[("within","createdDate",["2020.07.10D15:00:00.0000", "2021.07.10D15:00:00.0000"]),("<=", "length", 100)])
curl -s -H "Content-Type: application/json" localhost:8082/api/v1/kxi/search \
-d '{"table":"documents","n":3,"vectors":[[1.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,1.0]],"filter":[["within","createdDate",["2020.07.10D15:00:00.0000", "2021.07.10D15:00:00.0000"]],["<=", "length", 100]]}'
Next steps
Now that you're familiar with filters, you can do the following:
- Learn how to optimize vector search with metadata filtering from the Learning hub.
- Visit our GitHub repo.
- Open the sample.
- Run the notebook directly in Google Colab.