2

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.

7
  • What version of compiler (JDK) are you using? The error implies you're compiling with Java 6. Commented Aug 26, 2014 at 16:17
  • hey @user1676075, I just updated the installation details sections. I am not sure if that explains it? Otherwise, is there any command I can confirm that? Thank you Commented Aug 26, 2014 at 19:06
  • 2
    Where/how are you compiling your code? Eclipse? If so, check project properties, compiler version. You say you're getting that error at compile time, right, which means it probably has nothing to do with your ES installation, but your dev env instead. Commented Aug 26, 2014 at 20:00
  • i agree with user. if it is eclipse, check your compiler settings. if compile settings are ok, just try clean/rebuild as a sanity check. if you are using maven, check your pom java dependency as well as JAVA_HOME for maven. Commented Aug 28, 2014 at 18:28
  • thanks @user1676075 and @coffeeaddict. I have updated the question with the gist containing the file structure, including the pom.xml file. I have to agree with you guys thinking there might be something wrong with it. I’m just not sure why since Java is a completely new world for me :/ @coffeeaddict, you have mentioned the JAVA_HOME for maven, do you know how can I check that? Thanks Commented Aug 29, 2014 at 8:59

2 Answers 2

3
+50

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'.

Sign up to request clarification or add additional context in comments.

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.