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


(Shishir Kumar) #1

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/
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.
To view this discussion on the web visit 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.


(Ivan Brusic) #2

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.comwrote:

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/>
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.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBMCycmgsU0x7duaWZYF7XyXMvLZRgvMhceSDCQQiu5vQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Shishir Kumar) #3

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<javascript:>

wrote:

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/>
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 <javascript:>.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit 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.


(Jörg Prante) #4

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGS7h-pAGu52Rmv1W3VXs%2BA9qcxp1Lc0o21pVkQ2DcwyA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Shishir Kumar) #5

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<javascript:>

wrote:

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/>
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 <javascript:>.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit 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.


(Shishir Kumar) #6

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?prettyhttp://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.
To view this discussion on the web visit 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.


(Ivan Brusic) #7

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.comwrote:

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?prettyhttp://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.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD3Z-Ui8swVzkLoxASUiyuU9aWAeKpMkMkXfbN23vOGGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #8