我想知道是否有可能使ES 1.3.1插件运行客户端API,以便我可以在我的ElasticSearch插件上执行ES搜索?
我尝试过以下代码:
package org.elasticsearch.plugin.example;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.plugins.AbstractPlugin;
import java.util.Collection;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
public class ExamplePlugin extends AbstractPlugin {
@Override public String name() { return "example-plugin"; }
@Override public String description() { return "Example Plugin Description"; }
@Override public Collection<Class<? extends Module>> modules() {
//Trying to create a client to perform searching
Client client = NodeBuilder.nodeBuilder().client(true).node().client();
//returns error java.lang.AutoCloseable [not found];
Collection<Class<? extends Module>> modules = Lists.newArrayList();
modules.add(ExampleRestModule.class);
return modules;
}
}
但它返回以下编译错误:
[ERROR] class file for java.lang.AutoCloseable not found
关于如何使其发挥作用的任何想法?
安装详情:
$ elasticsearch -v
Version: 1.3.1, Build: 2de6dc5/2014-07-28T14:45:15Z, JVM: 1.8.0_11
$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
包含pom.xml
文件的当前文件结构位于here。
答案 0 :(得分:3)
您的pom.xml文件未指定要编译的java版本。默认值是1.3我想(我知道,跛脚),那个版本肯定没有java.lang.AutoCloseable。
无论如何,尝试将其添加到插件部分的pom.xml中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
您还需要在系统上安装java 1.8,然后再编译插件并将JAVA_HOME设置为安装的根路径。
因此,如果您的java二进制文件位于/ home / user / java / bin / java,则您的JAVA_HOME环境变量应设置为&#39; / home / user / java&#39;。
答案 1 :(得分:0)
为了在设置弹性搜索插件时获取客户端,我发现在运行时设置插件时更容易保存它。
例如“
public class ExampleRestHandler implements RestHandler {
private Client search_client; //client to save for later
@Inject
public ExampleRestHandler(Settings settings, Client client, RestController restController) {
this.search_client = client; //save the client for searching later
restController.registerHandler(GET, "/_hello", this);
}
@Override
public void handleRequest(final RestRequest request,
final RestChannel channel) {
TermsQueryBuilder qb = QueryBuilders.termsQuery("EXAMPLE_FIELD", "EXAMPLE_FIELD_VALLUE").minimumMatch(1);
SearchResponse search_response = search_client.prepareSearch("INDEX_NAME")
.setQuery(qb).execute().actionGet();
XContentBuilder builder;
try {
builder = XContentFactory.jsonBuilder();
builder.startObject();
search_response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
channel.sendResponse(new BytesRestResponse(OK, builder.string()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}