-
Notifications
You must be signed in to change notification settings - Fork 745
SOLR-5707: Lucene Expressions in Solr #1244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
443e84b
SOLR-5707: Lucene Expressions in Solr
risdenk ab38d9c
Merge branch 'main' into fork/risdenk/SOLR-5707
dsmiley 2dbfbde
ValueSourceAugmenter from SOLR-15030
dsmiley fecb3cd
Convert nocommit to TODO
dsmiley a8c1e44
Merge branch 'main' into fork/risdenk/SOLR-5707
dsmiley e05ebe7
VSA: fix prefetch to consider need for Scorable
dsmiley 00f8435
ignore another test that can't be supported yet
dsmiley 38d1eb5
optimization (avoid needless subIndex)
dsmiley 3270f77
Ref Guide page
dsmiley f47cfa3
Single expression, and support positional args
dsmiley 164603d
refer to it in function-queries.adoc
dsmiley c634796
CHANGES.txt
dsmiley d6a388e
un-ignore tests
dsmiley dc25024
Merge branch 'main' into fork/risdenk/SOLR-5707
dsmiley 8234a81
fix lucene "expressions" javadoc reference in ref guide
dsmiley 992ba1a
code review feedback
dsmiley b53e50f
typo/formatting
dsmiley 2c8967e
Merge branch 'main' into fork/risdenk/SOLR-5707
dsmiley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
solr/core/src/java/org/apache/solr/search/ExpressionValueSourceParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.search; | ||
|
||
import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR; | ||
|
||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.lucene.expressions.Bindings; | ||
import org.apache.lucene.expressions.Expression; | ||
import org.apache.lucene.expressions.js.JavascriptCompiler; | ||
import org.apache.lucene.queries.function.ValueSource; | ||
import org.apache.lucene.search.DoubleValuesSource; | ||
import org.apache.solr.common.SolrException; | ||
import org.apache.solr.common.util.NamedList; | ||
import org.apache.solr.schema.IndexSchema; | ||
import org.apache.solr.schema.SchemaField; | ||
|
||
/** | ||
* A ValueSource parser configured with a pre-compiled expression that can then be evaluated at | ||
* request time. It's powered by the Lucene Expressions module, which is a subset of JavaScript. | ||
*/ | ||
public class ExpressionValueSourceParser extends ValueSourceParser { | ||
|
||
public static final String SCORE_KEY = "score-name"; // TODO get rid of this? Why have it? | ||
public static final String EXPRESSION_KEY = "expression"; | ||
|
||
private Expression expression; | ||
private String scoreKey; | ||
private int numPositionalArgs = 0; // Number of positional arguments in the expression | ||
|
||
@Override | ||
public void init(NamedList<?> args) { | ||
initConfiguredExpression(args); | ||
initScoreKey(args); | ||
super.init(args); | ||
} | ||
|
||
/** Checks for optional scoreKey override */ | ||
private void initScoreKey(NamedList<?> args) { | ||
scoreKey = Optional.ofNullable((String) args.remove(SCORE_KEY)).orElse(SolrReturnFields.SCORE); | ||
} | ||
|
||
/** Parses the pre-configured expression */ | ||
private void initConfiguredExpression(NamedList<?> args) { | ||
String expressionStr = | ||
Optional.ofNullable((String) args.remove(EXPRESSION_KEY)) | ||
.orElseThrow( | ||
() -> | ||
new SolrException( | ||
SERVER_ERROR, EXPRESSION_KEY + " must be configured with an expression")); | ||
|
||
// Find the highest positional argument in the expression | ||
Pattern pattern = Pattern.compile("\\$(\\d+)"); | ||
Matcher matcher = pattern.matcher(expressionStr); | ||
while (matcher.find()) { | ||
int argNum = Integer.parseInt(matcher.group(1)); | ||
numPositionalArgs = Math.max(numPositionalArgs, argNum); | ||
} | ||
|
||
// TODO add way to register additional functions | ||
try { | ||
this.expression = JavascriptCompiler.compile(expressionStr); | ||
} catch (ParseException e) { | ||
throw new SolrException( | ||
SERVER_ERROR, "Unable to parse javascript expression: " + expressionStr, e); | ||
} | ||
} | ||
|
||
// TODO: support dynamic expressions: expr("foo * bar / 32") ?? | ||
|
||
@Override | ||
public ValueSource parse(FunctionQParser fp) throws SyntaxError { | ||
assert null != fp; | ||
|
||
// Parse positional arguments if any | ||
List<DoubleValuesSource> positionalArgs = new ArrayList<>(); | ||
for (int i = 0; i < numPositionalArgs; i++) { | ||
ValueSource vs = fp.parseValueSource(); | ||
positionalArgs.add(vs.asDoubleValuesSource()); | ||
} | ||
|
||
IndexSchema schema = fp.getReq().getSchema(); | ||
SolrBindings b = new SolrBindings(scoreKey, schema, positionalArgs); | ||
return ValueSource.fromDoubleValuesSource(expression.getDoubleValuesSource(b)); | ||
} | ||
|
||
/** | ||
* A bindings class that uses schema fields to resolve variables. | ||
* | ||
* @lucene.internal | ||
*/ | ||
public static class SolrBindings extends Bindings { | ||
private final String scoreKey; | ||
private final IndexSchema schema; | ||
private final List<DoubleValuesSource> positionalArgs; | ||
|
||
/** | ||
* @param scoreKey The binding name that should be used to represent the score, may be null | ||
* @param schema IndexSchema for field bindings | ||
* @param positionalArgs List of positional arguments | ||
*/ | ||
public SolrBindings( | ||
String scoreKey, IndexSchema schema, List<DoubleValuesSource> positionalArgs) { | ||
this.scoreKey = scoreKey; | ||
this.schema = schema; | ||
this.positionalArgs = positionalArgs != null ? positionalArgs : new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public DoubleValuesSource getDoubleValuesSource(String key) { | ||
assert null != key; | ||
|
||
if (Objects.equals(scoreKey, key)) { | ||
return DoubleValuesSource.SCORES; | ||
} | ||
|
||
// Check for positional arguments like $1, $2, etc. | ||
if (key.startsWith("$")) { | ||
try { | ||
int position = Integer.parseInt(key.substring(1)); | ||
return positionalArgs.get(position - 1); // Convert to 0-based index | ||
} catch (RuntimeException e) { | ||
throw new IllegalArgumentException("Not a valid positional argument: " + key, e); | ||
} | ||
} | ||
|
||
SchemaField field = schema.getFieldOrNull(key); | ||
if (null != field) { | ||
return field.getType().getValueSource(field, null).asDoubleValuesSource(); | ||
} | ||
|
||
throw new IllegalArgumentException("No binding or schema field for key: " + key); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wish for something like
and then wrap the sortedDocList.iterator() as a Scorable. This could remove the need for a custom mapping between docids and scores in ValueSourceAugmenter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sorting & mapping has to happen somewhere. It's inelegant. Is your point to add a convenience API/method? If we do this in multiple places, then that'd make sense, but not otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looked to me that the mapping between docIds and scores is already established in the DocList (so why do the mapping again?), and only the sorting piece is missing. That's why I was thinking of a way to handle just the sorting.
But I agree - if this is the only place, then I'm OK with the logic as long as it works.