java - ElasticSearch 2.2.0 - ESIntegTestCase - 在搜索中执行 groovy 脚本时出现 ClassNotFoundException

标签 java elasticsearch groovy

我一直在使用 Elastic 1.4.4,但我们现在正在升级到 2.2.0。我无法运行集成测试。我的集成测试扩展了org.elasticsearch.test.ESIntegTestCase:

@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
public abstract class AbstractApplicationTest extends ESIntegTestCase {
  ...
}

我可以毫无问题地索引文档,但是当我尝试使用脚本字段进行搜索时,出现错误。我正在使用 sbt 运行测试(我正在使用 Play 框架)。

我收到的错误如下:

{
    "error": {
        "root_cause": [{
            "type": "script_exception",
            "reason": "failed to compile groovy script"
        }],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [{
            "shard": 0,
            "index": "bokun",
            "node": "BNyjts9hTOicRgCAWGdKgQ",
            "reason": {
                "type": "script_exception",
                "reason": "Failed to compile inline script [if(_source.accumulated_availability != null){  for(item in _source.accumulated_availability){    if(start.compareTo(item.day) < 0 && (end == null || end.compareTo(item.day) >= 0)){      return item.day    }  }} else return null;] using lang [groovy]",
                "caused_by": {
                    "type": "script_exception",
                    "reason": "failed to compile groovy script",
                    "caused_by": {
                        "type": "multiple_compilation_errors_exception",
                        "reason": "startup failed:\nCould not instantiate global transform class groovy.grape.GrabAnnotationTransformation specified at jar:file:/Users/ogg/.ivy2/cache/org.codehaus.groovy/groovy-all/jars/groovy-all-2.4.4-indy.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.ClassNotFoundException: groovy.grape.GrabAnnotationTransformation\n\nCould not instantiate global transform class org.codehaus.groovy.ast.builder.AstBuilderTransformation specified at jar:file:/Users/ogg/.ivy2/cache/org.codehaus.groovy/groovy-all/jars/groovy-all-2.4.4-indy.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.ClassNotFoundException: org.codehaus.groovy.ast.builder.AstBuilderTransformation\n\n2 errors\n"
                    }
                }
            }
        }]
    },
    "status": 500
}

我将重新格式化“原因”消息以提高可读性:

startup failed:

Could not instantiate global transform class 
groovy.grape.GrabAnnotationTransformation 
specified at jar:file:/Users/ogg/.ivy2/cache/org.codehaus.groovy/groovy-all/jars/groovy-all-2.4.4-indy.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  

because of exception 
java.lang.ClassNotFoundException: groovy.grape.GrabAnnotationTransformation

Could not instantiate global transform class 
org.codehaus.groovy.ast.builder.AstBuilderTransformation 
specified at jar:file:/Users/ogg/.ivy2/cache/org.codehaus.groovy/groovy-all/jars/groovy-all-2.4.4-indy.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  

because of exception java.lang.ClassNotFoundException: org.codehaus.groovy.ast.builder.AstBuilderTransformation

什么可能导致这种情况?据我所知,我的类路径中有这个类:org.codehaus.groovy.ast.builder.AstBuilderTransformation。 我的 build.sbt 中有以下依赖项:

  "org.codehaus.groovy" % "groovy-all" % "2.4.4",
  "com.carrotsearch.randomizedtesting" % "randomizedtesting-runner" % "2.3.0" % "test",
  "org.apache.lucene" % "lucene-test-framework" % "5.4.1",
  "org.elasticsearch" % "elasticsearch" % "2.2.0" % "test" classifier "tests" withSources(),
  "org.elasticsearch" % "elasticsearch" % "2.2.0" withSources(),
  "org.elasticsearch.plugin" % "analysis-icu" % "2.2.0" % "test",
  "org.elasticsearch.module" % "lang-groovy" % "2.2.0" % "test"

...我的 EsIntegTestCase 扩展类中有以下内容:

@Override
protected Settings nodeSettings(int nodeOrdinal) {
    return Settings.settingsBuilder()
            .put(super.nodeSettings(nodeOrdinal))
            .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)
            .put(Node.HTTP_ENABLED, true)
            .put("script.groovy.sandbox.enabled", true)
            .put("script.engine.groovy.inline.search", true)
            .put("script.engine.groovy.inline.update", "true")
            .put("script.inline", true)
            .put("script.update", true)
            .put("script.indexed", true)
            .put("script.default_lang", "groovy")
            .build();
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
    return pluginList(GroovyPlugin.class, AnalysisICUPlugin.class);
}

我完全陷入困境,Google 不愿意提供帮助! :微微微笑:

任何帮助或指示将不胜感激。

非常感谢, 奥格

最佳答案

这个问题现在已经解决了。

问题实际上是这是一个 SecurityException 被重新抛出为 ClassNotFoundException。

使用 https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html 中的说明我创建了一个安全策略文件并添加了以下类的权限:

grant {
    permission org.elasticsearch.script.ClassPermission "java.lang.Class";
    permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.*";
    permission org.elasticsearch.script.ClassPermission "groovy.*";
    permission org.elasticsearch.script.ClassPermission "java.lang.*";
    permission org.elasticsearch.script.ClassPermission "java.util.*";
    permission org.elasticsearch.script.ClassPermission "java.math.BigDecimal";
    permission org.elasticsearch.script.ClassPermission "org.joda.time.*";
};

然后我开始在命令行上传递安全策略文件的测试:

-Djava.security.policy=security.policy

您可以在 Elastic 论坛上看到帮助我找到此解决方案的帖子:https://discuss.elastic.co/t/2-2-0-esintegtestcase-classnotfoundexception-when-executing-groovy-script-in-search/43579

关于java - ElasticSearch 2.2.0 - ESIntegTestCase - 在搜索中执行 groovy 脚本时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824822/

相关文章:

java - Talend Build Job - 编译问题

java - 将封闭范围之间的奇数添加到数组

elasticsearch - 戈朗 : How to construct a struct for Elasticsearch pipeline attachment

mongodb - 是否有像 mongoosastic 这样的插件可以与环回一起使用?

android-studio - Android Studio 3.4 Canary 3编译错误

java - 相当于@Primary的Spring XML

elasticsearch - Elasticsearch :聚合随机顺序?

groovy - 转换为 bool 值时,groovy 会隐式调用 Matcher 上的查找吗?

grails - 是否可以以编程方式区分美国和加拿大的电话号码?

java - Java 从 1.6 迁移到 1.7 后,由于 SocketChannel,我收到编译错误