See actual JSON from SearchRequestBuilder

Hi,

I have sort of a complex logic to build a search request using the Java API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

is this what you want?

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

On 10 Feb., 21:00, Enrique Medina Montenegro e.medin...@gmail.com
wrote:

Hi,

I have sort of a complex logic to build a search request using the Java API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

Also, you should be able to use any HTTP sniffer (there is Fiddler on
Windows, and I'm sure there are counterparts on other systems) which don't
require you to change your code so significantly.

-----Original Message-----
From: Karussell [mailto:tableyourtime@googlemail.com]
Sent: Thursday, February 10, 2011 3:04 PM
To: users
Subject: Re: See actual JSON from SearchRequestBuilder

is this what you want?

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

On 10 Feb., 21:00, Enrique Medina Montenegro e.medin...@gmail.com
wrote:

Hi,

I have sort of a complex logic to build a search request using the Java
API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

although it would be interesting if there is a logging settings for
the node ...

On 10 Feb., 21:05, "Administrator" ad...@sf4answers.com wrote:

Also, you should be able to use any HTTP sniffer (there is Fiddler on
Windows, and I'm sure there are counterparts on other systems) which don't
require you to change your code so significantly.

-----Original Message-----
From: Karussell [mailto:tableyourt...@googlemail.com]
Sent: Thursday, February 10, 2011 3:04 PM
To: users
Subject: Re: See actual JSON from SearchRequestBuilder

is this what you want?

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

On 10 Feb., 21:00, Enrique Medina Montenegro e.medin...@gmail.com
wrote:

Hi,

I have sort of a complex logic to build a search request using the Java
API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

I forgot to mention that the object where I build the search using the Java
API is a SearchRequestBuilder.

On Thu, Feb 10, 2011 at 9:22 PM, Karussell tableyourtime@googlemail.comwrote:

although it would be interesting if there is a logging settings for
the node ...

On 10 Feb., 21:05, "Administrator" ad...@sf4answers.com wrote:

Also, you should be able to use any HTTP sniffer (there is Fiddler on
Windows, and I'm sure there are counterparts on other systems) which
don't
require you to change your code so significantly.

-----Original Message-----
From: Karussell [mailto:tableyourt...@googlemail.com]
Sent: Thursday, February 10, 2011 3:04 PM
To: users
Subject: Re: See actual JSON from SearchRequestBuilder

is this what you want?

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

On 10 Feb., 21:00, Enrique Medina Montenegro e.medin...@gmail.com
wrote:

Hi,

I have sort of a complex logic to build a search request using the Java
API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

You can get the internal builder that implements the ToXContent (which allows you to print it as json) using SearchRequestBuilder#internalBuilder.
On Thursday, February 10, 2011 at 11:06 PM, Enrique Medina Montenegro wrote:

I forgot to mention that the object where I build the search using the Java API is a SearchRequestBuilder.

On Thu, Feb 10, 2011 at 9:22 PM, Karussell tableyourtime@googlemail.com wrote:

although it would be interesting if there is a logging settings for
the node ...

On 10 Feb., 21:05, "Administrator" ad...@sf4answers.com wrote:

Also, you should be able to use any HTTP sniffer (there is Fiddler on
Windows, and I'm sure there are counterparts on other systems) which don't
require you to change your code so significantly.

-----Original Message-----
From: Karussell [mailto:tableyourt...@googlemail.com]
Sent: Thursday, February 10, 2011 3:04 PM
To: users
Subject: Re: See actual JSON from SearchRequestBuilder

is this what you want?

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

On 10 Feb., 21:00, Enrique Medina Montenegro e.medin...@gmail.com
wrote:

Hi,

I have sort of a complex logic to build a search request using the Java
API.
I'm finding a corner case which is not working as expected, and I'm
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES
server would be extremely helpful. Is there any way to achieve this? In
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

1 Like

Hope this code snippet may help you

try {
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY_PARAMS);
String s1 = builderString1.string();

} catch (IOException e) {
e.printStackTrace();
}

Eventually, this is what I used:

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

SearchRequestBuilder search = new ....

log.debug(toString(search.internalBuilder())

@Shay: would it make sense to add this to the SearchRequestBuilder class as
an aid to debugging? I can take over that if you want...

That way we could do simply:

search.toJSON()

On Fri, Feb 11, 2011 at 10:00 AM, srrIN srrranga@gmail.com wrote:

Hope this code snippet may help you

try {
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY_PARAMS);
String s1 = builderString1.string();

} catch (IOException e) {
e.printStackTrace();
}

View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

+1i think this would help
On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" e.medina.m@gmail.com
wrote:

Eventually, this is what I used:

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

SearchRequestBuilder search = new ....

log.debug(toString(search.internalBuilder())

@Shay: would it make sense to add this to the SearchRequestBuilder class
as
an aid to debugging? I can take over that if you want...

That way we could do simply:

search.toJSON()

On Fri, Feb 11, 2011 at 10:00 AM, srrIN srrranga@gmail.com wrote:

Hope this code snippet may help you

try {
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY_PARAMS);
String s1 = builderString1.string();

} catch (IOException e) {
e.printStackTrace();
}

View this message in context:

http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html

Sent from the Elasticsearch Users mailing list archive at Nabble.com.

Right, a simpel String toJson can help in debugging. Actually, it can help on any class that implements ToXContent.
On Friday, February 11, 2011 at 3:05 PM, Rich Kroll wrote:

+1i think this would help
On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" e.medina.m@gmail.com wrote:

Eventually, this is what I used:

public static String toString(ToXContent tmp) {
try {
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

SearchRequestBuilder search = new ....

log.debug(toString(search.internalBuilder())

@Shay: would it make sense to add this to the SearchRequestBuilder class as
an aid to debugging? I can take over that if you want...

That way we could do simply:

search.toJSON()

On Fri, Feb 11, 2011 at 10:00 AM, srrIN srrranga@gmail.com wrote:

Hope this code snippet may help you

try {
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY_PARAMS);
String s1 = builderString1.string();

} catch (IOException e) {
e.printStackTrace();
}

View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

Or even a log.debug() statement when executing it, for instance :wink:

On Fri, Feb 11, 2011 at 2:57 PM, Shay Banon shay.banon@elasticsearch.comwrote:

Right, a simpel String toJson can help in debugging. Actually, it can
help on any class that implements ToXContent.

On Friday, February 11, 2011 at 3:05 PM, Rich Kroll wrote:

+1i think this would help
On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" e.medina.m@gmail.com
wrote:

Eventually, this is what I used:

public static String toString(ToXContent tmp) {
try {{
return
tmp.toXContent(JsonXContent.unCachedContentBuilder(),
ToXContent.EMPTY_PARAMS).
prettyPrint().
string();
} catch (Exception ex) {
return "<ERROR:" + ex.getMessage() + ">";
}
}

SearchRequestBuilder search = new ....

log.debug(toString(search.internalBuilder())

@Shay: would it make sense to add this to the SearchRequestBuilder class
as
an aid to debugging? I can take over that if you want...

That way we could do simply:

search.toJSON()

On Fri, Feb 11, 2011 at 10:00 AM, srrIN srrranga@gmail.com wrote:

Hope this code snippet may help you

try {
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1,
ToXContent.EMPTY_PARAMS);
String s1 = builderString1.string();

} catch (IOException e) {
e.printStackTrace();
}

View this message in context:

http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html

Sent from the Elasticsearch Users mailing list archive at Nabble.com.