Elasticsearch Chinese API Facets (⑩)

Source: Internet
Author: User

Facets

Elasticsearch provides a complete Java API to support facets. During the query, add the facets that you want to count to FacetBuilders . The condition is then FacetBuilders reached in the query request.

SearchResponse sr = node.client().prepareSearch()        /* your query */ )        .addFacet( /* add a facet */ )        .execute().actionGet();

To build the facets request, you need to use the FacetBuilders help class. You only need to import it in your program.

import org.elasticsearch.search.facet.FacetBuilders.*;
Terms facet prepare a facet request

The following example creates a new facet request

FacetBuilders.termsFacet("f")    .field("brand")    .size(10);
Using facet responses
import org.elasticsearch.search.facet.terms.*;// sr is here your SearchResponse objectTermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("f");f.getTotalCount();      // Total terms doc countf.getOtherCount();      // Not shown terms doc countf.getMissingCount(); // Without term doc count// For each entryfor (TermsFacet.Entry entry : f) { entry.getTerm(); // Term entry.getCount(); // Doc count}
Scope facet prepare a facet request

The following example creates a new 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
Using facet responses
import org.elasticsearch.search.facet.range.*;// sr is here your SearchResponse objectRangeFacet f = (RangeFacet) sr.getFacets().facetsAsMap().get("f");// For each entryfor (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 (histogram) facet prepare a facet request

The following example creates a new facet request

HistogramFacetBuilder facet = FacetBuilders.histogramFacet("f")    .field("price")    .interval(1);
Using facet responses
import org.elasticsearch.search.facet.histogram.*;// sr is here your SearchResponse objectHistogramFacet f = (HistogramFacet) sr.getFacets().facetsAsMap().get("f");// For each entryfor (HistogramFacet.Entry entry : f) { entry.getKey(); // Key (X-Axis) entry.getCount(); // Doc count (Y-Axis)}
Date histogram (histogram) facet prepare a facet request

The following example creates a new 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"
Using facet responses
import org.elasticsearch.search.facet.datehistogram.*;// sr is here your SearchResponse objectDateHistogramFacet f = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f");// For each entryfor (DateHistogramFacet.Entry entry : f) { entry.getTime(); // Date in ms since epoch (X-Axis) entry.getCount(); // Doc count (Y-Axis)}
Filter facet (not facet filter) prepare a facet request

The following example creates a new facet request

FacetBuilders.filterFacet("f",    FilterBuilders.termFilter("brand", "heineken"));    // Your Filter here
Using facet responses
import org.elasticsearch.search.facet.filter.*;// sr is here your SearchResponse objectFilterFacet f = (FilterFacet) sr.getFacets().facetsAsMap().get("f");f.getCount();   // Number of docs that matched
Query facet prepare a facet request

The following example creates a new facet request

FacetBuilders.queryFacet("f",    QueryBuilders.matchQuery("brand", "heineken"));
Using facet responses
import org.elasticsearch.search.facet.query.*;// sr is here your SearchResponse objectQueryFacet f = (QueryFacet) sr.getFacets().facetsAsMap().get("f");f.getCount();   // Number of docs that matched
Statistics prepare a facet request

The following example creates a new facet request

FacetBuilders.statisticalFacet("f")   .field("price");
Using facet responses
import org.elasticsearch.search.facet.statistical.*;// sr is here your SearchResponse objectStatisticalFacet f = (StatisticalFacet) sr.getFacets().facetsAsMap().get("f");f.getCount();           // Doc countf.getMin();             // Min valuef.getMax(); // Max valuef.getMean(); // Meanf.getTotal(); // Sum of valuesf.getStdDeviation(); // Standard Deviationf.getSumOfSquares(); // Sum of Squaresf.getVariance(); // Variance
Terms Stats facet prepare a facet request

The following example creates a new facet request

FacetBuilders.termsStatsFacet("f")    .keyField("brand")    .valueField("price");
Using facet responses
 //SR is here your searchresponse objecttermsstatsfacet f = (      TERMSSTATSFACET) Sr.getfacets (). Facetsasmap (). Get ( "F"); F.gettotalcount ();      //Total terms Doc countf.getothercount ();    //not shown terms Doc countf.getmissingcount (); //without term doc count//for each Entryfor (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}            
Geographic distance facet prepare a facet request

The following example creates a new facet request

Facetbuilders.geodistancefacet ("F"). Field ("Pin.location") //Field containing coordinates we want to compare with. Point (+-)//point from where we start (0 ). Addunboundedfrom (Ten) /0 to ten km (excluded). AddRange (ten ) //(excluded). AddRange (+) //-( excluded). Addunboundedto (+)//from (+) to infinity (and beyond ;-) ) . Unit (distanceunit.kilometers); //All distances is in kilometers. Can be MILES               
Using facet responses
// sr is here your SearchResponse objectGeoDistanceFacet f = (GeoDistanceFacet) sr.getFacets().facetsAsMap().get("f");// For each entryfor (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 Filter (not filter facet)

By default, facets are scoped to the result set of the query, regardless of whether the filter exists or not. If you need to count facets with filters, you can AbstractFacetBuilder#facetFilter(FilterBuilder) add filters to any facet.

FacetBuilders    .termsFacet("f").field("brand") // Your facet    .facetFilter( // Your filter here        FilterBuilders.termFilter("colour", "pale") );

For example, you can reuse a filter created in your query

// A common filterFilterBuilder filter = FilterBuilders.termFilter("colour", "pale");TermsFacetBuilder facet = FacetBuilders.termsFacet("f")    .field("brand") .facetFilter(filter); // We apply it to the facetSearchResponse sr = node.client().prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .setFilter(filter) // We apply it to the query .addFacet(facet) .execute().actionGet();
Scope

By default, facets Act on the result set of the query. However, regardless of the query, you can use global parameters to calculate facets from all documents in the index.

TermsFacetBuilder facet = FacetBuilders.termsFacet("f")    .field("brand")    .global(true);

Elasticsearch Chinese API Facets (⑩)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.