How to custom query result STATUS?

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

{
  "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?

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

It's not working

here's my code:

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:

{
  "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
}

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

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