|
29 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 30 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.AND;
|
31 | 31 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.EQ;
|
| 32 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GT; |
32 | 33 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GTE;
|
33 | 34 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.IN;
|
| 35 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LT; |
34 | 36 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LTE;
|
35 | 37 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NE;
|
36 | 38 | import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NIN;
|
@@ -123,4 +125,172 @@ public void testComplexIdentifiers() {
|
123 | 125 | assertThat(vectorExpr).isEqualTo("{\"metadata.country 1 2 3\":{$eq:\"BG\"}}");
|
124 | 126 | }
|
125 | 127 |
|
| 128 | + @Test |
| 129 | + public void testLt() { |
| 130 | + // value < 100 |
| 131 | + String vectorExpr = this.converter.convertExpression(new Expression(LT, new Key("value"), new Value(100))); |
| 132 | + assertThat(vectorExpr).isEqualTo("{\"metadata.value\":{$lt:100}}"); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testLte() { |
| 137 | + // value <= 100 |
| 138 | + String vectorExpr = this.converter.convertExpression(new Expression(LTE, new Key("value"), new Value(100))); |
| 139 | + assertThat(vectorExpr).isEqualTo("{\"metadata.value\":{$lte:100}}"); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testGt() { |
| 144 | + // value > 100 |
| 145 | + String vectorExpr = this.converter.convertExpression(new Expression(GT, new Key("value"), new Value(100))); |
| 146 | + assertThat(vectorExpr).isEqualTo("{\"metadata.value\":{$gt:100}}"); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testNin() { |
| 151 | + // region not in ["A", "B", "C"] |
| 152 | + String vectorExpr = this.converter |
| 153 | + .convertExpression(new Expression(NIN, new Key("region"), new Value(List.of("A", "B", "C")))); |
| 154 | + assertThat(vectorExpr).isEqualTo("{\"metadata.region\":{$nin:[\"A\",\"B\",\"C\"]}}"); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testComplexNestedGroups() { |
| 159 | + // ((value >= 100 AND type == "primary") OR (value <= 50 AND type == "secondary")) |
| 160 | + // AND region == "X" |
| 161 | + String vectorExpr = this.converter.convertExpression(new Expression(AND, |
| 162 | + new Group(new Expression(OR, |
| 163 | + new Group(new Expression(AND, new Expression(GTE, new Key("value"), new Value(100)), |
| 164 | + new Expression(EQ, new Key("type"), new Value("primary")))), |
| 165 | + new Group(new Expression(AND, new Expression(LTE, new Key("value"), new Value(50)), |
| 166 | + new Expression(EQ, new Key("type"), new Value("secondary")))))), |
| 167 | + new Expression(EQ, new Key("region"), new Value("X")))); |
| 168 | + |
| 169 | + assertThat(vectorExpr).isEqualTo( |
| 170 | + "{$and:[{$or:[{$and:[{\"metadata.value\":{$gte:100}},{\"metadata.type\":{$eq:\"primary\"}}]},{$and:[{\"metadata.value\":{$lte:50}},{\"metadata.type\":{$eq:\"secondary\"}}]}]},{\"metadata.region\":{$eq:\"X\"}}]}"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testNullValue() { |
| 175 | + // status == null |
| 176 | + String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("status"), new Value(null))); |
| 177 | + assertThat(vectorExpr).isEqualTo("{\"metadata.status\":{$eq:null}}"); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testEmptyString() { |
| 182 | + // name == "" |
| 183 | + String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("name"), new Value(""))); |
| 184 | + assertThat(vectorExpr).isEqualTo("{\"metadata.name\":{$eq:\"\"}}"); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testNumericString() { |
| 189 | + // id == "12345" |
| 190 | + String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("id"), new Value("12345"))); |
| 191 | + assertThat(vectorExpr).isEqualTo("{\"metadata.id\":{$eq:\"12345\"}}"); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testLongValue() { |
| 196 | + // timestamp >= 1640995200000L |
| 197 | + String vectorExpr = this.converter |
| 198 | + .convertExpression(new Expression(GTE, new Key("timestamp"), new Value(1640995200000L))); |
| 199 | + assertThat(vectorExpr).isEqualTo("{\"metadata.timestamp\":{$gte:1640995200000}}"); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void testFloatValue() { |
| 204 | + // score >= 4.5f |
| 205 | + String vectorExpr = this.converter.convertExpression(new Expression(GTE, new Key("score"), new Value(4.5f))); |
| 206 | + assertThat(vectorExpr).isEqualTo("{\"metadata.score\":{$gte:4.5}}"); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void testMixedTypesList() { |
| 211 | + // tags in [1, "priority", true] |
| 212 | + String vectorExpr = this.converter |
| 213 | + .convertExpression(new Expression(IN, new Key("tags"), new Value(List.of(1, "priority", true)))); |
| 214 | + assertThat(vectorExpr).isEqualTo("{\"metadata.tags\":{$in:[1,\"priority\",true]}}"); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void testEmptyList() { |
| 219 | + // categories in [] |
| 220 | + String vectorExpr = this.converter |
| 221 | + .convertExpression(new Expression(IN, new Key("categories"), new Value(List.of()))); |
| 222 | + assertThat(vectorExpr).isEqualTo("{\"metadata.categories\":{$in:[]}}"); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void testSingleItemList() { |
| 227 | + // status in ["active"] |
| 228 | + String vectorExpr = this.converter |
| 229 | + .convertExpression(new Expression(IN, new Key("status"), new Value(List.of("active")))); |
| 230 | + assertThat(vectorExpr).isEqualTo("{\"metadata.status\":{$in:[\"active\"]}}"); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void testKeyWithDots() { |
| 235 | + // "value.field" >= 18 |
| 236 | + String vectorExpr = this.converter |
| 237 | + .convertExpression(new Expression(GTE, new Key("value.field"), new Value(18))); |
| 238 | + assertThat(vectorExpr).isEqualTo("{\"metadata.value.field\":{$gte:18}}"); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void testKeyWithSpecialCharacters() { |
| 243 | + // "field-name_with@symbols" == "value" |
| 244 | + String vectorExpr = this.converter |
| 245 | + .convertExpression(new Expression(EQ, new Key("field-name_with@symbols"), new Value("value"))); |
| 246 | + assertThat(vectorExpr).isEqualTo("{\"metadata.field-name_with@symbols\":{$eq:\"value\"}}"); |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + public void testTripleAnd() { |
| 251 | + // value >= 100 AND type == "primary" AND region == "X" |
| 252 | + String vectorExpr = this.converter.convertExpression(new Expression(AND, |
| 253 | + new Expression(AND, new Expression(GTE, new Key("value"), new Value(100)), |
| 254 | + new Expression(EQ, new Key("type"), new Value("primary"))), |
| 255 | + new Expression(EQ, new Key("region"), new Value("X")))); |
| 256 | + |
| 257 | + assertThat(vectorExpr).isEqualTo( |
| 258 | + "{$and:[{$and:[{\"metadata.value\":{$gte:100}},{\"metadata.type\":{$eq:\"primary\"}}]},{\"metadata.region\":{$eq:\"X\"}}]}"); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void testTripleOr() { |
| 263 | + // value < 50 OR value > 200 OR type == "special" |
| 264 | + String vectorExpr = this.converter.convertExpression(new Expression(OR, |
| 265 | + new Expression(OR, new Expression(LT, new Key("value"), new Value(50)), |
| 266 | + new Expression(GT, new Key("value"), new Value(200))), |
| 267 | + new Expression(EQ, new Key("type"), new Value("special")))); |
| 268 | + |
| 269 | + assertThat(vectorExpr).isEqualTo( |
| 270 | + "{$or:[{$or:[{\"metadata.value\":{$lt:50}},{\"metadata.value\":{$gt:200}}]},{\"metadata.type\":{$eq:\"special\"}}]}"); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + public void testZeroValues() { |
| 275 | + // count == 0 |
| 276 | + String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("count"), new Value(0))); |
| 277 | + assertThat(vectorExpr).isEqualTo("{\"metadata.count\":{$eq:0}}"); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void testBooleanFalse() { |
| 282 | + // enabled == false |
| 283 | + String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("enabled"), new Value(false))); |
| 284 | + assertThat(vectorExpr).isEqualTo("{\"metadata.enabled\":{$eq:false}}"); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + public void testVeryLongString() { |
| 289 | + // Test with a very long string value |
| 290 | + String longValue = "This is a very long string that might be used as a value in a filter expression to test how the converter handles lengthy text content that could potentially cause issues with string manipulation or JSON formatting"; |
| 291 | + String vectorExpr = this.converter |
| 292 | + .convertExpression(new Expression(EQ, new Key("content"), new Value(longValue))); |
| 293 | + assertThat(vectorExpr).isEqualTo("{\"metadata.content\":{$eq:\"" + longValue + "\"}}"); |
| 294 | + } |
| 295 | + |
126 | 296 | }
|
0 commit comments