Providing Socket tineout exception not working in elastic java api

Hi I am using below libraries to connect to elasticsearch from java
implementation group: 'co.elastic.clients', name: 'elasticsearch-java', version: '8.11.4'
implementation group: 'org.elasticsearch.client', name: 'elasticsearch-rest-client', version: '8.11.4'

I need to provide timeout in my queries to cancel the running query after 50-60 ms as i need to provide response in given time frame.

I explored .timeout() in queries but it didn't work and in my knowledge it was working on per shard level and not on client level.

Then i found we can pass socket timeout to terminate the queries in low level client. This I am using as :


RestClient restClient = RestClient.builder(
                new HttpHost(esHost, Integer.parseInt(esPort)))
                    .setRequestConfigCallback(
                        requestConfigBuilder -> requestConfigBuilder
                                .setConnectTimeout(1)
                                .setSocketTimeout(1))
                .build();

 ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

 return new ElasticsearchClient(transport);

My search query looks like this :

SearchResponse<Void> response = elasticClient.search(s -> s
                        .index(index)
                        .size(0)
                        .timeout("1ms")
                        .aggregations(map)
                        .query(q -> q
                                .bool(b -> b.should(matchEntityQueries))
                        ),
                Void.class
        );

But as I am testing this it is also not working. As you can see i have given 1 ms in both timeout values but request doesn't timeout and keeps on running for response. Please suggest me what I am missing as I have read tonnes of articles on this but anything doesn't seems to explain this.

Thanks

@dadoonet Can you please help here?

Please be patient and refrain from pinging people like this unless they are already discussing in your thread.

May I suggest that you switch to the async client instead? I wrote an example here which seems to be doing what you would expect.

Hey!

Just to add some context:
Yes the .timeout() query parameter works per shard, so except for elaborate queries on complex indexes it will rarely exceed 1ms (which is the minimum value).

@dadoonet Apologies for directly tagging you.
Thanks, the code is working properly using async client and is giving timeout as intended.
But Is there any way we can achieve this using synchronous client and why
RequestConfigCallback is not working as expected even when we set socketTimeout. It would be really helpful if you can explain on this?

@ltrotta Yes had to go through various docs to understand that it is for per shard timeout. Thank you

It's strange that RequestConfigCallback is not working here, I have just tested it with those same libraries and configurations and it returns
java.net.SocketTimeoutException: 1 milliseconds timeout on connection
as expected.
If this is still an issue could you provide more information so that we can try to reproduce the problem?

Thanks for taking time to solve this. But for sync client I am using it in basic way as provided earlier. Not sure what is causing the issue here. Anyways async client works for me for now. Will get back to it if needed in future.
Thanks :slight_smile:

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