|
| 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 | + |
| 17 | +package org.springframework.ai.vectorstore; |
| 18 | + |
| 19 | +import java.util.Date; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.ai.vectorstore.filter.Filter; |
| 25 | +import org.springframework.ai.vectorstore.filter.FilterExpressionConverter; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.AND; |
| 29 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.EQ; |
| 30 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GT; |
| 31 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.GTE; |
| 32 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.IN; |
| 33 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.LTE; |
| 34 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NE; |
| 35 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.NIN; |
| 36 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.OR; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Jason Huynh |
| 40 | + */ |
| 41 | +class GemFireAiSearchFilterExpressionConverterTest { |
| 42 | + |
| 43 | + final FilterExpressionConverter converter = new GemFireAiSearchFilterExpressionConverter(); |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testDate() { |
| 47 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(EQ, new Filter.Key("activationDate"), |
| 48 | + new Filter.Value(new Date(1704637752148L)))); |
| 49 | + assertThat(vectorExpr).isEqualTo("activationDate:2024-01-07T14:29:12Z"); |
| 50 | + |
| 51 | + vectorExpr = this.converter.convertExpression( |
| 52 | + new Filter.Expression(EQ, new Filter.Key("activationDate"), new Filter.Value("1970-01-01T00:00:02Z"))); |
| 53 | + assertThat(vectorExpr).isEqualTo("activationDate:1970-01-01T00:00:02Z"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testEQ() { |
| 58 | + String vectorExpr = this.converter |
| 59 | + .convertExpression(new Filter.Expression(EQ, new Filter.Key("country"), new Filter.Value("BG"))); |
| 60 | + assertThat(vectorExpr).isEqualTo("country:BG"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testEqAndGte() { |
| 65 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(AND, |
| 66 | + new Filter.Expression(EQ, new Filter.Key("genre"), new Filter.Value("drama")), |
| 67 | + new Filter.Expression(GTE, new Filter.Key("year"), new Filter.Value(2020)))); |
| 68 | + assertThat(vectorExpr).isEqualTo("genre:drama AND year:[2020 TO *]"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testEqAndGe() { |
| 73 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(AND, |
| 74 | + new Filter.Expression(EQ, new Filter.Key("genre"), new Filter.Value("drama")), |
| 75 | + new Filter.Expression(GT, new Filter.Key("year"), new Filter.Value(2020)))); |
| 76 | + assertThat(vectorExpr).isEqualTo("genre:drama AND year:{2020 TO *]"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testIn() { |
| 81 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(IN, new Filter.Key("genre"), |
| 82 | + new Filter.Value(List.of("comedy", "documentary", "drama")))); |
| 83 | + assertThat(vectorExpr).isEqualTo("genre:(comedy OR documentary OR drama)"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testNe() { |
| 88 | + String vectorExpr = this.converter.convertExpression( |
| 89 | + new Filter.Expression(OR, new Filter.Expression(GTE, new Filter.Key("year"), new Filter.Value(2020)), |
| 90 | + new Filter.Expression(AND, |
| 91 | + new Filter.Expression(EQ, new Filter.Key("country"), new Filter.Value("BG")), |
| 92 | + new Filter.Expression(NE, new Filter.Key("city"), new Filter.Value("Sofia"))))); |
| 93 | + assertThat(vectorExpr).isEqualTo("year:[2020 TO *] OR country:BG AND city: NOT Sofia"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testGroup() { |
| 98 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(AND, |
| 99 | + new Filter.Group(new Filter.Expression(OR, |
| 100 | + new Filter.Expression(GTE, new Filter.Key("year"), new Filter.Value(2020)), |
| 101 | + new Filter.Expression(EQ, new Filter.Key("country"), new Filter.Value("BG")))), |
| 102 | + new Filter.Expression(NIN, new Filter.Key("city"), new Filter.Value(List.of("Sofia", "Plovdiv"))))); |
| 103 | + assertThat(vectorExpr).isEqualTo("(year:[2020 TO *] OR country:BG) AND NOT city:(Sofia OR Plovdiv)"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testBoolean() { |
| 108 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(AND, |
| 109 | + new Filter.Expression(AND, new Filter.Expression(EQ, new Filter.Key("isOpen"), new Filter.Value(true)), |
| 110 | + new Filter.Expression(GTE, new Filter.Key("year"), new Filter.Value(2020))), |
| 111 | + new Filter.Expression(IN, new Filter.Key("country"), new Filter.Value(List.of("BG", "NL", "US"))))); |
| 112 | + |
| 113 | + assertThat(vectorExpr).isEqualTo("isOpen:true AND year:[2020 TO *] AND country:(BG OR NL OR US)"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testDecimal() { |
| 118 | + String vectorExpr = this.converter.convertExpression(new Filter.Expression(AND, |
| 119 | + new Filter.Expression(GTE, new Filter.Key("temperature"), new Filter.Value(-15.6)), |
| 120 | + new Filter.Expression(LTE, new Filter.Key("temperature"), new Filter.Value(20.13)))); |
| 121 | + |
| 122 | + assertThat(vectorExpr).isEqualTo("temperature:[-15.6 TO *] AND temperature:[* TO 20.13]"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testComplexIdentifiers() { |
| 127 | + String vectorExpr = this.converter |
| 128 | + .convertExpression(new Filter.Expression(EQ, new Filter.Key("\"country 1 2 3\""), new Filter.Value("BG"))); |
| 129 | + assertThat(vectorExpr).isEqualTo("country 1 2 3:BG"); |
| 130 | + |
| 131 | + vectorExpr = this.converter |
| 132 | + .convertExpression(new Filter.Expression(EQ, new Filter.Key("'country 1 2 3'"), new Filter.Value("BG"))); |
| 133 | + assertThat(vectorExpr).isEqualTo("country 1 2 3:BG"); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments