From JSON to Java API and back

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

Hi Petar,

SearchResponse has a writeTo and a readFrom methods taking a
Stream[Input/Output], if think it should do the job ?

Concerning the deserialization I think it's better to use the static
method 'readSearchResponse' that return a new SearchResponse instead
of using the readFrom instance method.

Hope that's help !

Cheers,

Alois Cochard

http://www.twitter.com/aloiscochard

On Mar 9, 2:54 pm, paranoiabla paranoia...@gmail.com wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.
On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.comwrote:

SearchResponse implements the ToXContent interface, which means you can
use XContentFactory to create a json builder, pass it to the toXContent
method, and then get its binary / string representation. There is no way to
parse a json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.

Note, you will still need to explicitly set the search type as its not part of the json "body" request.
On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com wrote:

SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.
On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

    XContentBuilder builder =

XContentFactory.contentBuilder(XContentType.JSON);

    buildRequest(request).execute().actionGet().toXContent(builder,

ToXContent.EMPTY_PARAMS);

    String result = builder.string();

    System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com wrote:

SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

You need to wrap the toXContent call with startObject and endObject. Something like this:

builder.startObject();
response.toXContent(builder, request);
builder.endObject();

The stream is an internal serialization format, not a json one.
On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

BytesStreamOutput stream = new BytesStreamOutput();

buildRequest(request).execute().actionGet().writeTo(stream);

stream.close();

String result = new String(stream.copiedByteArray());

System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

XContentBuilder builder =
XContentFactory.contentBuilder(XContentType.JSON);

buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

String result = builder.string();

System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com wrote:

SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile: 򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

    XContentBuilder builder =

XContentFactory.contentBuilder(XContentType.JSON);

    buildRequest(request).execute().actionGet().toXContent(builder,

ToXContent.EMPTY_PARAMS);

    String result = builder.string();

    System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile: 򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

    XContentBuilder builder =

XContentFactory.contentBuilder(XContentType.JSON);

    buildRequest(request).execute().actionGet().toXContent(builder,

ToXContent.EMPTY_PARAMS);

    String result = builder.string();

    System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

You don't need to send any parameters. Can you gist the code you use now?
On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile: 򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

BytesStreamOutput stream = new BytesStreamOutput();

buildRequest(request).execute().actionGet().writeTo(stream);

stream.close();

String result = new String(stream.copiedByteArray());

System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

XContentBuilder builder =
XContentFactory.contentBuilder(XContentType.JSON);

buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

String result = builder.string();

System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

check gist:864108 · GitHub for my code snippet

The expected JSON which should be createdshould look like this
{

* took: 9
* timed_out: false
* _shards: {
      o total: 5
      o successful: 5
      o failed: 0
  }

..............
instead after this code is invoked a content which looks like this is returned:
:slight_smile:
򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot

so you see in the begining there are some strange non normally encoded
symbols ... in the toXContent method of XContentBuilder I see that the
method starts with adding a fields starting with TOOK... and etc
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut());
(because the scroll_id is null in my case ) so .. I dont know why at
the end before took somewhere this strangly encoded query is passed. (
it looks like my search query but the encoding is wrong ) )

Thanks with the help.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You don't need to send any parameters. Can you gist the code you use now?

On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile:
򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

    XContentBuilder builder =

XContentFactory.contentBuilder(XContentType.JSON);

    buildRequest(request).execute().actionGet().toXContent(builder,

ToXContent.EMPTY_PARAMS);

    String result = builder.string();

    System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Make sure you create the builder after you execute the search request. Also, you have the option of the builder outputting the json directly to the servlet output stream by using:

XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(JSON), OutputStream);

p.s.

The above is kindda fugly, so I pushed simplified way to do that (in master):

XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)

On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:

check gist:864108 · GitHub for my code snippet

The expected JSON which should be createdshould look like this
{

  • took: 9
  • timed_out: false
  • _shards: {
    o total: 5
    o successful: 5
    o failed: 0
    }
    ..............
    instead after this code is invoked a content which looks like this is returned:
    :slight_smile:
    򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot

so you see in the begining there are some strange non normally encoded
symbols ... in the toXContent method of XContentBuilder I see that the
method starts with adding a fields starting with TOOK... and etc
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut());
(because the scroll_id is null in my case ) so .. I dont know why at
the end before took somewhere this strangly encoded query is passed. (
it looks like my search query but the encoding is wrong ) )

Thanks with the help.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You don't need to send any parameters. Can you gist the code you use now?

On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile:
򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

BytesStreamOutput stream = new BytesStreamOutput();

buildRequest(request).execute().actionGet().writeTo(stream);

stream.close();

String result = new String(stream.copiedByteArray());

System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

XContentBuilder builder =
XContentFactory.contentBuilder(XContentType.JSON);

buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

String result = builder.string();

System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Hi Shay,

swaping the two lines seems to work good. I still think though that it
is a really bad practice to
hide such "functionality" with the code.

Thanks for your support.

Cheers, Petar.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

Make sure you create the builder after you execute the search request.
Also, you have the option of the builder outputting the json directly to the
servlet output stream by using:
XContentBuilder builder = new
XContentBuilder(XContentFactory.xContent(JSON), OutputStream);
p.s.
The above is kindda fugly, so I pushed simplified way to do that (in
master):
XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)

On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:

check gist:864108 · GitHub for my code snippet

The expected JSON which should be createdshould look like this
{

  • took: 9
  • timed_out: false
  • _shards: {
    o total: 5
    o successful: 5
    o failed: 0
    }
    ..............
    instead after this code is invoked a content which looks like this is
    returned:
    :slight_smile:
    򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot

so you see in the begining there are some strange non normally encoded
symbols ... in the toXContent method of XContentBuilder I see that the
method starts with adding a fields starting with TOOK... and etc
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut());
(because the scroll_id is null in my case ) so .. I dont know why at
the end before took somewhere this strangly encoded query is passed. (
it looks like my search query but the encoding is wrong ) )

Thanks with the help.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You don't need to send any parameters. Can you gist the code you use now?

On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile:
򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

    XContentBuilder builder =

XContentFactory.contentBuilder(XContentType.JSON);

    buildRequest(request).execute().actionGet().toXContent(builder,

ToXContent.EMPTY_PARAMS);

    String result = builder.string();

    System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: Oracle.com Outage


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Hide what functionality with the code? And, make sure you don't convert it to String and then back to bytes, you can get the bytes out of it.
On Thursday, March 10, 2011 at 4:10 PM, Petar Tahchiev wrote:

Hi Shay,

swaping the two lines seems to work good. I still think though that it
is a really bad practice to
hide such "functionality" with the code.

Thanks for your support.

Cheers, Petar.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

Make sure you create the builder after you execute the search request.
Also, you have the option of the builder outputting the json directly to the
servlet output stream by using:
XContentBuilder builder = new
XContentBuilder(XContentFactory.xContent(JSON), OutputStream);
p.s.
The above is kindda fugly, so I pushed simplified way to do that (in
master):
XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)

On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:

check gist:864108 · GitHub for my code snippet

The expected JSON which should be createdshould look like this
{

  • took: 9
  • timed_out: false
  • _shards: {
    o total: 5
    o successful: 5
    o failed: 0
    }
    ..............
    instead after this code is invoked a content which looks like this is
    returned:
    :slight_smile:
    򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot

so you see in the begining there are some strange non normally encoded
symbols ... in the toXContent method of XContentBuilder I see that the
method starts with adding a fields starting with TOOK... and etc
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut());
(because the scroll_id is null in my case ) so .. I dont know why at
the end before took somewhere this strangly encoded query is passed. (
it looks like my search query but the encoding is wrong ) )

Thanks with the help.

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You don't need to send any parameters. Can you gist the code you use now?

On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

I guess I have to send some parameters other than EMPTY_PARAMS, but I
have no idea what exactly.

2011/3/10 Petar Tahchiev paranoiabla@gmail.com:

OK, this time no exception is thrown, but instead I get again weird
characters, like:
":slight_smile:
򄱵ery򇦩ltered򀺈match_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon shay.banon@elasticsearch.com:

You need to wrap the toXContent call with startObject and endObject.
Something like this:
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
The stream is an internal serialization format, not a json one.

On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing
something wrong).
I first tried:

BytesStreamOutput stream = new BytesStreamOutput();

buildRequest(request).execute().actionGet().writeTo(stream);

stream.close();

String result = new String(stream.copiedByteArray());

System.out.println("!!" + result);

but the result I get is a semi-json containing some just random weird
bytes (you can see the result.txt attached to this email)

Then I tried using:

XContentBuilder builder =
XContentFactory.contentBuilder(XContentType.JSON);

buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

String result = builder.string();

System.out.println("!!" + result);

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not
write a field name, expecting a value
org.elasticsearch.common.jackson.impl.JsonGeneratorBase._reportError(JsonGeneratorBase.java:481)
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line
buildRequest(request).execute().actionGet().toXContent(builder,
ToXContent.EMPTY_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon shay.banon@elasticsearch.com

The question was about SearchResponse, not SearchRequest :). Parsing the
search request from json is simple, just pass it using the
SearchRequset#source(...) method.
Note, you will still need to explicitly set the search type as its not part
of the json "body" request.

On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

@Shay
There should be a way to parse json request to something like SearchRequest
object, I think this is the second part of the question.
When we send a curl search request you probably convert the json to java
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon shay.banon@elasticsearch.com
wrote:

SearchResponse implements the ToXContent interface, which means you can use
XContentFactory to create a json builder, pass it to the toXContent method,
and then get its binary / string representation. There is no way to parse a
json to a SearchResponse.

On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

Hi there,

Im using the Java API and I want to know what is the best way to
convert the SearchResponse object into JSON format. Also If Im using
the JSON API and I receive back a JSON response, is there a way to
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

Attachments:

  • result.txt

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--
Regards, Petar!
Karlovo, Bulgaria.


| Author @ Manning Publications.
| CEO @ Phamola
| BGJUG-Bulgarian Java User Group Leader.
| Apache Maven Developer.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Codehaus Plexus Developer
| Blogger: http://weblogs.java.net/blog/paranoiabla/


Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611