Convert SearchSourceBuilder to Json

Hi all,
May be this question was asked before See actual JSON from SearchRequestBuilder but I am completely new to the elastic search.

I am trying to find a way to convert the queryBuilder/searchSourceBuilder to a Json.

I am trying out the very basic "match_all" query like below:

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    searchSourceBuilder.query(QueryBuilders.matchAllQuery());

    searchSourceBuilder.from(0); 

    searchSourceBuilder.size(5);

I need to send the "json" from this builder to the REST end point to grab the results.
Please help me find out how can I convert the builder to json

Also, I am not able to find any examples on how to use SearchRequestBuilder to see if I can use the above mentioned link for my use case.

SearchSourceBuilder ssb = new SearchSourceBuilder()
        .query(QueryBuilders.matchAllQuery())
        .from(0)
        .size(5);
logger.info("{}", ssb);

Gives:

08:23:42,017 INFO [f.p.t.e.t.i.ElasticsearchIT] {"from":0,"size":5,"query":{"match_all":{"boost":1.0}}}

So call ssb.toString() ?

Thanks for your reply.
But I already tried that and its printing the object reference "org.elasticsearch.search.builder.SearchSourceBuilder@6e0be858"

I wish I can get the query like that!
Any settings you did at your side?

I'm using 6.4.3 version. May be you are using an old one?

Excellent. I was using some old versioned jar looks like.

I changed it to 6.4.2 like you mentioned and its printing the query as below:
-> {"from":0,"size":5,"query":{"match_all":{"boost":1.0}}}

Thank you @dadoonet. Appreciate you idea.

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