|
| 1 | +/* |
| 2 | + * Copyright 2023 - 2024 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 | +package org.springframework.ai.vectorstore; |
| 17 | + |
| 18 | +import org.springframework.ai.vectorstore.filter.Filter; |
| 19 | +import org.springframework.ai.vectorstore.filter.converter.AbstractFilterExpressionConverter; |
| 20 | + |
| 21 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.AND; |
| 22 | +import static org.springframework.ai.vectorstore.filter.Filter.ExpressionType.OR; |
| 23 | + |
| 24 | +/** |
| 25 | + * Converts {@link Filter.Expression} into MongDB Atlas metadata filter expression format. |
| 26 | + * (https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#std-label-vectorSearch-agg-pipeline-filter) |
| 27 | + * |
| 28 | + * @author Chris Smith |
| 29 | + * @since 1.0.0 |
| 30 | + */ |
| 31 | +public class MongoDBAtlasFilterExpressionConverter extends AbstractFilterExpressionConverter { |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void doExpression(Filter.Expression expression, StringBuilder context) { |
| 35 | + // Handling AND/OR |
| 36 | + if (AND.equals(expression.type()) || OR.equals(expression.type())) { |
| 37 | + doCompoundExpressionType(expression, context); |
| 38 | + } |
| 39 | + else { |
| 40 | + doSingleExpressionType(expression, context); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private void doCompoundExpressionType(Filter.Expression expression, StringBuilder context) { |
| 45 | + context.append("{"); |
| 46 | + context.append(getOperationSymbol(expression)); |
| 47 | + context.append(":["); |
| 48 | + this.convertOperand(expression.left(), context); |
| 49 | + context.append(","); |
| 50 | + this.convertOperand(expression.right(), context); |
| 51 | + context.append("]}"); |
| 52 | + } |
| 53 | + |
| 54 | + private void doSingleExpressionType(Filter.Expression expression, StringBuilder context) { |
| 55 | + context.append("{"); |
| 56 | + this.convertOperand(expression.left(), context); |
| 57 | + context.append(":{"); |
| 58 | + context.append(getOperationSymbol(expression)); |
| 59 | + context.append(":"); |
| 60 | + this.convertOperand(expression.right(), context); |
| 61 | + context.append("}}"); |
| 62 | + } |
| 63 | + |
| 64 | + private String getOperationSymbol(Filter.Expression exp) { |
| 65 | + switch (exp.type()) { |
| 66 | + case AND: |
| 67 | + return "$and"; |
| 68 | + case OR: |
| 69 | + return "$or"; |
| 70 | + case EQ: |
| 71 | + return "$eq"; |
| 72 | + case NE: |
| 73 | + return "$ne"; |
| 74 | + case LT: |
| 75 | + return "$lt"; |
| 76 | + case LTE: |
| 77 | + return "$lte"; |
| 78 | + case GT: |
| 79 | + return "$gt"; |
| 80 | + case GTE: |
| 81 | + return "$gte"; |
| 82 | + case IN: |
| 83 | + return "$in"; |
| 84 | + case NIN: |
| 85 | + return "$nin"; |
| 86 | + default: |
| 87 | + throw new RuntimeException("Not supported expression type:" + exp.type()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void doKey(Filter.Key filterKey, StringBuilder context) { |
| 93 | + var identifier = (hasOuterQuotes(filterKey.key())) ? removeOuterQuotes(filterKey.key()) : filterKey.key(); |
| 94 | + context.append("\"metadata." + identifier + "\""); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments