Skip to content

Commit 5235696

Browse files
leevh0908markpollack
authored andcommitted
Change NIN filter to use "not in" operator in Milvus
- Milvus requires "not in" operator syntax instead of "nin" for filtering. - See https://milvus.io/docs/boolean.md - Updated test and added additional test coverage
1 parent cd88643 commit 5235696

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

vector-stores/spring-ai-milvus-store/src/main/java/org/springframework/ai/vectorstore/MilvusFilterExpressionConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private String getOperationSymbol(Expression exp) {
5858
case IN:
5959
return " in ";
6060
case NIN:
61-
return " nin ";
61+
return " not in ";
6262
default:
6363
throw new RuntimeException("Not supported expression type:" + exp.type());
6464
}

vector-stores/spring-ai-milvus-store/src/test/java/org/springframework/ai/vectorstore/MilvusFilterExpressionConverterTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.AND;
3131
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.EQ;
32+
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GT;
3233
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GTE;
3334
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.IN;
35+
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LT;
3436
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LTE;
3537
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NE;
3638
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NIN;
@@ -86,11 +88,11 @@ public void testGroup() {
8688
new Expression(EQ, new Key("country"), new Value("BG")))),
8789
new Expression(NIN, new Key("city"), new Value(List.of("Sofia", "Plovdiv")))));
8890
assertThat(vectorExpr).isEqualTo(
89-
"metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"city\"] nin [\"Sofia\",\"Plovdiv\"]");
91+
"metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"city\"] not in [\"Sofia\",\"Plovdiv\"]");
9092
}
9193

9294
@Test
93-
public void tesBoolean() {
95+
public void testBoolean() {
9496
// isOpen == true AND year >= 2020 AND country IN ["BG", "NL", "US"]
9597
String vectorExpr = this.converter.convertExpression(new Expression(AND,
9698
new Expression(AND, new Expression(EQ, new Key("isOpen"), new Value(true)),
@@ -121,4 +123,36 @@ public void testComplexIdentifiers() {
121123
assertThat(vectorExpr).isEqualTo("metadata[\"country 1 2 3\"] == \"BG\"");
122124
}
123125

126+
@Test
127+
public void testLt() {
128+
// temperature < 0
129+
String vectorExpr = this.converter.convertExpression(new Expression(LT, new Key("temperature"), new Value(0)));
130+
assertThat(vectorExpr).isEqualTo("metadata[\"temperature\"] < 0");
131+
}
132+
133+
@Test
134+
public void testLte() {
135+
// humidity <= 100
136+
String vectorExpr = this.converter.convertExpression(new Expression(LTE, new Key("humidity"), new Value(100)));
137+
assertThat(vectorExpr).isEqualTo("metadata[\"humidity\"] <= 100");
138+
}
139+
140+
@Test
141+
public void testGt() {
142+
// price > 1000
143+
String vectorExpr = this.converter.convertExpression(new Expression(GT, new Key("price"), new Value(1000)));
144+
assertThat(vectorExpr).isEqualTo("metadata[\"price\"] > 1000");
145+
}
146+
147+
@Test
148+
public void testCombinedComparisons() {
149+
// price > 1000 && temperature < 25 && humidity <= 80
150+
String vectorExpr = this.converter.convertExpression(new Expression(AND,
151+
new Expression(AND, new Expression(GT, new Key("price"), new Value(1000)),
152+
new Expression(LT, new Key("temperature"), new Value(25))),
153+
new Expression(LTE, new Key("humidity"), new Value(80))));
154+
assertThat(vectorExpr)
155+
.isEqualTo("metadata[\"price\"] > 1000 && metadata[\"temperature\"] < 25 && metadata[\"humidity\"] <= 80");
156+
}
157+
124158
}

0 commit comments

Comments
 (0)