|
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,140 @@ public void testComplexIdentifiers() {
|
123 | 125 | assertThat(vectorExpr).isEqualTo("{\"country 1 2 3\": {\"$eq\": \"BG\"}}");
|
124 | 126 | }
|
125 | 127 |
|
| 128 | + @Test |
| 129 | + public void testNumericValues() { |
| 130 | + // score > 85 |
| 131 | + String vectorExpr = this.converter.convertExpression(new Expression(GT, new Key("score"), new Value(85))); |
| 132 | + assertThat(vectorExpr).isEqualTo("{\"score\": {\"$gt\": 85}}"); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testLessThan() { |
| 137 | + // priority < 10 |
| 138 | + String vectorExpr = this.converter.convertExpression(new Expression(LT, new Key("priority"), new Value(10))); |
| 139 | + assertThat(vectorExpr).isEqualTo("{\"priority\": {\"$lt\": 10}}"); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testNotInWithNumbers() { |
| 144 | + // status NIN [100, 200, 404] |
| 145 | + String vectorExpr = this.converter |
| 146 | + .convertExpression(new Expression(NIN, new Key("status"), new Value(List.of(100, 200, 404)))); |
| 147 | + assertThat(vectorExpr).isEqualTo("{\"status\": {\"$nin\": [100,200,404]}}"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testComplexAndOrCombination() { |
| 152 | + // (category == "A" OR category == "B") AND (value >= 50 AND value <= 100) |
| 153 | + String vectorExpr = this.converter.convertExpression(new Expression(AND, |
| 154 | + new Group(new Expression(OR, new Expression(EQ, new Key("category"), new Value("A")), |
| 155 | + new Expression(EQ, new Key("category"), new Value("B")))), |
| 156 | + new Group(new Expression(AND, new Expression(GTE, new Key("value"), new Value(50)), |
| 157 | + new Expression(LTE, new Key("value"), new Value(100)))))); |
| 158 | + |
| 159 | + assertThat(vectorExpr).isEqualTo( |
| 160 | + "{\"$and\": [{\"$or\": [{\"category\": {\"$eq\": \"A\"}},{\"category\": {\"$eq\": \"B\"}}]},{\"$and\": [{\"value\": {\"$gte\": 50}},{\"value\": {\"$lte\": 100}}]}]}"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testNestedGroups() { |
| 165 | + // ((type == "premium" AND level > 5) OR (type == "basic" AND level > 10)) AND |
| 166 | + // active == true |
| 167 | + String vectorExpr = this.converter.convertExpression(new Expression(AND, |
| 168 | + new Group(new Expression(OR, |
| 169 | + new Group(new Expression(AND, new Expression(EQ, new Key("type"), new Value("premium")), |
| 170 | + new Expression(GT, new Key("level"), new Value(5)))), |
| 171 | + new Group(new Expression(AND, new Expression(EQ, new Key("type"), new Value("basic")), |
| 172 | + new Expression(GT, new Key("level"), new Value(10)))))), |
| 173 | + new Expression(EQ, new Key("active"), new Value(true)))); |
| 174 | + |
| 175 | + assertThat(vectorExpr).isEqualTo( |
| 176 | + "{\"$and\": [{\"$or\": [{\"$and\": [{\"type\": {\"$eq\": \"premium\"}},{\"level\": {\"$gt\": 5}}]},{\"$and\": [{\"type\": {\"$eq\": \"basic\"}},{\"level\": {\"$gt\": 10}}]}]},{\"active\": {\"$eq\": true}}]}"); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testMixedDataTypes() { |
| 181 | + // name == "test" AND count >= 5 AND enabled == true AND ratio <= 0.95 |
| 182 | + String vectorExpr = this.converter.convertExpression(new Expression(AND, |
| 183 | + new Expression(AND, |
| 184 | + new Expression(AND, new Expression(EQ, new Key("name"), new Value("test")), |
| 185 | + new Expression(GTE, new Key("count"), new Value(5))), |
| 186 | + new Expression(EQ, new Key("enabled"), new Value(true))), |
| 187 | + new Expression(LTE, new Key("ratio"), new Value(0.95)))); |
| 188 | + |
| 189 | + assertThat(vectorExpr).isEqualTo( |
| 190 | + "{\"$and\": [{\"$and\": [{\"$and\": [{\"name\": {\"$eq\": \"test\"}},{\"count\": {\"$gte\": 5}}]},{\"enabled\": {\"$eq\": true}}]},{\"ratio\": {\"$lte\": 0.95}}]}"); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testInWithMixedTypes() { |
| 195 | + // tag IN ["A", "B", "C"] |
| 196 | + String vectorExpr = this.converter |
| 197 | + .convertExpression(new Expression(IN, new Key("tag"), new Value(List.of("A", "B", "C")))); |
| 198 | + assertThat(vectorExpr).isEqualTo("{\"tag\": {\"$in\": [\"A\",\"B\",\"C\"]}}"); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testNegativeNumbers() { |
| 203 | + // balance >= -100.0 AND balance <= -10.0 |
| 204 | + String vectorExpr = this.converter |
| 205 | + .convertExpression(new Expression(AND, new Expression(GTE, new Key("balance"), new Value(-100.0)), |
| 206 | + new Expression(LTE, new Key("balance"), new Value(-10.0)))); |
| 207 | + |
| 208 | + assertThat(vectorExpr) |
| 209 | + .isEqualTo("{\"$and\": [{\"balance\": {\"$gte\": -100.0}},{\"balance\": {\"$lte\": -10.0}}]}"); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testSpecialCharactersInValues() { |
| 214 | + // description == "Item with spaces & symbols!" |
| 215 | + String vectorExpr = this.converter |
| 216 | + .convertExpression(new Expression(EQ, new Key("description"), new Value("Item with spaces & symbols!"))); |
| 217 | + assertThat(vectorExpr).isEqualTo("{\"description\": {\"$eq\": \"Item with spaces & symbols!\"}}"); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testMultipleOrConditions() { |
| 222 | + // status == "pending" OR status == "processing" OR status == "completed" |
| 223 | + String vectorExpr = this.converter.convertExpression(new Expression(OR, |
| 224 | + new Expression(OR, new Expression(EQ, new Key("status"), new Value("pending")), |
| 225 | + new Expression(EQ, new Key("status"), new Value("processing"))), |
| 226 | + new Expression(EQ, new Key("status"), new Value("completed")))); |
| 227 | + |
| 228 | + assertThat(vectorExpr).isEqualTo( |
| 229 | + "{\"$or\": [{\"$or\": [{\"status\": {\"$eq\": \"pending\"}},{\"status\": {\"$eq\": \"processing\"}}]},{\"status\": {\"$eq\": \"completed\"}}]}"); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testSingleElementList() { |
| 234 | + // category IN ["single"] |
| 235 | + String vectorExpr = this.converter |
| 236 | + .convertExpression(new Expression(IN, new Key("category"), new Value(List.of("single")))); |
| 237 | + assertThat(vectorExpr).isEqualTo("{\"category\": {\"$in\": [\"single\"]}}"); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void testZeroValues() { |
| 242 | + // quantity == 0 AND price > 0 |
| 243 | + String vectorExpr = this.converter |
| 244 | + .convertExpression(new Expression(AND, new Expression(EQ, new Key("quantity"), new Value(0)), |
| 245 | + new Expression(GT, new Key("price"), new Value(0)))); |
| 246 | + |
| 247 | + assertThat(vectorExpr).isEqualTo("{\"$and\": [{\"quantity\": {\"$eq\": 0}},{\"price\": {\"$gt\": 0}}]}"); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void testComplexNestedExpression() { |
| 252 | + // (priority >= 1 AND priority <= 5) OR (urgent == true AND category NIN ["low", |
| 253 | + // "medium"]) |
| 254 | + String vectorExpr = this.converter.convertExpression(new Expression(OR, |
| 255 | + new Group(new Expression(AND, new Expression(GTE, new Key("priority"), new Value(1)), |
| 256 | + new Expression(LTE, new Key("priority"), new Value(5)))), |
| 257 | + new Group(new Expression(AND, new Expression(EQ, new Key("urgent"), new Value(true)), |
| 258 | + new Expression(NIN, new Key("category"), new Value(List.of("low", "medium"))))))); |
| 259 | + |
| 260 | + assertThat(vectorExpr).isEqualTo( |
| 261 | + "{\"$or\": [{\"$and\": [{\"priority\": {\"$gte\": 1}},{\"priority\": {\"$lte\": 5}}]},{\"$and\": [{\"urgent\": {\"$eq\": true}},{\"category\": {\"$nin\": [\"low\",\"medium\"]}}]}]}"); |
| 262 | + } |
| 263 | + |
126 | 264 | }
|
0 commit comments