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
This commit is contained in:
leevh0908
2024-11-04 20:45:30 +08:00
committed by Mark Pollack
parent cd886437eb
commit 5235696160
2 changed files with 37 additions and 3 deletions

View File

@@ -58,7 +58,7 @@ public class MilvusFilterExpressionConverter extends AbstractFilterExpressionCon
case IN:
return " in ";
case NIN:
return " nin ";
return " not in ";
default:
throw new RuntimeException("Not supported expression type:" + exp.type());
}

View File

@@ -29,8 +29,10 @@ import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.AND;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.EQ;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GT;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GTE;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.IN;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LT;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LTE;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NE;
import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NIN;
@@ -86,11 +88,11 @@ public class MilvusFilterExpressionConverterTests {
new Expression(EQ, new Key("country"), new Value("BG")))),
new Expression(NIN, new Key("city"), new Value(List.of("Sofia", "Plovdiv")))));
assertThat(vectorExpr).isEqualTo(
"metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"city\"] nin [\"Sofia\",\"Plovdiv\"]");
"metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"year\"] >= 2020 || metadata[\"country\"] == \"BG\" && metadata[\"city\"] not in [\"Sofia\",\"Plovdiv\"]");
}
@Test
public void tesBoolean() {
public void testBoolean() {
// isOpen == true AND year >= 2020 AND country IN ["BG", "NL", "US"]
String vectorExpr = this.converter.convertExpression(new Expression(AND,
new Expression(AND, new Expression(EQ, new Key("isOpen"), new Value(true)),
@@ -121,4 +123,36 @@ public class MilvusFilterExpressionConverterTests {
assertThat(vectorExpr).isEqualTo("metadata[\"country 1 2 3\"] == \"BG\"");
}
@Test
public void testLt() {
// temperature < 0
String vectorExpr = this.converter.convertExpression(new Expression(LT, new Key("temperature"), new Value(0)));
assertThat(vectorExpr).isEqualTo("metadata[\"temperature\"] < 0");
}
@Test
public void testLte() {
// humidity <= 100
String vectorExpr = this.converter.convertExpression(new Expression(LTE, new Key("humidity"), new Value(100)));
assertThat(vectorExpr).isEqualTo("metadata[\"humidity\"] <= 100");
}
@Test
public void testGt() {
// price > 1000
String vectorExpr = this.converter.convertExpression(new Expression(GT, new Key("price"), new Value(1000)));
assertThat(vectorExpr).isEqualTo("metadata[\"price\"] > 1000");
}
@Test
public void testCombinedComparisons() {
// price > 1000 && temperature < 25 && humidity <= 80
String vectorExpr = this.converter.convertExpression(new Expression(AND,
new Expression(AND, new Expression(GT, new Key("price"), new Value(1000)),
new Expression(LT, new Key("temperature"), new Value(25))),
new Expression(LTE, new Key("humidity"), new Value(80))));
assertThat(vectorExpr)
.isEqualTo("metadata[\"price\"] > 1000 && metadata[\"temperature\"] < 25 && metadata[\"humidity\"] <= 80");
}
}