NEST 2.5.1 - Cancel SearchAsync method

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

Thanks

Hi @forloop

Could you please help me?

Thanks

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 fix #2438 make sure each async part of HttpConnection is abortable by Mpdreamz · Pull Request #2440 · elastic/elasticsearch-net · GitHub. 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.

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.

Thanks

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?

Sure, will try and report tomorrow.

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:

_cancelTokenSource.Cancel();

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

Thanks

We use HTTPS. Can it be the reason?

Thanks

I don't think so.

How are you asserting that the query is not cancelled?

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