Convert SearchSourceBuilder to Json


(Deepak) #1

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.


(David Pilato) #2
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() ?


(Deepak) #3

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


(Deepak) #4

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


(David Pilato) #5

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


(Deepak) #6

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.


(system) #7

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