zanona
zanona

Reputation: 12717

using ElasticSearch Java Client API with plugin

I was wondering if is it somehow possible to make an ES 1.3.1 plugin run the Client API so I can perform ES searching along my ElasticSearch plugin?

I have tried the following code:

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;
    }
}

but it returns the following compile error:

[ERROR] class file for java.lang.AutoCloseable not found

Any ideas on how to make it work?

Installation details:

$ 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)

Source:

The current file structure containing pom.xml file is located here.

Upvotes: 2

Views: 4258

Answers (2)

oneklc
oneklc

Reputation: 3174

To get the client when setting up an elastic search plugin i found it much easier to just save it when the plugin is setup during runtime.

For example"

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();
    }

}

Upvotes: 0

Chris Albright
Chris Albright

Reputation: 154

Your pom.xml file does not specify which version of java to compile for. The default is 1.3 I think (I know, lame) and that version certainly does not have java.lang.AutoCloseable.

Anyway, try adding this to your pom.xml in the plugins section:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

You'll also need to have java 1.8 installed on the system you're compiling the plugin AND set JAVA_HOME to the root path for your install.

So, if you're java binary is at /home/user/java/bin/java, your JAVA_HOME environment variable should be set to '/home/user/java'.

Upvotes: 3

Related Questions