# Stop search if client has disconnected

**URL:** <https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873>\
**Category:** Elasticsearch\
**Created:** [July 6, 2018, 9:20am UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873 "2018-07-06T09:20:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [July 6, 2018, 9:20am UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/1 "2018-07-06T09:20:50Z")

</div>

Is it possible to stop the search if the main HTTP connection is closed by the client?

My use case: "a web-site with Nginx+php+elasticsearch, public user does a search and closes the page immediately"

- Nginx connection is closed
- php-fpm is closed (via [http://nginx.org/en/docs/http/ngx\_http\_fastcgi\_module.html#fastcgi\_ignore\_client\_abort](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_ignore_client_abort))
- With php function [http://php.net/manual/en/function.register-shutdown-function.php](http://php.net/manual/en/function.register-shutdown-function.php) I can execute some code just before exiting
- elasticsearch query still continue 😕

I think it's possible with task API, but I am wondering if there is a more elegant way.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 6, 2018, 10:02am UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/2 "2018-07-06T10:02:08Z")

</div>

Do you mean that you have very long queries running? Task API might help indeed but only for long queries (which you should try to avoid anyway).

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [July 6, 2018, 2:27pm UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/3 "2018-07-06T14:27:32Z")

</div>

It's not about long queries, we have timeout for that, it's about "zombie" search, when the end-user has left the web site, but elasticsearch is still searching.

I must setup a monitoring to count how many ES queries are "abandoned" by Php, maybe it's very little, but I am curious to know if it's possible to optimize our ES cluster by stoping "zombie" searches.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 6, 2018, 4:09pm UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/4 "2018-07-06T16:09:44Z")

</div>

Do you mean that it is still consuming resources on elasticsearch end?  
That should not happen AFAIK.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 9, 2018, 1:10am UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/5 "2018-07-09T01:10:43Z")

</div>

I think the [Task API's `X-Opaque-Id` header](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/tasks.html#_identifying_running_tasks) support is the most suitable mechanism for this purpose; if a value is supplied in the search call, it can be used to cancel the tasks with that value after client has aborted.

---

<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:** [August 6, 2018, 1:10am UTC](https://discuss.elastic.co/t/stop-search-if-client-has-disconnected/138873/6 "2018-08-06T01:10:52Z")

</div>

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