# How to custom query result STATUS?

**URL:** <https://discuss.elastic.co/t/how-to-custom-query-result-status/109865>\
**Category:** Elasticsearch\
**Created:** [December 1, 2017, 2:31am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865 "2017-12-01T02:31:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tavandung12](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@tavandung12](https://discuss.elastic.co/u/tavandung12)\
**Post date:** [December 1, 2017, 2:31am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865/1 "2017-12-01T02:31:09Z")

</div>

I use elasticsearch 5.6.3 to write ScriptPlugin with SearchScript.  
when I throw an exception I get:

```json
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "script_score: the script could not be loaded",
        "index_uuid" : "CcfW4YT7Rn2v7dD96uOqGg",
        "index" : "test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "8iFnvKhFQce5KPohroZ_pw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "script_score: the script could not be loaded",
          "index_uuid" : "CcfW4YT7Rn2v7dD96uOqGg",
          "index" : "test",
          "caused_by" : {
            "type" : "query_vector_not_found_exception",
            "reason" : "query vector not found"
          }
        }
      }
    ]
  },
  "status" : 400
}

```

but I want to status:404 to say that "something not found", so, how to?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 1, 2017, 7:55am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865/2 "2017-12-01T07:55:53Z")

</div>

if your exception extends from `ElasticsearchException`, you can overwrite the `RestStatus status()` method to return the correct status.

---

<div class="post-metadata">

**Author:** ![tavandung12](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@tavandung12](https://discuss.elastic.co/u/tavandung12)\
**Post date:** [December 1, 2017, 8:28am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865/3 "2017-12-01T08:28:01Z")

</div>

It's not working

here's my code:

```java
public class QueryVectorNotFoundException extends ElasticsearchException {
	private static final long serialVersionUID = -4813065797253468811L;
	
	public QueryVectorNotFoundException() {
		super("Query Vector not found");
	}
	
	@Override
	public RestStatus status() {
		return RestStatus.NOT_FOUND;
	}

}

```

output:

```json
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_vector_not_found_exception",
        "reason" : "Query Vector not found"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "8iFnvKhFQce5KPohroZ_pw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "script_score: the script could not be loaded",
          "index_uuid" : "CcfW4YT7Rn2v7dD96uOqGg",
          "index" : "test",
          "caused_by" : {
            "type" : "query_vector_not_found_exception",
            "reason" : "Query Vector not found"
          }
        }
      }
    ]
  },
  "status" : 400
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 1, 2017, 8:52am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865/4 "2017-12-01T08:52:21Z")

</div>

hey,

my bad, I did not interpret your code correctly. As your exception is wrapped in a `SearchPhaseException` that one decides about the rest status return code. I dont know on top of my head, if there is a workaround, as your integration is pretty inside of a search request.

--Alex

---

<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:** [December 29, 2017, 8:52am UTC](https://discuss.elastic.co/t/how-to-custom-query-result-status/109865/5 "2017-12-29T08:52:24Z")

</div>

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