Elasticsearch Java client async term query not working

Hi all,

I am trying to execute a term query from the elasticsearch java client and I am not getting any results. The code is as follows,

    final Client client = TransportClient.builder()
        .addPlugin(ShieldPlugin.class)
        .settings(Settings.builder()
                .put("shield.user", "admin:password12")
                .put("client.transport.sniff", true)
                .build())
        .build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    client.prepareSearch("items")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.termQuery("itemid", "1234"))
        .setExplain(false)
        .execute(new ActionListener<SearchResponse>() {
            @Override
            public void onResponse(SearchResponse searchResponse) {
                System.out.println("Response : " + searchResponse.toString());
                }
            
            @Override
            public void onFailure(Throwable throwable) {
                System.err.println("Error :" + throwable.getMessage());
                }
            });

Whereas if I use it as .execute().actionGet() I am getting the results fine. What am I doing wrong here?

Thanks,
G

Are you running that within a test?

I mean that is there any chance your application closes early?

1 Like

I am running it inside the main method

My bad, if I introduce a Thread.sleep(..) it works fine.. sorry about that..Thanks David