NEST 2.5.1 - Cancel SearchAsync method

(Denis Lamanov) #1

Hi All,

Is there any way to cancel SearchAsync request method in NEST?

I am doing it like this:

var request = new SearchRequest(index, filter.Types)
        RequestConfiguration = new RequestConfiguration()
          CancellationToken = filter.CancellationToken
        Query = query,
        Size = _limitRecordCount,
        Scroll = _scrollTime,
        Sort = sortFields,
      var result = await _client.SearchAsync<ElasticMessage>(request).ConfigureAwait(false);

but search response is not cancellable


(Denis Lamanov) #2

Hi @forloop

Could you please help me?


(Russ Cam) #3

How are you determining that the request is not cancelled?

There was a regression in 2.x and some elements of the HTTP pipeline that were not cancellable, but these were corrected in NEST 2.5.0 with PR There's an example on that PR that uses Fiddler to check cancellation behaviour.

What platform are you using (.NET Framework, .NET Core ) and what OS (Windows)? Reason I ask is that they have different IConnection implementations.

(Denis Lamanov) #4

Hi @forloop,

We use

server: ElasticSearch 5.1.2 on CentOS 6.6,
client: .NET Framework 4.6.2, Windows 7, 8.1, NEST 2.5.2

If I do _cancelTokenSource.Cancel() nothing will happen and server will return result after query on server finished.
Checked this using Fiddler.
As I correctly understand http connection should be broken in this situation.


(Russ Cam) #5

First thing I recommend is updating NEST to the latest 5.x version; 2.5.2 is not compatible with Elasticsearch 5.1.2 - it may work for the most part but where there are breaking changes between Elasticsearch 2.x. and 5.x, NEST 2.5.2 will not work correctly.

After updating to NEST 5.x, the CancellationToken can be passed as a parameter to the SearchAsync<T>() call. Could you try with this using Fiddler and report what you see?

(Denis Lamanov) #6

Sure, will try and report tomorrow.

(Denis Lamanov) #7

Hi @forloop,

Today I updated project to NEST 5.0.1, but still can't cancel query.

Use this code:

_cancelTokenSource = new CancellationTokenSource();

var request = new SearchRequest(index, filter.Types)
Query = query,
Size = _limitRecordCount,
Scroll = _scrollTime,
Sort = sortFields

var result = await _client.SearchAsync(request, _cancelTokenSource.Token);

As I correctly understand I can cancel a query in any part of execution time, even if the query is still running on server.

So, in order to cancel the query I just run this code:


Do you have any ideas why a query is not cancellable?


(Denis Lamanov) #8

We use HTTPS. Can it be the reason?


(Russ Cam) #9

I don't think so.

How are you asserting that the query is not cancelled?

(system) #10

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