|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.search; |
| 18 | + |
| 19 | +import static org.apache.solr.common.SolrException.ErrorCode; |
| 20 | +import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Locale; |
| 24 | +import org.apache.lucene.index.VectorEncoding; |
| 25 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 26 | +import org.apache.lucene.queries.function.ValueSource; |
| 27 | +import org.apache.lucene.queries.function.valuesource.ByteVectorSimilarityFunction; |
| 28 | +import org.apache.lucene.queries.function.valuesource.FloatVectorSimilarityFunction; |
| 29 | +import org.apache.solr.common.SolrException; |
| 30 | +import org.apache.solr.schema.DenseVectorField; |
| 31 | +import org.apache.solr.schema.FieldType; |
| 32 | +import org.apache.solr.schema.SchemaField; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class provides implementation for two variants for parsing function query vectorSimilarity |
| 36 | + * which is used to calculate the similarity between two vectors. |
| 37 | + */ |
| 38 | +public class VectorSimilaritySourceParser extends ValueSourceParser { |
| 39 | + @Override |
| 40 | + public ValueSource parse(FunctionQParser fp) throws SyntaxError { |
| 41 | + |
| 42 | + final String arg1Str = fp.parseArg(); |
| 43 | + if (arg1Str == null || !fp.hasMoreArguments()) |
| 44 | + throw new SolrException( |
| 45 | + BAD_REQUEST, "Invalid number of arguments. Please provide either two or four arguments."); |
| 46 | + |
| 47 | + final String arg2Str = peekIsConstVector(fp) ? null : fp.parseArg(); |
| 48 | + if (fp.hasMoreArguments() && arg2Str != null) { |
| 49 | + return handle4ArgsVariant(fp, arg1Str, arg2Str); |
| 50 | + } |
| 51 | + return handle2ArgsVariant(fp, arg1Str, arg2Str); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * returns true if and only if the next argument is a constant vector, taking into consideration |
| 56 | + * that the next (literal) argument may be a param reference |
| 57 | + */ |
| 58 | + private boolean peekIsConstVector(final FunctionQParser fp) throws SyntaxError { |
| 59 | + final char rawPeek = fp.sp.peek(); |
| 60 | + if ('[' == rawPeek) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + if ('$' == rawPeek) { |
| 64 | + final int savedPos = fp.sp.pos; |
| 65 | + try { |
| 66 | + final String rawParam = fp.parseArg(); |
| 67 | + return ((null != rawParam) && ('[' == (new StrParser(rawParam)).peek())); |
| 68 | + } finally { |
| 69 | + fp.sp.pos = savedPos; |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private static int buildVectorEncodingFlag(final VectorEncoding vectorEncoding) { |
| 76 | + return FunctionQParser.FLAG_DEFAULT |
| 77 | + | FunctionQParser.FLAG_CONSUME_DELIMITER |
| 78 | + | (vectorEncoding.equals(VectorEncoding.BYTE) |
| 79 | + ? FunctionQParser.FLAG_PARSE_VECTOR_BYTE_ENCODING |
| 80 | + : 0); |
| 81 | + } |
| 82 | + |
| 83 | + /** Expects to find args #3 and #4 (two vector ValueSources) still in the function parser */ |
| 84 | + private ValueSource handle4ArgsVariant(FunctionQParser fp, String vecEncStr, String vecSimFuncStr) |
| 85 | + throws SyntaxError { |
| 86 | + final var vectorEncoding = enumValueOrBadRequest(VectorEncoding.class, vecEncStr); |
| 87 | + final var vectorSimilarityFunction = |
| 88 | + enumValueOrBadRequest(VectorSimilarityFunction.class, vecSimFuncStr); |
| 89 | + final int vectorEncodingFlag = buildVectorEncodingFlag(vectorEncoding); |
| 90 | + final ValueSource v1 = fp.parseValueSource(vectorEncodingFlag); |
| 91 | + final ValueSource v2 = fp.parseValueSource(vectorEncodingFlag); |
| 92 | + return createSimilarityFunction(vectorSimilarityFunction, vectorEncoding, v1, v2); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * If <code>field2Name</code> is null, then expects to find a constant vector as the only |
| 97 | + * remaining arg in the function parser. |
| 98 | + */ |
| 99 | + private ValueSource handle2ArgsVariant(FunctionQParser fp, String field1Name, String field2Name) |
| 100 | + throws SyntaxError { |
| 101 | + |
| 102 | + final SchemaField field1 = fp.req.getSchema().getField(field1Name); |
| 103 | + final DenseVectorField field1Type = requireVectorType(field1); |
| 104 | + |
| 105 | + final var vectorEncoding = field1Type.getVectorEncoding(); |
| 106 | + final var vectorSimilarityFunction = field1Type.getSimilarityFunction(); |
| 107 | + |
| 108 | + final ValueSource v1 = field1Type.getValueSource(field1, fp); |
| 109 | + final ValueSource v2; |
| 110 | + |
| 111 | + if (null == field2Name) { |
| 112 | + final int vectorEncodingFlag = buildVectorEncodingFlag(vectorEncoding); |
| 113 | + v2 = fp.parseValueSource(vectorEncodingFlag); |
| 114 | + |
| 115 | + } else { |
| 116 | + final SchemaField field2 = fp.req.getSchema().getField(field2Name); |
| 117 | + final DenseVectorField field2Type = requireVectorType(field2); |
| 118 | + if (vectorEncoding != field2Type.getVectorEncoding() |
| 119 | + || vectorSimilarityFunction != field2Type.getSimilarityFunction()) { |
| 120 | + throw new SolrException( |
| 121 | + BAD_REQUEST, |
| 122 | + String.format( |
| 123 | + Locale.ROOT, |
| 124 | + "Invalid arguments: vector field %s and vector field %s must have the same vectorEncoding and similarityFunction", |
| 125 | + field1.getName(), |
| 126 | + field2.getName())); |
| 127 | + } |
| 128 | + v2 = field2Type.getValueSource(field2, fp); |
| 129 | + } |
| 130 | + return createSimilarityFunction(vectorSimilarityFunction, vectorEncoding, v1, v2); |
| 131 | + } |
| 132 | + |
| 133 | + private ValueSource createSimilarityFunction( |
| 134 | + VectorSimilarityFunction functionName, |
| 135 | + VectorEncoding vectorEncoding, |
| 136 | + ValueSource v1, |
| 137 | + ValueSource v2) |
| 138 | + throws SyntaxError { |
| 139 | + switch (vectorEncoding) { |
| 140 | + case FLOAT32: |
| 141 | + return new FloatVectorSimilarityFunction(functionName, v1, v2); |
| 142 | + case BYTE: |
| 143 | + return new ByteVectorSimilarityFunction(functionName, v1, v2); |
| 144 | + default: |
| 145 | + throw new SyntaxError("Invalid vector encoding: " + vectorEncoding); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private DenseVectorField requireVectorType(final SchemaField field) throws SyntaxError { |
| 150 | + final FieldType fieldType = field.getType(); |
| 151 | + if (fieldType instanceof DenseVectorField) { |
| 152 | + return (DenseVectorField) field.getType(); |
| 153 | + } |
| 154 | + throw new SolrException( |
| 155 | + BAD_REQUEST, |
| 156 | + String.format( |
| 157 | + Locale.ROOT, |
| 158 | + "Type mismatch: Expected [%s], but found a different field type for field: [%s]", |
| 159 | + DenseVectorField.class.getSimpleName(), |
| 160 | + field.getName())); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Helper method that returns the correct Enum instance for the <code>arg</code> String, or throws |
| 165 | + * a {@link ErrorCode#BAD_REQUEST} with specifics on the "Invalid argument" |
| 166 | + */ |
| 167 | + private static <T extends Enum<T>> T enumValueOrBadRequest( |
| 168 | + final Class<T> enumClass, final String arg) throws SolrException { |
| 169 | + assert null != enumClass; |
| 170 | + try { |
| 171 | + return Enum.valueOf(enumClass, arg); |
| 172 | + } catch (IllegalArgumentException | NullPointerException e) { |
| 173 | + throw new SolrException( |
| 174 | + BAD_REQUEST, |
| 175 | + String.format( |
| 176 | + Locale.ROOT, |
| 177 | + "Invalid argument: %s is not a valid %s. Expected one of %s", |
| 178 | + arg, |
| 179 | + enumClass.getSimpleName(), |
| 180 | + Arrays.toString(enumClass.getEnumConstants()))); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments