# How to query custom rest handler in elastic search using Java api

**URL:** <https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079>\
**Category:** Elasticsearch\
**Created:** [January 2, 2014, 1:14pm UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079 "2014-01-02T13:14:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shishir\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Shishir\_Kumar](https://discuss.elastic.co/u/Shishir_Kumar)\
**Post date:** [January 2, 2014, 1:14pm UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/1 "2014-01-02T13:14:15Z")

</div>

Hi,  
I have implemented a simple custom rest handler class for elastic search.  
If I need to call it using curl it works just fine.  
curl -XGET '10.114.24.132:9200/\_mastering/nodes?pretty'

However, I want to call this using the elastic search Java api (with an  
embedded client node). Could you please help me with this? I am new to  
elastic search and not able to figure this out.

P.S. I had followed the instructions given in the link  
[http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/](http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/)  
for setting up the custom handler.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 2, 2014, 6:22pm UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/2 "2014-01-02T18:22:51Z")

</div>

You would need to supply information about your custom class for more  
details. Did you create a new Action hierarchy? The handleRequest method of  
your BaseRestHandler implementation should provide details on how the Java  
API is used.

Cheers,

Ivan

On Thu, Jan 2, 2014 at 5:14 AM, Shishir Kumar [shishir.sunny@gmail.com](mailto:shishir.sunny@gmail.com)wrote:

> Hi,  
> I have implemented a simple custom rest handler class for Elasticsearch.  
> If I need to call it using curl it works just fine.  
> curl -XGET '10.114.24.132:9200/\_mastering/nodes?pretty'
> 
> However, I want to call this using the Elasticsearch Java api (with an  
> embedded client node). Could you please help me with this? I am new to  
> Elasticsearch and not able to figure this out.
> 
> P.S. I had followed the instructions given in the link \<  
> [http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/](http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/)\>  
> for setting up the custom handler.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBMCycmgsU0x7duaWZYF7XyXMvLZRgvMhceSDCQQiu5vQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBMCycmgsU0x7duaWZYF7XyXMvLZRgvMhceSDCQQiu5vQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Shishir\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Shishir\_Kumar](https://discuss.elastic.co/u/Shishir_Kumar)\
**Post date:** [January 3, 2014, 7:00am UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/3 "2014-01-03T07:00:14Z")

</div>

Hi Ivan,

Below given is my custom class. How do I direct my search queries to this  
end-point using the ES-Java API.

public class CustomRestAction extends BaseRestHandler {  
@Inject  
public CustomRestAction(Settings settings, Client client, RestController  
controller) {  
super(settings, client);  
controller.registerHandler(Method.GET, "/\_mastering/nodes", this);  
}

@Override  
public void handleRequest(RestRequest request, RestChannel channel) {  
String prefix = request.param("prefix", "");  
NodesInfoResponse response =  
client.admin().cluster().prepareNodesInfo().all().execute().actionGet();  
List nodes = new ArrayList();  
for (NodeInfo nodeInfo : response.getNodes()) {  
String nodeName = nodeInfo.getNode().getName();  
if (prefix.isEmpty()) {  
nodes.add(nodeName);  
} else if (nodeName.startsWith(prefix)) {  
nodes.add(nodeName);  
}  
}  
try {  
sendResponse(request, channel, nodes);  
} catch (IOException ioe) {  
logger.error("Error sending response", ioe);  
}  
return;  
}

private void sendResponse(RestRequest request, RestChannel channel, List  
nodes) throws IOException {  
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);  
builder.startObject().startArray("nodes");  
if (!nodes.isEmpty()) {  
builder.value(nodes);  
}  
builder.endArray().endObject();  
channel.sendResponse(new XContentRestResponse(request, RestStatus.OK,  
builder));  
}

}

public class CustomRestActionPlugin extends AbstractPlugin {  
@Inject  
public CustomRestActionPlugin(Settings settings) {  
}

public void onModule(RestModule module) {  
module.addRestAction(CustomRestAction.class);  
}

@Override  
public String name() {  
return "CustomRestActionPlugin";  
}

@Override  
public String description() {  
return "Custom REST action";  
}  
}

On Thursday, 2 January 2014 23:52:51 UTC+5:30, Ivan Brusic wrote:

> You would need to supply information about your custom class for more  
> details. Did you create a new Action hierarchy? The handleRequest method of  
> your BaseRestHandler implementation should provide details on how the Java  
> API is used.
> 
> Cheers,
> 
> Ivan
> 
> On Thu, Jan 2, 2014 at 5:14 AM, Shishir Kumar \<[shishi...@gmail.com](mailto:shishi...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,  
> > I have implemented a simple custom rest handler class for Elasticsearch.  
> > If I need to call it using curl it works just fine.  
> > curl -XGET '10.114.24.132:9200/\_mastering/nodes?pretty'
> > 
> > However, I want to call this using the Elasticsearch Java api (with an  
> > embedded client node). Could you please help me with this? I am new to  
> > Elasticsearch and not able to figure this out.
> > 
> > P.S. I had followed the instructions given in the link \<  
> > [http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/](http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/)\>  
> > for setting up the custom handler.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fb4f8bf3-c32d-4f77-964b-ec31ab8c5060%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fb4f8bf3-c32d-4f77-964b-ec31ab8c5060%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [January 3, 2014, 8:05am UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/4 "2014-01-03T08:05:13Z")

</div>

You have wrapped a NodesInfoAction, so all you have to do is

NodesInfoResponse response = client.admin().cluster().  
prepareNodesInfo().all().execute().actionGet();

That is the Java API.

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGS7h-pAGu52Rmv1W3VXs%2BA9qcxp1Lc0o21pVkQ2DcwyA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGS7h-pAGu52Rmv1W3VXs%2BA9qcxp1Lc0o21pVkQ2DcwyA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Shishir\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Shishir\_Kumar](https://discuss.elastic.co/u/Shishir_Kumar)\
**Post date:** [January 3, 2014, 8:30am UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/5 "2014-01-03T08:30:23Z")

</div>

Hi Ivan,

Below given is my custom class. How do I direct my search queries to this  
end-point using the ES-Java API.

public class CustomRestAction extends BaseRestHandler {  
@Inject  
public CustomRestAction(Settings settings, Client client, RestController  
controller) {  
super(settings, client);  
controller.registerHandler(Method.GET, "/\_mastering/nodes", this);  
}

@Override  
public void handleRequest(RestRequest request, RestChannel channel) {  
String prefix = request.param("prefix", "");  
NodesInfoResponse response =  
client.admin().cluster().prepareNodesInfo().all().execute().actionGet();  
List nodes = new ArrayList();  
for (NodeInfo nodeInfo : response.getNodes()) {  
String nodeName = nodeInfo.getNode().getName();  
if (prefix.isEmpty()) {  
nodes.add(nodeName);  
} else if (nodeName.startsWith(prefix)) {  
nodes.add(nodeName);  
}  
}  
try {  
sendResponse(request, channel, nodes);  
} catch (IOException ioe) {  
logger.error("Error sending response", ioe);  
}  
return;  
}

private void sendResponse(RestRequest request, RestChannel channel, List  
nodes) throws IOException {  
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);  
builder.startObject().startArray("nodes");  
if (!nodes.isEmpty()) {  
builder.value(nodes);  
}  
builder.endArray().endObject();  
channel.sendResponse(new XContentRestResponse(request, RestStatus.OK,  
builder));  
}

}

On Thursday, 2 January 2014 23:52:51 UTC+5:30, Ivan Brusic wrote:

> You would need to supply information about your custom class for more  
> details. Did you create a new Action hierarchy? The handleRequest method of  
> your BaseRestHandler implementation should provide details on how the Java  
> API is used.
> 
> Cheers,
> 
> Ivan
> 
> On Thu, Jan 2, 2014 at 5:14 AM, Shishir Kumar \<[shishi...@gmail.com](mailto:shishi...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,  
> > I have implemented a simple custom rest handler class for Elasticsearch.  
> > If I need to call it using curl it works just fine.  
> > curl -XGET '10.114.24.132:9200/\_mastering/nodes?pretty'
> > 
> > However, I want to call this using the Elasticsearch Java api (with an  
> > embedded client node). Could you please help me with this? I am new to  
> > Elasticsearch and not able to figure this out.
> > 
> > P.S. I had followed the instructions given in the link \<  
> > [http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/](http://elasticsearchserverbook.com/creating-custom-elasticsearch-rest-action/)\>  
> > for setting up the custom handler.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a352a4d-3ac6-4970-95de-80d56e4d7dec%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7e0a1ea7-1039-4b8a-bc87-715bd61c1a84%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7e0a1ea7-1039-4b8a-bc87-715bd61c1a84%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Shishir\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Shishir\_Kumar](https://discuss.elastic.co/u/Shishir_Kumar)\
**Post date:** [January 8, 2014, 8:46am UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/6 "2014-01-08T08:46:58Z")

</div>

Hi,

I am not facing any issue with the NodesInfoAction or the custom endpoint  
code. The rest endpoint is working fine, if I curl to it using:curl -XGET '  
localhost:9200/\_mastering/nodes?pretty[http://10.114.24.132:9200/\_mastering/nodes?pretty](http://10.114.24.132:9200/_mastering/nodes?pretty)  
'.

I am trying to find out a way to do this from an embedded node. In other  
words, somthing like below:

```
    Node node = 

```

NodeBuilder.nodeBuilder().clusterName("elasticsearch").node();  
Client client = node.client();  
SearchResponse response =  
client.prepareSearch().setSearchType("/\_mastering/nodes").  
setQuery(QueryBuilders.queryString("")).  
execute().actionGet();

P.S. the code snippet doesn't actually work. But I want to query the  
"/\_mastering/nodes" through Java api.

On Friday, 3 January 2014 13:35:13 UTC+5:30, Jörg Prante wrote:

> You have wrapped a NodesInfoAction, so all you have to do is
> 
> NodesInfoResponse response = client.admin().cluster().  
> prepareNodesInfo().all().execute().actionGet();
> 
> That is the Java API.
> 
> Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/bc517dbc-8dfc-4b8e-a9b3-567d87cc734c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bc517dbc-8dfc-4b8e-a9b3-567d87cc734c%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 8, 2014, 3:37pm UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/7 "2014-01-08T15:37:03Z")

</div>

The CustomRestAction code you posted contains _exactly_ the Java code you  
need to execute the same action as the REST action.

If you want to still want to use the REST URL, you cannot use the  
elasticsearch libraries. "/\_mastering/nodes" is not a valid search type.  
The action does not even execute a query technically, but retrieves node  
leve information.

Cheers,

Ivan

On Wed, Jan 8, 2014 at 12:46 AM, Shishir Kumar [shishir.sunny@gmail.com](mailto:shishir.sunny@gmail.com)wrote:

> Hi,
> 
> I am not facing any issue with the NodesInfoAction or the custom endpoint  
> code. The rest endpoint is working fine, if I curl to it using:curl -XGET '  
> localhost:9200/\_mastering/nodes?pretty[http://10.114.24.132:9200/\_mastering/nodes?pretty](http://10.114.24.132:9200/_mastering/nodes?pretty)  
> '.
> 
> I am trying to find out a way to do this from an embedded node. In other  
> words, somthing like below:
> 
> ```
> Node node =
> 
> ```
> 
> NodeBuilder.nodeBuilder().clusterName("elasticsearch").node();  
> Client client = node.client();  
> SearchResponse response =  
> client.prepareSearch().setSearchType("/\_mastering/nodes").  
> setQuery(QueryBuilders.queryString("")).  
> execute().actionGet();
> 
> P.S. the code snippet doesn't actually work. But I want to query the  
> "/\_mastering/nodes" through Java api.
> 
> On Friday, 3 January 2014 13:35:13 UTC+5:30, Jörg Prante wrote:
> 
> > You have wrapped a NodesInfoAction, so all you have to do is
> > 
> > NodesInfoResponse response = client.admin().cluster().prepa  
> > reNodesInfo().all().execute().actionGet();
> > 
> > That is the Java API.
> > 
> > Jörg
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/bc517dbc-8dfc-4b8e-a9b3-567d87cc734c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bc517dbc-8dfc-4b8e-a9b3-567d87cc734c%40googlegroups.com)  
> > .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD3Z-Ui8swVzkLoxASUiyuU9aWAeKpMkMkXfbN23vOGGQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD3Z-Ui8swVzkLoxASUiyuU9aWAeKpMkMkXfbN23vOGGQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 1:57am UTC](https://discuss.elastic.co/t/how-to-query-custom-rest-handler-in-elastic-search-using-java-api/15079/8 "2017-07-06T01:57:55Z")

</div>


