|
| 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.SERVER_ERROR; |
| 20 | + |
| 21 | +import java.text.ParseException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | +import org.apache.lucene.expressions.Bindings; |
| 29 | +import org.apache.lucene.expressions.Expression; |
| 30 | +import org.apache.lucene.expressions.js.JavascriptCompiler; |
| 31 | +import org.apache.lucene.queries.function.ValueSource; |
| 32 | +import org.apache.lucene.search.DoubleValuesSource; |
| 33 | +import org.apache.solr.common.SolrException; |
| 34 | +import org.apache.solr.common.util.NamedList; |
| 35 | +import org.apache.solr.schema.IndexSchema; |
| 36 | +import org.apache.solr.schema.SchemaField; |
| 37 | + |
| 38 | +/** |
| 39 | + * A ValueSource parser configured with a pre-compiled expression that can then be evaluated at |
| 40 | + * request time. It's powered by the Lucene Expressions module, which is a subset of JavaScript. |
| 41 | + */ |
| 42 | +public class ExpressionValueSourceParser extends ValueSourceParser { |
| 43 | + |
| 44 | + public static final String SCORE_KEY = "score-name"; // TODO get rid of this? Why have it? |
| 45 | + public static final String EXPRESSION_KEY = "expression"; |
| 46 | + |
| 47 | + private Expression expression; |
| 48 | + private String scoreKey; |
| 49 | + private int numPositionalArgs = 0; // Number of positional arguments in the expression |
| 50 | + |
| 51 | + @Override |
| 52 | + public void init(NamedList<?> args) { |
| 53 | + initConfiguredExpression(args); |
| 54 | + initScoreKey(args); |
| 55 | + super.init(args); |
| 56 | + } |
| 57 | + |
| 58 | + /** Checks for optional scoreKey override */ |
| 59 | + private void initScoreKey(NamedList<?> args) { |
| 60 | + scoreKey = Optional.ofNullable((String) args.remove(SCORE_KEY)).orElse(SolrReturnFields.SCORE); |
| 61 | + } |
| 62 | + |
| 63 | + /** Parses the pre-configured expression */ |
| 64 | + private void initConfiguredExpression(NamedList<?> args) { |
| 65 | + String expressionStr = |
| 66 | + Optional.ofNullable((String) args.remove(EXPRESSION_KEY)) |
| 67 | + .orElseThrow( |
| 68 | + () -> |
| 69 | + new SolrException( |
| 70 | + SERVER_ERROR, EXPRESSION_KEY + " must be configured with an expression")); |
| 71 | + |
| 72 | + // Find the highest positional argument in the expression |
| 73 | + Pattern pattern = Pattern.compile("\\$(\\d+)"); |
| 74 | + Matcher matcher = pattern.matcher(expressionStr); |
| 75 | + while (matcher.find()) { |
| 76 | + int argNum = Integer.parseInt(matcher.group(1)); |
| 77 | + numPositionalArgs = Math.max(numPositionalArgs, argNum); |
| 78 | + } |
| 79 | + |
| 80 | + // TODO add way to register additional functions |
| 81 | + try { |
| 82 | + this.expression = JavascriptCompiler.compile(expressionStr); |
| 83 | + } catch (ParseException e) { |
| 84 | + throw new SolrException( |
| 85 | + SERVER_ERROR, "Unable to parse javascript expression: " + expressionStr, e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // TODO: support dynamic expressions: expr("foo * bar / 32") ?? |
| 90 | + |
| 91 | + @Override |
| 92 | + public ValueSource parse(FunctionQParser fp) throws SyntaxError { |
| 93 | + assert null != fp; |
| 94 | + |
| 95 | + // Parse positional arguments if any |
| 96 | + List<DoubleValuesSource> positionalArgs = new ArrayList<>(); |
| 97 | + for (int i = 0; i < numPositionalArgs; i++) { |
| 98 | + ValueSource vs = fp.parseValueSource(); |
| 99 | + positionalArgs.add(vs.asDoubleValuesSource()); |
| 100 | + } |
| 101 | + |
| 102 | + IndexSchema schema = fp.getReq().getSchema(); |
| 103 | + SolrBindings b = new SolrBindings(scoreKey, schema, positionalArgs); |
| 104 | + return ValueSource.fromDoubleValuesSource(expression.getDoubleValuesSource(b)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A bindings class that uses schema fields to resolve variables. |
| 109 | + * |
| 110 | + * @lucene.internal |
| 111 | + */ |
| 112 | + public static class SolrBindings extends Bindings { |
| 113 | + private final String scoreKey; |
| 114 | + private final IndexSchema schema; |
| 115 | + private final List<DoubleValuesSource> positionalArgs; |
| 116 | + |
| 117 | + /** |
| 118 | + * @param scoreKey The binding name that should be used to represent the score, may be null |
| 119 | + * @param schema IndexSchema for field bindings |
| 120 | + * @param positionalArgs List of positional arguments |
| 121 | + */ |
| 122 | + public SolrBindings( |
| 123 | + String scoreKey, IndexSchema schema, List<DoubleValuesSource> positionalArgs) { |
| 124 | + this.scoreKey = scoreKey; |
| 125 | + this.schema = schema; |
| 126 | + this.positionalArgs = positionalArgs != null ? positionalArgs : new ArrayList<>(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public DoubleValuesSource getDoubleValuesSource(String key) { |
| 131 | + assert null != key; |
| 132 | + |
| 133 | + if (Objects.equals(scoreKey, key)) { |
| 134 | + return DoubleValuesSource.SCORES; |
| 135 | + } |
| 136 | + |
| 137 | + // Check for positional arguments like $1, $2, etc. |
| 138 | + if (key.startsWith("$")) { |
| 139 | + try { |
| 140 | + int position = Integer.parseInt(key.substring(1)); |
| 141 | + return positionalArgs.get(position - 1); // Convert to 0-based index |
| 142 | + } catch (RuntimeException e) { |
| 143 | + throw new IllegalArgumentException("Not a valid positional argument: " + key, e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + SchemaField field = schema.getFieldOrNull(key); |
| 148 | + if (null != field) { |
| 149 | + return field.getType().getValueSource(field, null).asDoubleValuesSource(); |
| 150 | + } |
| 151 | + |
| 152 | + throw new IllegalArgumentException("No binding or schema field for key: " + key); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments