Not able to search from java

I want to create a simple java program for elastic search.
from the documentations and examples i came up with the following

package testListJava;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;


public class test {

    public static void main(String[] args) {
    	Node node = nodeBuilder().clusterName("mycluster1").node();
    	Client client = node.client();
    	
    	GetResponse response = client.prepareGet("abc", "def", "1")
    	        .execute()
    	        .actionGet();

        System.out.println(response.toString());  
        System.out.println("-----------------------------------"); 
    }

}

But I am not able to get the response.
I get the following error.

Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.common.settings.Settings.settingsBuilder()Lorg/elasticsearch/common/settings/Settings$Builder;
at org.elasticsearch.node.NodeBuilder.(NodeBuilder.java:57)
at org.elasticsearch.node.NodeBuilder.nodeBuilder(NodeBuilder.java:63)
at testListJava.test.main(test.java:12)

what should I do?

I am using elasticsearch-5.5.2.
openjdk version "1.8.0_111"
Eclipse Platform Version: Luna SR1 (4.4.1)

What does your pom.xml looks like?

MY pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.5.2</version>


</project>

No dependency here.

Your Artifact coordinates are wrong BTW.

Sounds like you're pretty much where I am.

See this thread: ElasticSearch, NetBeans, Maven and Java

It's got my notes on setting up a simple java program to use elasticsearch under NetBeans. The doco elastic gives us is less than helpful...

I'm writing code to index new data, but the only difference between that and a search should be the action call towards the end of the try loop. That's probably what I'll try next. (I did index first so I'd have something to search for. :wink:)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>check</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
        </dependency>
    </dependencies>




</project>

I am getting the following errors :

[main] INFO org.elasticsearch.plugins.PluginsService - no modules loaded
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.transport.Netty3Plugin]
[main] INFO org.elasticsearch.plugins.PluginsService - loaded plugin [org.elasticsearch.transport.Netty4Plugin]
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: []]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
	at check.main(check.java:13)

What should I do now?

What does you java code looks like?

Did you read: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html ?

My java code is

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class check {

    public static void main(String[] args) {
        Settings settings = Settings.builder()
                .put("cluster.name", "mycluster1").build();
        try {
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9200))
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9200));
            GetResponse response = client.prepareGet("bank", "account", "1").get();

            System.out.println(response.toString());
            System.out.println("-----------------------------------");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }



}

TransportClient does not use port 9200 but 9300.
Also what are the elasticsearch logs? (server side)

I tried both 9200 and 9300 port. Both are not working.

The elasticsearch logs:

[2017-09-12T14:00:32,879][INFO ][o.e.n.Node               ] [] initializing ...
[2017-09-12T14:00:32,940][INFO ][o.e.e.NodeEnvironment    ] [_-RF39w] using [1] data paths, mounts [[/home (/dev/nvme0n1p2)]], net usable_space [185.7gb], net total_space [311.5gb], spins? [no], types [ext4]
[2017-09-12T14:00:32,940][INFO ][o.e.e.NodeEnvironment    ] [_-RF39w] heap size [1.9gb], compressed ordinary object pointers [true]
[2017-09-12T14:00:32,941][INFO ][o.e.n.Node               ] node name [_-RF39w] derived from node ID [_-RF39w2Qj6dM9iK0Fn7XQ]; set [node.name] to override
[2017-09-12T14:00:32,941][INFO ][o.e.n.Node               ] version[5.5.2], pid[32504], build[b2f0c09/2017-08-14T12:33:14.154Z], OS[Linux/4.4.0-93-generic/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_111/25.111-b14]
[2017-09-12T14:00:32,941][INFO ][o.e.n.Node               ] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/home/local/ZOHOCORP/nishanth-5204/elasticsearch-5.5.2]
[2017-09-12T14:00:33,731][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [aggs-matrix-stats]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [ingest-common]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [lang-expression]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [lang-groovy]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [lang-mustache]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [lang-painless]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [parent-join]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [percolator]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [reindex]
[2017-09-12T14:00:33,732][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [transport-netty3]
[2017-09-12T14:00:33,733][INFO ][o.e.p.PluginsService     ] [_-RF39w] loaded module [transport-netty4]
[2017-09-12T14:00:33,733][INFO ][o.e.p.PluginsService     ] [_-RF39w] no plugins loaded
[2017-09-12T14:00:35,050][INFO ][o.e.d.DiscoveryModule    ] [_-RF39w] using discovery type [zen]
[2017-09-12T14:00:35,641][INFO ][o.e.n.Node               ] initialized
[2017-09-12T14:00:35,641][INFO ][o.e.n.Node               ] [_-RF39w] starting ...
[2017-09-12T14:00:35,803][INFO ][o.e.t.TransportService   ] [_-RF39w] publish_address {127.0.0.1:9301}, bound_addresses {[::1]:9300}, {127.0.0.1:9301}
[2017-09-12T14:00:35,815][WARN ][o.e.b.BootstrapChecks    ] [_-RF39w] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2017-09-12T14:00:35,999][WARN ][o.e.t.n.Netty4Transport  ] [_-RF39w] exception caught on transport layer [[id: 0x3121287e, L:/127.0.0.1:56846 - R:/127.0.0.1:9300]], closing connection
java.io.IOException: Invalid string; unexpected character: 253 hex: fd
	at org.elasticsearch.common.io.stream.StreamInput.readString(StreamInput.java:372) ~[elasticsearch-5.5.2.jar:5.5.2]
	at org.elasticsearch.common.util.concurrent.ThreadContext$ThreadContextStruct.<init>(ThreadContext.java:362) ~[elasticsearch-5.5.2.jar:5.5.2]
	at org.elasticsearch.common.util.concurrent.ThreadContext$ThreadContextStruct.<init>(ThreadContext.java:352) ~[elasticsearch-5.5.2.jar:5.5.2]
	at org.elasticsearch.common.util.concurrent.ThreadContext.readHeaders(ThreadContext.java:186) ~[elasticsearch-5.5.2.jar:5.5.2]
	at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1383) ~[elasticsearch-5.5.2.jar:5.5.2]
	at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:74) ~[transport-netty4-5.5.2.jar:5.5.2]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) [netty-codec-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:297) [netty-codec-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:413) [netty-codec-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265) [netty-codec-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.11.Final.jar:4.1.11.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)

Elasticsearch is bound to 127.0.0.1. If host1 is anything else than 127.0.0.1 it will not work.

If you want to call elasticsearch from another machine, you need to change network.host in elasticsearch.yml.

In such a case, read:

[2017-09-12T14:00:35,999][WARN ][o.e.t.n.Netty4Transport  ] [_-RF39w] exception caught on transport layer [[id: 0x3121287e, L:/127.0.0.1:56846 - R:/127.0.0.1:9300]], closing connection
java.io.IOException: Invalid string; unexpected character: 253 hex: fd

^^^ Was that you attempt to use 9200 port instead of 9300?

I have not made any attempt to use 9200 port instead of 9300.
In curl request I am able to elastic search in 127.0.0.1:9200.

I now found that in localhost:9200 I have cluster named elasticsearch and no indexes in it.
And in 127.0.0.1:9200 I have a cluster named mycluster1 and I have 3 indexes in it which is the one I am trying to query.

Your code does not show that. You set a cluster name, try to connect to 2 hosts....

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.