Expand my Community achievements bar.

SOLVED

SearchResult in json

Avatar

Level 1

Hi,

   I'm using the Querybuilder api to query CQ within a Sling servlet. I want to know if there is a way to get the SearchResult in json directly?

   Below is the code I'm doing in the servlet - 

                @SuppressWarnings("deprecation")
                ResourceResolver resourceResolver = resolverFactory
                        .getAdministrativeResourceResolver(null);
                session = resourceResolver.adaptTo(Session.class);

        //        String fulltextSearchTerm = "Geometrixx";

                // create query description as hash map (simplest way, same as form
                // post)
                Map<String, String> map = new HashMap<String, String>();

                // create query description as hash map (simplest way, same as form
                // post)

        //        map.put("path", "/content");
                map.put("type", "dam:Asset");
        
                // can be done in map or with Query methods
                map.put("p.hits", "full");
                map.put("p.offset", "0"); // same as query.setStart(0) below
                map.put("p.limit", "10"); // same as query.setHitsPerPage(20) below
                map.put("p.nodedepth", "2");
                
                    
                
                Query query = builder.createQuery(PredicateGroup.create(map),
                        session);

            //    query.setStart(0);
            //    query.setHitsPerPage(20);

                SearchResult result = query.getResult();

 

 

Thank you,

Koshy

1 Accepted Solution

Avatar

Correct answer by
Level 10

When using Java API - you will have to write business logic to read the nodes and then produce JSON data. 

View solution in original post

4 Replies

Avatar

Level 10

When using QueryBuilder Java API- the result set is nodes.

// iterating over the results
for (Hit hit : result.getHits()) {
   String path = hit.getPath();
 
    //Create a result element
    Element resultel = doc.createElement( "result" );
    root.appendChild( resultel );
                     
    Element pathel = doc.createElement( "path" );
    pathel.appendChild( doc.createTextNode(path ) );
    resultel.appendChild( pathel );
}

 

However - you can write an AEM service and use app logic to read the nodes and encode the data into JSON. For example - use the simple JSON API. We have an example here:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

JSONObject obj=new JSONObject();
obj.put("id","id");
//etc

Avatar

Employee Advisor

The QueryBuilder already provides a REST interface delivering the results in JSON:

http://localhost:4502/bin/querybuilder.json?type=dam:Asset&hits=full&nodedepth=2&offset=0&limit=10

kind regards,
Jörg

Avatar

Level 1

Thank you Jorg.

I wanted to try out using the querybuilder api in java without using the url call.

Avatar

Correct answer by
Level 10

When using Java API - you will have to write business logic to read the nodes and then produce JSON data.