Permalink
Browse files

Scripts: Allow to register native scripts (Java) for better script ex…

…ecution performance, closes #752.
  • Loading branch information...
1 parent 1242cf5 commit 4bdae621f92beb226cf5873a9efe721b38c7e0c7 @kimchy kimchy committed Mar 7, 2011
Showing with 490 additions and 12 deletions.
  1. +1 −1 ...k/micro/src/main/java/org/elasticsearch/benchmark/percolator/EmbeddedPercolatorBenchmarkTest.java
  2. +1 −1 modules/elasticsearch/src/main/java/org/elasticsearch/node/internal/InternalNode.java
  3. +37 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/AbstractDoubleSearchScript.java
  4. +30 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/AbstractExecutableScript.java
  5. +37 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/AbstractFloatSearchScript.java
  6. +37 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/AbstractLongSearchScript.java
  7. +102 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/AbstractSearchScript.java
  8. +82 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/NativeScriptEngineService.java
  9. +44 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/NativeScriptFactory.java
  10. +36 −1 modules/elasticsearch/src/main/java/org/elasticsearch/script/ScriptModule.java
  11. +1 −5 modules/elasticsearch/src/main/java/org/elasticsearch/script/SearchScript.java
  12. +4 −0 modules/elasticsearch/src/main/java/org/elasticsearch/script/mvel/MvelScriptEngineService.java
  13. +1 −1 modules/elasticsearch/src/test/java/org/elasticsearch/index/percolator/PercolatorExecutorTests.java
  14. +1 −1 ...asticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java
  15. +1 −1 ...earch/src/test/java/org/elasticsearch/index/query/xcontent/guice/IndexQueryParserModuleTests.java
  16. +1 −1 ...arch/src/test/java/org/elasticsearch/index/query/xcontent/plugin/IndexQueryParserPluginTests.java
  17. +62 −0 modules/elasticsearch/src/test/java/org/elasticsearch/script/NativeScriptTests.java
  18. +4 −0 plugins/lang/groovy/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java
  19. +4 −0 ...g/javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java
  20. +4 −0 plugins/lang/python/src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java
@@ -62,7 +62,7 @@ public static void main(String[] args) throws Exception {
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new ThreadPoolModule(settings),
- new ScriptModule(),
+ new ScriptModule(settings),
new MapperServiceModule(),
new IndexSettingsModule(settings),
new IndexCacheModule(settings),
@@ -119,7 +119,7 @@ public InternalNode(Settings pSettings, boolean loadConfigSettings) throws Elast
modules.add(new NodeModule(this));
modules.add(new NetworkModule());
modules.add(new NodeCacheModule(settings));
- modules.add(new ScriptModule());
+ modules.add(new ScriptModule(settings));
modules.add(new JmxModule(settings));
modules.add(new EnvironmentModule(environment));
modules.add(new NodeEnvironmentModule());
@@ -0,0 +1,37 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+public abstract class AbstractDoubleSearchScript extends AbstractSearchScript {
+
+ @Override public Object run() {
+ return runAsDouble();
+ }
+
+ @Override public abstract double runAsDouble();
+
+ @Override public long runAsLong() {
+ return (long) runAsDouble();
+ }
+
+ @Override public float runAsFloat() {
+ return (float) runAsDouble();
+ }
+}
@@ -0,0 +1,30 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+public abstract class AbstractExecutableScript implements ExecutableScript {
+
+ @Override public void setNextVar(String name, Object value) {
+ }
+
+ @Override public Object unwrap(Object value) {
+ return value;
+ }
+}
@@ -0,0 +1,37 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+public abstract class AbstractFloatSearchScript extends AbstractSearchScript {
+
+ @Override public Object run() {
+ return runAsFloat();
+ }
+
+ @Override public abstract float runAsFloat();
+
+ @Override public double runAsDouble() {
+ return runAsFloat();
+ }
+
+ @Override public long runAsLong() {
+ return (long) runAsFloat();
+ }
+}
@@ -0,0 +1,37 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+public abstract class AbstractLongSearchScript extends AbstractSearchScript {
+
+ @Override public Object run() {
+ return runAsLong();
+ }
+
+ @Override public abstract long runAsLong();
+
+ @Override public double runAsDouble() {
+ return runAsLong();
+ }
+
+ @Override public float runAsFloat() {
+ return runAsLong();
+ }
+}
@@ -0,0 +1,102 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.Scorer;
+import org.elasticsearch.search.lookup.DocLookup;
+import org.elasticsearch.search.lookup.FieldsLookup;
+import org.elasticsearch.search.lookup.SearchLookup;
+import org.elasticsearch.search.lookup.SourceLookup;
+
+/**
+ * A base class for any script type that is used during the search process (custom score, facets, and so on).
+ *
+ * <p>If the script returns a specific numeric type, consider overriding the type specific base classes
+ * such as {@link AbstractDoubleSearchScript}, {@link AbstractFloatSearchScript} and {@link AbstractLongSearchScript}
+ * for better performance.
+ *
+ * <p>The use is required to implement the {@link #run()} method.
+ */
+public abstract class AbstractSearchScript extends AbstractExecutableScript implements SearchScript {
+
+ private SearchLookup lookup;
+
+ private float score = Float.NaN;
+
+ // helper methods
+ protected final float score() {
+ return score;
+ }
+
+ /**
+ * Returns the doc lookup allowing to access field data (cached) values as well as the current document score
+ * (where applicable).
+ */
+ protected final DocLookup doc() {
+ return lookup.doc();
+ }
+
+ /**
+ * Allows to access the actual source (loaded and parsed).
+ */
+ protected final SourceLookup source() {
+ return lookup.source();
+ }
+
+ /**
+ * Allows to access the *stored* fields.
+ */
+ protected final FieldsLookup fields() {
+ return lookup.fields();
+ }
+
+ void setLookup(SearchLookup lookup) {
+ this.lookup = lookup;
+ }
+
+ @Override public void setScorer(Scorer scorer) {
+ lookup.setScorer(scorer);
+ }
+
+ @Override public void setNextReader(IndexReader reader) {
+ lookup.setNextReader(reader);
+ }
+
+ @Override public void setNextDocId(int doc) {
+ lookup.setNextDocId(doc);
+ }
+
+ @Override public void setNextScore(float score) {
+ this.score = score;
+ }
+
+ @Override public float runAsFloat() {
+ return ((Number) run()).floatValue();
+ }
+
+ @Override public long runAsLong() {
+ return ((Number) run()).longValue();
+ }
+
+ @Override public double runAsDouble() {
+ return ((Number) run()).doubleValue();
+ }
+}
@@ -0,0 +1,82 @@
+/*
+ * Licensed to Elastic Search and Shay Banon under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Elastic Search 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.elasticsearch.script;
+
+import org.elasticsearch.ElasticSearchIllegalArgumentException;
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.collect.ImmutableMap;
+import org.elasticsearch.common.component.AbstractComponent;
+import org.elasticsearch.common.inject.Inject;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.search.lookup.SearchLookup;
+
+import java.util.Map;
+
+/**
+ * A native script engine service.
+ */
+public class NativeScriptEngineService extends AbstractComponent implements ScriptEngineService {
+
+ private final ImmutableMap<String, NativeScriptFactory> scripts;
+
+ @Inject public NativeScriptEngineService(Settings settings, Map<String, NativeScriptFactory> scripts) {
+ super(settings);
+ this.scripts = ImmutableMap.copyOf(scripts);
+ }
+
+ @Override public String[] types() {
+ return new String[]{"native"};
+ }
+
+ @Override public String[] extensions() {
+ return new String[0];
+ }
+
+ @Override public Object compile(String script) {
+ NativeScriptFactory scriptFactory = scripts.get(script);
+ if (scriptFactory != null) {
+ return scriptFactory;
+ }
+ throw new ElasticSearchIllegalArgumentException("Native script [" + script + "] not found");
+ }
+
+ @Override public ExecutableScript executable(Object compiledScript, @Nullable Map<String, Object> vars) {
+ NativeScriptFactory scriptFactory = (NativeScriptFactory) compiledScript;
+ return scriptFactory.newScript(vars);
+ }
+
+ @Override public SearchScript search(Object compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) {
+ NativeScriptFactory scriptFactory = (NativeScriptFactory) compiledScript;
+ AbstractSearchScript script = (AbstractSearchScript) scriptFactory.newScript(vars);
+ script.setLookup(lookup);
+ return script;
+ }
+
+ @Override public Object execute(Object compiledScript, Map<String, Object> vars) {
+ return executable(compiledScript, vars).run();
+ }
+
+ @Override public Object unwrap(Object value) {
+ return value;
+ }
+
+ @Override public void close() {
+ }
+}
Oops, something went wrong.

0 comments on commit 4bdae62

Please sign in to comment.