2

我想知道是否有可能让 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位于此处

4

2 回答 2

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 环境变量应设置为“/home/user/java”。

于 2014-09-02T07:47:02.593 回答
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();
    }

}
于 2014-11-13T23:00:34.277 回答