Skip to content

Commit da347c3

Browse files
committed
Fix escaping issue in filterExpression for RedisVectorStore file name filtering
Signed-off-by: Minu Kim <[email protected]>
1 parent b219c21 commit da347c3

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SearchRequest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
2525
import org.springframework.lang.Nullable;
2626
import org.springframework.util.Assert;
27+
import java.util.regex.Pattern;
28+
import java.util.regex.Matcher;
2729

2830
/**
2931
* Similarity search request. Use the {@link SearchRequest#builder()} to create the
@@ -283,10 +285,22 @@ public Builder filterExpression(@Nullable Filter.Expression expression) {
283285
*/
284286
public Builder filterExpression(@Nullable String textExpression) {
285287
this.searchRequest.filterExpression = (textExpression != null)
286-
? new FilterExpressionTextParser().parse(textExpression) : null;
288+
? new FilterExpressionTextParser().parse(escapeTextExpression(textExpression)) : null;
287289
return this;
288290
}
289291

292+
private String escapeTextExpression(String expression) {
293+
Pattern pattern = Pattern.compile("'([^']*)'");
294+
Matcher matcher = pattern.matcher(expression);
295+
StringBuffer sb = new StringBuffer();
296+
while (matcher.find()) {
297+
String content = matcher.group(1).replace("\\", "\\\\").replace(".", "\\.");
298+
matcher.appendReplacement(sb, "'" + content + "'");
299+
}
300+
matcher.appendTail(sb);
301+
return sb.toString();
302+
}
303+
290304
public SearchRequest build() {
291305
return this.searchRequest;
292306
}

0 commit comments

Comments
 (0)