Using Facets

edit

The following code shows how to add two facets within your search:

SearchResponse sr = node.client().prepareSearch()
    .setQuery(QueryBuilders.matchAllQuery())
    .addFacet(FacetBuilders.termsFacet("f1").field("field"))
    .addFacet(FacetBuilders.dateHistogramFacet("f2").field("birth").interval("year"))
    .execute().actionGet();

// Get your facet results
TermsFacet f1 = (TermsFacet) sr.getFacets().facetsAsMap().get("f1");
DateHistogramFacet f2 = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f2");

See Facets Java API documentation for details.