-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add script engine for Lucene expressions #6819
Closed
Closed
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f61f23f
Start of expressions integration as a script engine. Does not run
rjernst a95c289
Basic contant parameters and field access (through variables of the g…
rjernst dfae59b
Fixed _score to actually work
rjernst 892d980
Added copies changes from LUCENE-5806. Finished tests.
rjernst c3726a8
Fixed possible NPE when mapping doesn't exist and added test
rjernst 58eb558
Merge branch 'master' into feature/expressions
rjernst b8c8644
Add assert to trip on upgrade to 4.10
rjernst 631a38d
Test terms aggs fail with expressions
rjernst 8edb0ce
Minor readability tweaks before PR
rjernst ad96e83
Merge branch 'master' into feature/expressions
rjernst 8a31ae1
Address first round of review comments
rjernst 5a42e4c
Added scoring TODO and made expressions deps optional
rjernst caa1d60
Fix a test bug, changed execution exception to compilation exception in
rjernst 17091e2
Remove empty benchmark file
rjernst 93f8bb3
Change expression disk extension to match langauge name (to require l…
rjernst ec49e96
Tweaked documentation and add benchmark comparing expressions to nati…
rjernst 1a8154b
Adding experimental warning to docs
rjernst 737b1c3
Merge branch 'master' into feature/expressions
rjernst 140f7da
Address more review comments
rjernst 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 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
128 changes: 128 additions & 0 deletions
128
src/main/java/org/apache/lucene/expressions/XSimpleBindings.java
This file contains 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,128 @@ | ||
package org.apache.lucene.expressions; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.queries.function.ValueSource; | ||
import org.apache.lucene.queries.function.valuesource.DoubleFieldSource; | ||
import org.apache.lucene.queries.function.valuesource.FloatFieldSource; | ||
import org.apache.lucene.queries.function.valuesource.IntFieldSource; | ||
import org.apache.lucene.queries.function.valuesource.LongFieldSource; | ||
import org.apache.lucene.search.SortField; | ||
|
||
/** | ||
* Simple class that binds expression variable names to {@link SortField}s | ||
* or other {@link Expression}s. | ||
* <p> | ||
* Example usage: | ||
* <pre class="prettyprint"> | ||
* XSimpleBindings bindings = new XSimpleBindings(); | ||
* // document's text relevance score | ||
* bindings.add(new SortField("_score", SortField.Type.SCORE)); | ||
* // integer NumericDocValues field (or from FieldCache) | ||
* bindings.add(new SortField("popularity", SortField.Type.INT)); | ||
* // another expression | ||
* bindings.add("recency", myRecencyExpression); | ||
* | ||
* // create a sort field in reverse order | ||
* Sort sort = new Sort(expr.getSortField(bindings, true)); | ||
* </pre> | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public final class XSimpleBindings extends Bindings { | ||
|
||
static { | ||
assert org.elasticsearch.Version.CURRENT.luceneVersion == org.apache.lucene.util.Version.LUCENE_4_9: "Remove this code once we upgrade to Lucene 4.10 (LUCENE-5806)"; | ||
} | ||
|
||
final Map<String,Object> map = new HashMap<>(); | ||
|
||
/** Creates a new empty Bindings */ | ||
public XSimpleBindings() {} | ||
|
||
/** | ||
* Adds a SortField to the bindings. | ||
* <p> | ||
* This can be used to reference a DocValuesField, a field from | ||
* FieldCache, the document's score, etc. | ||
*/ | ||
public void add(SortField sortField) { | ||
map.put(sortField.getField(), sortField); | ||
} | ||
|
||
/** | ||
* Bind a {@link ValueSource} directly to the given name. | ||
*/ | ||
public void add(String name, ValueSource source) { map.put(name, source); } | ||
|
||
/** | ||
* Adds an Expression to the bindings. | ||
* <p> | ||
* This can be used to reference expressions from other expressions. | ||
*/ | ||
public void add(String name, Expression expression) { | ||
map.put(name, expression); | ||
} | ||
|
||
@Override | ||
public ValueSource getValueSource(String name) { | ||
Object o = map.get(name); | ||
if (o == null) { | ||
throw new IllegalArgumentException("Invalid reference '" + name + "'"); | ||
} else if (o instanceof Expression) { | ||
return ((Expression)o).getValueSource(this); | ||
} else if (o instanceof ValueSource) { | ||
return ((ValueSource)o); | ||
} | ||
SortField field = (SortField) o; | ||
switch(field.getType()) { | ||
case INT: | ||
return new IntFieldSource(field.getField()); | ||
case LONG: | ||
return new LongFieldSource(field.getField()); | ||
case FLOAT: | ||
return new FloatFieldSource(field.getField()); | ||
case DOUBLE: | ||
return new DoubleFieldSource(field.getField()); | ||
case SCORE: | ||
return getScoreValueSource(); | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
/** | ||
* Traverses the graph of bindings, checking there are no cycles or missing references | ||
* @throws IllegalArgumentException if the bindings is inconsistent | ||
*/ | ||
public void validate() { | ||
for (Object o : map.values()) { | ||
if (o instanceof Expression) { | ||
Expression expr = (Expression) o; | ||
try { | ||
expr.getValueSource(this); | ||
} catch (StackOverflowError e) { | ||
throw new IllegalArgumentException("Recursion Error: Cycle detected originating in (" + expr.sourceText + ")"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
I am not sure if we should mark it optional due to all the dependencies it has maybe @kimchy knows what to do?
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 only has two: antlr-runtime and asm. I don't think its that many :)
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.
I agree with @rmuir. If we make it optional, we would have to do some magic to make the expression script engine load only if expressions are available (which I guess isn't that hard to do, but the other built in script engines don't assume they are optional).
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 should be optional, but we should make sure we package it as part of our distro. In our script service we already support optional mvel and groovy, so thats easy, similar logic.
Its less about the size, its about the fact that those are 2 very popular libraries, and almost certainly will cause clashes when someone wants to use a NodeClient / TransportClient
In order to include it in our distro, make sure to add it in the deb/rpm logic in the pom.xml (with all deps), and in src/main/assemblies/common-bin, then run the package and check tar.gz includes all the deps, as well as the deb and rpm
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.
Ok I made the dep optional. I didn't need to change anything else since the lib starts with "lucene" and lucene* is already included. I checked the tar.gz and it includes all 3 deps.