# How to get in C# client not-null response even for failed search requests?

**URL:** <https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 22, 2023, 11:36am UTC](https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358 "2023-08-22T11:36:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonid\_P](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@Leonid\_P](https://discuss.elastic.co/u/Leonid_P)\
**Post date:** [August 22, 2023, 11:36am UTC](https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358/1 "2023-08-22T11:36:34Z")

</div>

Hi there!

There is C# code that generates searches through code like that:  
` searchResult = client.Search(descriptor);`

The request generated contains inappropriate data, and therefore I get `Elasticsearch.Net.ElasticsearchClientException`, and it's completely OK. When I turn on `DisableDirectStreaming`, I can see the json that ElasticsearchCluster returns and the root\_cause in my `apiCallDetails`:  
`"reason" : "[simple_query_string] analyzer [custom_search_analyzer] not found",`

The problem is that I can not turn on `DisableDirectStreaming` due to perfomance cost and that `searchResult` is **null** , though the documentation on `ElasticsearchClientException` [Exceptions | Elasticsearch.Net and NEST: the .NET clients [6.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/thrown-exceptions.html) states that the response should contain `ServerError` with the root cause.

What am I doing wrong here? How can I tune the code or the elasticsearch cluster to get from `client.Search` not-null response with `ServerError` and the root cause?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [August 24, 2023, 8:54am UTC](https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358/2 "2023-08-24T08:54:05Z")

</div>

Hi @Leonid_P,

Welcome back! I've moved your question to the `language-clients` tag as I think it's more appropriate.

Can you share the code snippet?

---

<div class="post-metadata">

**Author:** ![Leonid\_P](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@Leonid\_P](https://discuss.elastic.co/u/Leonid_P)\
**Post date:** [August 29, 2023, 7:15am UTC](https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358/3 "2023-08-29T07:15:17Z")

</div>

Hi Carly, thank you for changing the tag, it's got really more correct, here are my sting of code and some additional explainaition:

```auto
// Client generation
var settings = new ConnectionSettings(connectionPool)
				.DisableDirectStreaming(false)
				.ThrowExceptions(true)
				.// and some more settings which are not importaint here

return new ElasticClient(settings);

```

and

```auto
// The search that I need to deal with:
var searchResult = await ReadClient?.SearchAsync(descriptor);

```

My goal is to get the root\_cause for failed searchesfor example  
`"reason" : "[simple_query_string] analyzer [custom_search_analyzer] not found",`

How can I do it?

I would like neither setting `DisableDirectStreaming(true)` (due to performance reasons), nor 'ThrowExceptions(true)` (because for most searches with the same client we do need the exception).

So the desirable code is something like this:

```auto
var searchResult = await ReadClient?.SearchAsync(descriptor.MyTrickyWayToGetNotNullResponseEvenForFailedRequestsThoughThrowExceptionsIsSwitchedOn());

```

or some tricky way to get the root cause in `.OnRequestCompleted()` though `DisableDirectStreaming` is set to false.

Is it possible without creating different slients wit different `ThrowExceptions` behaviour?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 26, 2023, 7:15am UTC](https://discuss.elastic.co/t/how-to-get-in-c-client-not-null-response-even-for-failed-search-requests/341358/4 "2023-09-26T07:15:19Z")

</div>

Closed on September 26, 2023, 7:15am UTC

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