Skip to content

Commit 8067637

Browse files
committed
feat: add filter params in query
1 parent ee357a7 commit 8067637

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/main/java/io/endee/client/Index.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ public List<QueryResult> query(QueryOptions options) {
219219
if (options.getEf() > MAX_EF) {
220220
throw new IllegalArgumentException("ef search cannot be greater than " + MAX_EF);
221221
}
222+
if (options.getPrefilterCardinalityThreshold() < 1_000 || options.getPrefilterCardinalityThreshold() > 1_000_000) {
223+
throw new IllegalArgumentException("prefilterCardinalityThreshold must be between 1,000 and 1,000,000");
224+
}
225+
if (options.getFilterBoostPercentage() < 0 || options.getFilterBoostPercentage() > 100) {
226+
throw new IllegalArgumentException("filterBoostPercentage must be between 0 and 100");
227+
}
222228

223229
boolean hasSparse = options.getSparseIndices() != null && options.getSparseIndices().length > 0
224230
&& options.getSparseValues() != null && options.getSparseValues().length > 0;
@@ -258,6 +264,11 @@ public List<QueryResult> query(QueryOptions options) {
258264
data.put("filter", JsonUtils.toJson(options.getFilter()));
259265
}
260266

267+
Map<String, Object> filterParams = new HashMap<>();
268+
filterParams.put("prefilter_cardinality_threshold", options.getPrefilterCardinalityThreshold());
269+
filterParams.put("filter_boost_percentage", options.getFilterBoostPercentage());
270+
data.put("filter_params", filterParams);
271+
261272
try {
262273
String jsonBody = JsonUtils.toJson(data);
263274
HttpRequest request = buildPostJsonRequest("/index/" + name + "/search", jsonBody);

src/main/java/io/endee/client/types/QueryOptions.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
* }</pre>
2020
*/
2121
public class QueryOptions {
22+
private static final int DEFAULT_PREFILTER_CARDINALITY_THRESHOLD = 10_000;
23+
2224
private double[] vector;
2325
private int topK;
2426
private List<Map<String, Object>> filter;
2527
private int ef = 128;
2628
private boolean includeVectors = false;
2729
private int[] sparseIndices;
2830
private double[] sparseValues;
31+
private int prefilterCardinalityThreshold = DEFAULT_PREFILTER_CARDINALITY_THRESHOLD;
32+
private int filterBoostPercentage = 0;
2933

3034
private QueryOptions() {}
3135

@@ -40,6 +44,8 @@ public static Builder builder() {
4044
public boolean isIncludeVectors() { return includeVectors; }
4145
public int[] getSparseIndices() { return sparseIndices; }
4246
public double[] getSparseValues() { return sparseValues; }
47+
public int getPrefilterCardinalityThreshold() { return prefilterCardinalityThreshold; }
48+
public int getFilterBoostPercentage() { return filterBoostPercentage; }
4349

4450
public static class Builder {
4551
private final QueryOptions options = new QueryOptions();
@@ -86,6 +92,25 @@ public Builder sparseValues(double[] sparseValues) {
8692
return this;
8793
}
8894

95+
/**
96+
* Sets the prefilter cardinality threshold. When the estimated number of
97+
* matching vectors exceeds this value, postfiltering is used instead.
98+
* Must be between 1,000 and 1,000,000. Default: 10,000.
99+
*/
100+
public Builder prefilterCardinalityThreshold(int prefilterCardinalityThreshold) {
101+
options.prefilterCardinalityThreshold = prefilterCardinalityThreshold;
102+
return this;
103+
}
104+
105+
/**
106+
* Sets the filter boost percentage (0-100). Higher values bias results
107+
* toward filter matches. Default: 0.
108+
*/
109+
public Builder filterBoostPercentage(int filterBoostPercentage) {
110+
options.filterBoostPercentage = filterBoostPercentage;
111+
return this;
112+
}
113+
89114
public QueryOptions build() {
90115
return options;
91116
}

0 commit comments

Comments
 (0)