Facets

Terms Facet

Here is how you can use Terms Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.termsFacet("f")
    .field("brand")
    .size(10);

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.terms.*;
// sr is here your SearchResponse object
TermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("f");

f.getTotalCount();      // Total terms doc count
f.getOtherCount();      // Not shown terms doc count
f.getMissingCount();    // Without term doc count

// For each entry
for (TermsFacet.Entry entry : f) {
    entry.getTerm();    // Term
    entry.getCount();   // Doc count
}

Range Facet

Here is how you can use Range Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.rangeFacet("f")
    .field("price")         // Field to compute on
    .addUnboundedFrom(3)    // from -infinity to 3 (excluded)
    .addRange(3, 6)         // from 3 to 6 (excluded)
    .addUnboundedTo(6);     // from 6 to +infinity

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.range.*;
// sr is here your SearchResponse object
RangeFacet f = (RangeFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (RangeFacet.Entry entry : f) {
    entry.getFrom();    // Range from requested
    entry.getTo();      // Range to requested
    entry.getCount();   // Doc count
    entry.getMin();     // Min value
    entry.getMax();     // Max value
    entry.getMean();    // Mean
    entry.getTotal();   // Sum of values
}

Histogram Facet

Here is how you can use Histogram Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

HistogramFacetBuilder facet = FacetBuilders.histogramFacet("f")
    .field("price")
    .interval(1);

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.histogram.*;
// sr is here your SearchResponse object
HistogramFacet f = (HistogramFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (HistogramFacet.Entry entry : f) {
    entry.getKey();     // Key (X-Axis)
    entry.getCount();   // Doc count (Y-Axis)
}

Date Histogram Facet

Here is how you can use Date Histogram Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.dateHistogramFacet("f")
    .field("date")      // Your date field
    .interval("year");  // You can also use "quarter", "month", "week", "day",
                        // "hour" and "minute" or notation like "1.5h" or "2w"

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.datehistogram.*;
// sr is here your SearchResponse object
DateHistogramFacet f = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (DateHistogramFacet.Entry entry : f) {
    entry.getTime();    // Date in ms since epoch (X-Axis)
    entry.getCount();   // Doc count (Y-Axis)
}

Filter Facet (not facet filter)

Here is how you can use Filter Facet with Java API.

If you are looking on how to apply a filter to a facet, have a look at facet filter using Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.filterFacet("f",
    FilterBuilders.termFilter("brand", "heineken"));    // Your Filter here

See Filters to learn how to build filters using Java.

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.filter.*;
// sr is here your SearchResponse object
FilterFacet f = (FilterFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();   // Number of docs that matched

Query Facet

Here is how you can use Query Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.queryFacet("f",
    QueryBuilders.matchQuery("brand", "heineken"));

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.query.*;
// sr is here your SearchResponse object
QueryFacet f = (QueryFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();   // Number of docs that matched

See Queries to learn how to build queries using Java.

Statistical

Here is how you can use the Statistical Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.statisticalFacet("f")
   .field("price");

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.statistical.*;
// sr is here your SearchResponse object
StatisticalFacet f = (StatisticalFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();           // Doc count
f.getMin();             // Min value
f.getMax();             // Max value
f.getMean();            // Mean
f.getTotal();           // Sum of values
f.getStdDeviation();    // Standard Deviation
f.getSumOfSquares();    // Sum of Squares
f.getVariance();        // Variance

Terms Stats Facet

Here is how you can use the Terms Stats Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.termsStatsFacet("f")
    .keyField("brand")
    .valueField("price");

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.termsstats.*;
// sr is here your SearchResponse object
TermsStatsFacet f = (TermsStatsFacet) sr.getFacets().facetsAsMap().get("f");
f.getTotalCount();      // Total terms doc count
f.getOtherCount();      // Not shown terms doc count
f.getMissingCount();    // Without term doc count

// For each entry
for (TermsStatsFacet.Entry entry : f) {
    entry.getTerm();            // Term
    entry.getCount();           // Doc count
    entry.getMin();             // Min value
    entry.getMax();             // Max value
    entry.getMean();            // Mean
    entry.getTotal();           // Sum of values
}

Geo Distance Facet

Here is how you can use the Geo Distance Facet with Java API.

Prepare facet request

Here is an example on how to create the facet request:

FacetBuilders.geoDistanceFacet("f")
    .field("pin.location")              // Field containing coordinates we want to compare with
    .point(40, -70)                     // Point from where we start (0)
    .addUnboundedFrom(10)               // 0 to 10 km (excluded)
    .addRange(10, 20)                   // 10 to 20 km (excluded)
    .addRange(20, 100)                  // 20 to 100 km (excluded)
    .addUnboundedTo(100)                // from 100 km to infinity (and beyond ;-) )
    .unit(DistanceUnit.KILOMETERS);     // All distances are in kilometers. Can be MILES

Use facet response

Import Facet definition classes:

import org.elasticsearch.search.facet.geodistance.*;
// sr is here your SearchResponse object
GeoDistanceFacet f = (GeoDistanceFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (GeoDistanceFacet.Entry entry : f) {
    entry.getFrom();            // Distance from requested
    entry.getTo();              // Distance to requested
    entry.getCount();           // Doc count
    entry.getMin();             // Min value
    entry.getMax();             // Max value
    entry.getTotal();           // Sum of values
    entry.getMean();            // Mean
}