|
| 1 | +/* |
| 2 | + * Copyright 2023 - 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.ai.vectorstore; |
| 17 | + |
| 18 | +import java.text.ParseException; |
| 19 | +import java.text.SimpleDateFormat; |
| 20 | +import java.util.Date; |
| 21 | +import java.util.List; |
| 22 | +import java.util.TimeZone; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +import org.springframework.ai.vectorstore.filter.Filter; |
| 26 | +import org.springframework.ai.vectorstore.filter.Filter.Expression; |
| 27 | +import org.springframework.ai.vectorstore.filter.Filter.Key; |
| 28 | +import org.springframework.ai.vectorstore.filter.converter.AbstractFilterExpressionConverter; |
| 29 | + |
| 30 | +/** |
| 31 | + * GemFireAiSearchFilterExpressionConverter is a class that converts Filter.Expression |
| 32 | + * objects into GemFire VectorDB query string representation. It extends the |
| 33 | + * AbstractFilter ExpressionConverter class. |
| 34 | + * |
| 35 | + * @author Jason Huynh |
| 36 | + */ |
| 37 | +public class GemFireAiSearchFilterExpressionConverter extends AbstractFilterExpressionConverter { |
| 38 | + |
| 39 | + private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); |
| 40 | + |
| 41 | + private final SimpleDateFormat dateFormat; |
| 42 | + |
| 43 | + public GemFireAiSearchFilterExpressionConverter() { |
| 44 | + this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
| 45 | + this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void doExpression(Expression expression, StringBuilder context) { |
| 50 | + if (expression.type() == Filter.ExpressionType.IN || expression.type() == Filter.ExpressionType.NIN) { |
| 51 | + context.append(getOperationSymbol(expression)); |
| 52 | + this.convertOperand(expression.left(), context); |
| 53 | + context.append("("); |
| 54 | + this.convertOperand(expression.right(), context); |
| 55 | + context.append(")"); |
| 56 | + } |
| 57 | + else if (expression.type() == Filter.ExpressionType.GT || expression.type() == Filter.ExpressionType.GTE) { |
| 58 | + this.convertOperand(expression.left(), context); |
| 59 | + context.append(getOperationSymbol(expression)); |
| 60 | + this.convertOperand(expression.right(), context); |
| 61 | + context.append(" TO *]"); |
| 62 | + } |
| 63 | + else if (expression.type() == Filter.ExpressionType.LT || expression.type() == Filter.ExpressionType.LTE) { |
| 64 | + this.convertOperand(expression.left(), context); |
| 65 | + context.append("[* TO "); |
| 66 | + this.convertOperand(expression.right(), context); |
| 67 | + context.append(getOperationSymbol(expression)); |
| 68 | + } |
| 69 | + else { |
| 70 | + this.convertOperand(expression.left(), context); |
| 71 | + context.append(getOperationSymbol(expression)); |
| 72 | + this.convertOperand(expression.right(), context); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void doStartValueRange(Filter.Value listValue, StringBuilder context) { |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void doEndValueRange(Filter.Value listValue, StringBuilder context) { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void doAddValueRangeSpitter(Filter.Value listValue, StringBuilder context) { |
| 86 | + context.append(" OR "); |
| 87 | + } |
| 88 | + |
| 89 | + private String getOperationSymbol(Expression exp) { |
| 90 | + return switch (exp.type()) { |
| 91 | + case AND -> " AND "; |
| 92 | + case OR -> " OR "; |
| 93 | + case EQ, IN -> ""; |
| 94 | + case NE -> " NOT "; |
| 95 | + case LT -> "}"; |
| 96 | + case LTE -> "]"; |
| 97 | + case GT -> "{"; |
| 98 | + case GTE -> "["; |
| 99 | + case NIN -> "NOT "; |
| 100 | + default -> throw new RuntimeException("Not supported expression type: " + exp.type()); |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void doKey(Key key, StringBuilder context) { |
| 106 | + var identifier = hasOuterQuotes(key.key()) ? removeOuterQuotes(key.key()) : key.key(); |
| 107 | + context.append(identifier.trim()).append(":"); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void doValue(Filter.Value filterValue, StringBuilder context) { |
| 112 | + if (filterValue.value() instanceof List list) { |
| 113 | + int c = 0; |
| 114 | + for (Object v : list) { |
| 115 | + context.append(v); |
| 116 | + if (c++ < list.size() - 1) { |
| 117 | + this.doAddValueRangeSpitter(filterValue, context); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + else { |
| 122 | + this.doSingleValue(filterValue.value(), context); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected void doSingleValue(Object value, StringBuilder context) { |
| 128 | + if (value instanceof Date date) { |
| 129 | + context.append(this.dateFormat.format(date)); |
| 130 | + } |
| 131 | + else if (value instanceof String text) { |
| 132 | + if (DATE_FORMAT_PATTERN.matcher(text).matches()) { |
| 133 | + try { |
| 134 | + Date date = this.dateFormat.parse(text); |
| 135 | + context.append(this.dateFormat.format(date)); |
| 136 | + } |
| 137 | + catch (ParseException e) { |
| 138 | + throw new IllegalArgumentException("Invalid date type:" + text, e); |
| 139 | + } |
| 140 | + } |
| 141 | + else { |
| 142 | + context.append(text); |
| 143 | + } |
| 144 | + } |
| 145 | + else { |
| 146 | + context.append(value); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void doStartGroup(Filter.Group group, StringBuilder context) { |
| 152 | + context.append("("); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void doEndGroup(Filter.Group group, StringBuilder context) { |
| 157 | + context.append(")"); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments