0

I am going to ask the same question again Convert SearchResponse to JsonObject

Actually I am using the same solution as mentioned in this question. But this solution is not working for me.

Below is my Elastic query:

AggregationBuilder aggregation = AggregationBuilders
                                    .terms("users")
                                     .field("USER")
                                     .size(10)
SearchResponse res = client.prepareSearch(_index)
                .setSize(0)
                .addAggregation(aggregation)
                .execute()
                .actionGet();

But when I try to convert it into a JSON object

JSONObject SRJSON = new JSONObject(res.toString());

It is saying

Constructor JSONObject(String) undefined

Remove argument to match JSONObject()

And I am using below maven plugin and elastic vs ~2.3:

<dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
</dependency>

1 Answer 1

1

I don't think that json-simple has JSONObject(String source) constructor and it's a compile time error. If you wish to follow the way it is being used in link to other thread, consider using the below dependency in your pom.xml:

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20180813</version>
</dependency> 

After including the dependency this snippet will work:

JSONObject SRJSON = new JSONObject(res.toString());

refer: JSONObject(String source)

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Great !! It solved my issue working like a charm now....Just curious to know...what are the other ways to do the same using other plugins ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.