### Question:
ESClient extends SearchClient { @Override public FacetResult facet(QueryRequest query) throws SearchClientException, IOException { String jsonRequest = buildFacetRequest(query); JsonResponse jsonResponse = postJson(url + "_search", jsonRequest); if (jsonResponse.getStatus() != 200) { throw new SearchClientException(jsonResponse.getMsg()); } JsonObject root = (JsonObject) jsonResponse.getJson(); long totalDocs = JsonUtil.getPrimitive(root.get("hits"), "total", -1l); JsonObject aggs = (JsonObject) root.get("aggregations"); JsonObject fieldAggs = (JsonObject) aggs.get(query.getFacetField()); JsonArray buckets = fieldAggs.getAsJsonArray("buckets"); Map<String, Long> counts = new HashMap<>(); for (JsonElement el : buckets) { String key = JsonUtil.getPrimitive(el, "key", ""); long cnt = JsonUtil.getPrimitive(el, "doc_count", -1l); counts.put(key, cnt); } return new FacetResult(totalDocs, counts); } ESClient(String url); @Override SearchResultSet search(QueryRequest query); @Override FacetResult facet(QueryRequest query); @Override void addDocuments(List<StoredDocument> documents); @Override List<StoredDocument> getDocs(String idField, Set<String> ids,
                                        Set<String> whiteListFields,
                                        Set<String> blackListFields); @Override Collection<? extends String> getCopyFields(); @Override String getDefaultIdField(); @Override void deleteAll(); @Override IdGrabber getIdGrabber(ArrayBlockingQueue<Set<String>> ids,
                                  int batchSize,
                                  int copierThreads,
                                  Collection<Query> filterQueries); @Override Set<String> getSystemInternalFields(); @Override List<String> analyze(String field, String string); @Override List<TokenDF> getTerms(String field, String lower,
                                  int limit, int minCount);  }### Answer:
@Test public void testFacets() throws Exception { SearchClient client = SearchClientFactory.getClient(TMDB_URL); QueryRequest queryRequest = new QueryRequest(ALL_DOCS); queryRequest.setFacetField("genres.facet"); queryRequest.setFacetLimit(10000); FacetResult result = client.facet(queryRequest); Map<String, Long> counts = result.getFacetCounts(); assertEquals(21, counts.size()); assertEquals(13582, counts.get("drama")); assertEquals(243, counts.get("tv movie")); queryRequest = new QueryRequest(ALL_DOCS); queryRequest.setFacetField("production_companies.facet"); queryRequest.setFacetLimit(100000); result = client.facet(queryRequest); counts = result.getFacetCounts(); assertEquals(15905, counts.size()); assertEquals(1, counts.get("haile gerima")); assertEquals(90, counts.get("dreamworks skg")); queryRequest = new QueryRequest(new TermQuery("title", "red")); queryRequest.setFacetLimit(20000); queryRequest.setFacetField("production_companies.facet"); result = client.facet(queryRequest); counts = result.getFacetCounts(); assertEquals(15905, counts.size()); assertEquals(2, counts.get("summit entertainment")); assertEquals(5, counts.get("paramount pictures")); }