public < T > Map < String , List < T > > queryGroups ( String query , Class < T > classOfT ) { InputStream instream = null ; try { Reader reader = new InputStreamReader ( instream = queryForStream ( query ) , "UTF-8" ) ; JsonObject json = new JsonParser ( ) . parse ( reader ) . getAsJsonObject ( ) ; Map < String , List < T > > result = new LinkedHashMap < String , List < T > > ( ) ; if ( json . has ( "groups" ) ) { for ( JsonElement e : json . getAsJsonArray ( "groups" ) ) { String groupName = e . getAsJsonObject ( ) . get ( "by" ) . getAsString ( ) ; List < T > orows = new ArrayList < T > ( ) ; if ( ! includeDocs ) { log . warning ( "includeDocs set to false and attempting to retrieve doc. " + "null object will be returned" ) ; } for ( JsonElement rows : e . getAsJsonObject ( ) . getAsJsonArray ( "rows" ) ) { orows . add ( jsonToObject ( client . getGson ( ) , rows , "doc" , classOfT ) ) ; } result . put ( groupName , orows ) ; } // end for(groups) } // end hasgroups else { log . warning ( "No grouped results available. Use query() if non grouped query" ) ; } return result ; } catch ( UnsupportedEncodingException e1 ) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException ( e1 ) ; } finally { close ( instream ) ; } }
Queries a Search Index and returns grouped results in a map where key of the map is the groupName . In case the query didnt use grouping an empty map is returned