|
45 | 45 | * List<Document> documents = retriever.retrieve(new Query("example query"));
|
46 | 46 | * }</pre>
|
47 | 47 | *
|
| 48 | + * <p> |
| 49 | + * The {@link #FILTER_EXPRESSION} context key can be used to provide a filter expression |
| 50 | + * for a specific query. This key accepts either a string representation of a filter |
| 51 | + * expression or a {@link Filter.Expression} object directly. |
| 52 | + * |
48 | 53 | * @author Thomas Vitale
|
49 | 54 | * @since 1.0.0
|
50 | 55 | */
|
@@ -89,10 +94,27 @@ public List<Document> retrieve(Query query) {
|
89 | 94 | return this.vectorStore.similaritySearch(searchRequest);
|
90 | 95 | }
|
91 | 96 |
|
| 97 | + /** |
| 98 | + * Computes the filter expression to use for the current request. |
| 99 | + * <p> |
| 100 | + * The filter expression can be provided in the query context using the |
| 101 | + * {@link #FILTER_EXPRESSION} key. This key accepts either a string representation of |
| 102 | + * a filter expression or a {@link Filter.Expression} object directly. |
| 103 | + * <p> |
| 104 | + * If no filter expression is provided in the context, the default filter expression |
| 105 | + * configured for this retriever is used. |
| 106 | + * @param query the query containing potential context with filter expression |
| 107 | + * @return the filter expression to use for the request |
| 108 | + */ |
92 | 109 | private Filter.Expression computeRequestFilterExpression(Query query) {
|
93 | 110 | var contextFilterExpression = query.context().get(FILTER_EXPRESSION);
|
94 |
| - if (contextFilterExpression != null && StringUtils.hasText(contextFilterExpression.toString())) { |
95 |
| - return new FilterExpressionTextParser().parse(contextFilterExpression.toString()); |
| 111 | + if (contextFilterExpression != null) { |
| 112 | + if (contextFilterExpression instanceof Filter.Expression) { |
| 113 | + return (Filter.Expression) contextFilterExpression; |
| 114 | + } |
| 115 | + else if (StringUtils.hasText(contextFilterExpression.toString())) { |
| 116 | + return new FilterExpressionTextParser().parse(contextFilterExpression.toString()); |
| 117 | + } |
96 | 118 | }
|
97 | 119 | return this.filterExpression.get();
|
98 | 120 | }
|
|
0 commit comments