facets
Elasticsearch提供完整的java API用來支持facets。在查詢的過程中,將需要計數的facets添加到FacetBuilders中。然后將該FacetBuilders條件到查詢請求中。
SearchResponse sr = node.client().prepareSearch()
.setQuery( /* your query */ ) .addFacet( /* add a facet */ ) .execute().actionGet();
為了構建facets請求,需要用到FacetBuilders幫助類。你只需要在你的程序中導入它即可。
import org.elasticsearch.search.facet.FacetBuilders.*;
terms facet
准備一個facet請求
下面的例子新建一個facet請求
FacetBuilders.termsFacet("f") .field("brand") .size(10);
利用facet響應
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 }
范圍facet
准備一個facet請求
下面的例子新建一個facet請求
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
利用facet響應
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
准備一個facet請求
下面的例子新建一個facet請求
HistogramFacetBuilder facet = FacetBuilders.histogramFacet("f") .field("price") .interval(1);
利用facet響應
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) }
日期直方圖(Histogram) Facet
准備一個facet請求
下面的例子新建一個facet請求
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"
利用facet響應
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) }
過濾facet(不是facet過濾)
准備一個facet請求
下面的例子新建一個facet請求
FacetBuilders.filterFacet("f", FilterBuilders.termFilter("brand", "heineken")); // Your Filter here
利用facet響應
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
查詢facet
准備一個facet請求
下面的例子新建一個facet請求
FacetBuilders.queryFacet("f", QueryBuilders.matchQuery("brand", "heineken"));
利用facet響應
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
統計
准備一個facet請求
下面的例子新建一個facet請求
FacetBuilders.statisticalFacet("f") .field("price");
利用facet響應
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
准備一個facet請求
下面的例子新建一個facet請求
FacetBuilders.termsStatsFacet("f") .keyField("brand") .valueField("price");
利用facet響應
// 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 }
地理距離Facet
准備一個facet請求
下面的例子新建一個facet請求
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
利用facet響應
// 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 }
facet過濾器(不是過濾facet)
默認情況下,不管過濾器存在與否,facet都是作用在查詢的結果集上。如果你需要計數帶有過濾器的facet,你能夠通過AbstractFacetBuilder#facetFilter(FilterBuilder)添加 過濾器到任何facet上。
FacetBuilders
.termsFacet("f").field("brand") // Your facet .facetFilter( // Your filter here FilterBuilders.termFilter("colour", "pale") );
例如,你可以在你的查詢中重用創建的過濾器
// A common filter FilterBuilder filter = FilterBuilders.termFilter("colour", "pale"); TermsFacetBuilder facet = FacetBuilders.termsFacet("f") .field("brand") .facetFilter(filter); // We apply it to the facet SearchResponse sr = node.client().prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .setFilter(filter) // We apply it to the query .addFacet(facet) .execute().actionGet();
作用域
默認情況下,facet作用在查詢的結果集上。但是,不管是哪個查詢,你可以用global參數去計算來自於索引中的所有文檔的facet。
TermsFacetBuilder facet = FacetBuilders.termsFacet("f") .field("brand") .global(true);
